-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraph_List.java
More file actions
97 lines (94 loc) · 2.63 KB
/
Graph_List.java
File metadata and controls
97 lines (94 loc) · 2.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
96
97
//created by sidhant
//28/03/2017
//Addition and deletion of edges in vertices with the help of linked list
import java.util.*;
class node
{
int data;
node next;
}
public class Graph_list
{
public static void main (String args[])
{
Scanner in=new Scanner (System.in);
System.out.println("Enter number of vertices");
int n=in.nextInt();
node ar[]=new node[n];
for (int i=0;i<n;i++)
{
ar[i]=new node();
ar[i].data=i+1;
ar[i].next=null;
}
while (true)
{
System.out.println("1 for adding an edge\n2 for deltion of edge\n3 to print\n4 to break");
int d=in.nextInt();
if (d==1)
{
System.out.println("Enter two vertices between 1 to "+n);
int a=in.nextInt();
int b=in.nextInt();
node x=new node();
x.data=b;
x.next=null;
node y=new node();
y.data=a;
y.next=null;
node head1=ar[a-1];
while(head1.next!=null)
{
head1=head1.next;
}
head1.next=x;
node head2=ar[b-1];
while(head2.next!=null)
{
head2=head2.next;
}
head2.next=y;
}
else if (d==3)
{
for (int i=0;i<n;i++)
{
node head=ar[i];
while (head!=null)
{
System.out.print(head.data+" ");
head=head.next;
}
System.out.println();
}
}
else if (d==2)
{
System.out.println("Enter two vertices between 1 to "+n);
int a=in.nextInt();
int b=in.nextInt();
node s;
node head1=ar[a-1];
s=head1;
while(head1.next!=null)
{
head1=head1.next;
if(head1.data==b)
s.next=head1.next;
s=head1;
}
node head2=ar[b-1];
s=head2;
while(head2.next!=null)
{
head2=head2.next;
if(head2.data==a)
s.next=head2.next;
s=head2;
}
}
else
break;
}
}
}