-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure_linked_list_representation_of_graph.c
110 lines (110 loc) · 2.2 KB
/
pure_linked_list_representation_of_graph.c
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node_list
{
int data;
struct node_list* next;
struct edge_list* edge;
}nl;
typedef struct edge_list
{
struct node_list* node;
struct edge_list* next;
}el;
nl* create_node(nl*sn)
{
nl*temp=(nl*)malloc(sizeof(nl));
if(sn==NULL)
{
sn=temp;
}
else
{
nl*p=sn;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
temp->next=NULL;
temp->edge=NULL;
printf("Enter the value to be stored at the node : ");
scanf("%d",&temp->data);
return sn;
}
el* create_edge(el*head,nl*sn)
{
el*temp=(el*)malloc(sizeof(el));
if(head==NULL)
{
head=temp;
}
else
{
el*p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
int val;
printf("Enter the value of node to be connected : ");
scanf("%d",&val);
nl*p=sn;
while(p->data!=val)
{
p=p->next;
}
temp->node=p;
temp->next=NULL;
return head;
}
void display(nl*sn)
{
nl*p=sn;
el*ptr;
printf("NODE\t\tAdjacent nodes\n");
while(p!=NULL)
{
printf("%d\t\t",p->data);
ptr=p->edge;
while(ptr!=NULL)
{
printf("%d , ",ptr->node->data);
ptr=ptr->next;
}
printf("\n");
p=p->next;
}
}
int main()
{
nl*sn=NULL,*p=sn;
int c,n;
do
{
printf("1-Inserting new node \n2Making a new connection\n3-Display\nEnter your choice : ");
scanf("%d",&c);
switch (c)
{
case 1:
sn=create_node(sn);
break;
case 2:
printf("Enter the node from which you want to make an edge : ");
scanf("%d",&n);
p=sn;
while(p->data!=n)
{
p=p->next;
}
p->edge=create_edge(p->edge,sn);
break;
case 3:
display(sn);
}
}while(c>0&&c<4);
return 0;
}