forked from SumedhArani/Traffic-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjList.java
More file actions
95 lines (85 loc) · 1.63 KB
/
Copy pathadjList.java
File metadata and controls
95 lines (85 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class adjList
{
node[] list;
private int vertices;
int []visit;
public adjList(int v)
{
vertices =v;
list = new node[v+1];
visit = new int[v+1];
}
public void insertEdge(int i, int weight,int j) //oVertex, distance, tVertex
{
node p;
p=list[i];
node temp = new node(weight,j);
if(p==null)
list[i]=temp;
else
{
while(p.getNext()!=null)
p=p.getNext();
p.setNext(temp);
}
node p1;
p1=list[j];
node temp1 = new node(weight,i);
if(p1==null)
list[j]=temp1;
else
{
while(p1.getNext()!=null)
p1=p1.getNext();
p1.setNext(temp1);
}
}
public int vertexCount()
{
return vertices+1;
}
public void deleteEdge(int i, int weight, int j)
{
node p =list[i];
node q =null;
//empty
if(p==null)
{
System.out.println("No edges to delete");
}
else
{
//first
if(p.getTvertex()==j && p.getDistance()==weight)
{
list[i] =p.getNext();
}
else
{
while(p.getNext()!=null && (p.getTvertex()!=j && p.getDistance()!=weight))
{
q=p;
p =p.getNext();
}
//last node reached but edge not found
if(p.getTvertex()==j && p.getDistance()==weight)
q.setNext(p.getNext());
else
System.out.println("Edge not found");
}
}
}
public void display()
{
for(int i=1; i<=vertices; i++)
{
node temp =list[i];
System.out.println("-----");
while(temp!=null)
{
System.out.println(temp);
temp =temp.getNext();
}
}
}
}