-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadjList.java
More file actions
123 lines (109 loc) · 2.12 KB
/
adjList.java
File metadata and controls
123 lines (109 loc) · 2.12 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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 j, int weight, int traffic) //oVertex, tVertex, distance, traffic
{
node p;
p=list[i];
node temp = new node(weight,j);
temp.setTraffic(traffic);
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);
temp1.setTraffic(traffic);
if(p1==null)
list[j]=temp1;
else
{
while(p1.getNext()!=null)
p1=p1.getNext();
p1.setNext(temp1);
}
}
public void setTraffic(int i, int j, int traffic)
{
node p =list[i];
node p1 =list[j];
try
{
while(p.getTvertex()!=j)
{
p=p.getNext();
}
p.setTraffic(traffic);
while(p1.getTvertex()!=i)
{
p1=p1.getNext();
}
p1.setTraffic(traffic);
}
catch(Exception e)
{
System.out.println(e);
}
}
public int vertexCount()
{
return vertices+1;
}
public void deleteEdge(int i, int j, int weight)
{
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();
}
}
}
}