forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphll.java
More file actions
52 lines (44 loc) · 963 Bytes
/
Copy pathgraphll.java
File metadata and controls
52 lines (44 loc) · 963 Bytes
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
import java.util.*;
class graph
{
int v;
LinkedList<Integer> adjli[];
graph(int v)
{
this.v=v;
adjli=new LinkedList[v];
for(int i=0;i<v;i++)
{
adjli[i]=new LinkedList<>();
}
}
void addEdge(int a,int b)
{
adjli[a].add(b);
adjli[b].add(a); //because undirected graph
}
void print()
{
for(int i=0;i<v;i++)
{
System.out.println("Adjacency List of vertes:"+i);
for(Integer n:adjli[i])
{
System.out.print(n+" ");
}
System.out.println();
}
}
}
class graphll
{
public static void main(String[] args) {
graph g=new graph(5);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,3);
g.addEdge(3,4);
g.addEdge(4,2);
g.print();
}
}