forked from golumall/Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List_Representation.java
More file actions
50 lines (49 loc) · 952 Bytes
/
Linked_List_Representation.java
File metadata and controls
50 lines (49 loc) · 952 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
import java.util.LinkedList;
import java.util.Scanner;
class Linked_List_Represent
{
public static class Graph
{
int v;
LinkedList<Integer> adjlist[];
public Graph(int v)
{
this.v=v;
adjlist=new LinkedList[v];
for(int i=0;i<v;i++)
{
adjlist[i]=new LinkedList<>();
}
}
}
static void addEdge(Graph graph,int src,int dest)
{
graph.adjlist[src].addFirst(dest);
graph.adjlist[dest].addFirst(src);
}
static void printGraph(Graph graph)
{
for(int i=0;i<graph.v;i++)
{
for(Integer gp:graph.adjlist[i])
{
System.out.print("->"+gp);
}
System.out.println();
}
}
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int e=sc.nextInt();
Graph graph=new Graph(v);
for(int i=1;i<=e;i++)
{
int src=sc.nextInt();
int dest=sc.nextInt();
addEdge(graph,src,dest);
}
printGraph(graph);
}
}