-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLRep.C
100 lines (100 loc) · 2.33 KB
/
LLRep.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
#include <stdio.h>
#include <stdlib.h>
struct adj
{
int value;
struct adj *next;
};
struct vertex
{
int value;
struct adj *head;
struct vertex *next;
};
struct graph
{
struct vertex *vhead;
} obj;
struct adj *create_adj(struct adj *head, int value)
{
struct adj *new_node = (struct adj *)malloc(sizeof(struct adj));
new_node->value = value;
new_node->next = NULL;
if (head == NULL)
{
head = new_node;
return head;
}
struct adj *k = head;
while (k->next != NULL)
{
k = k->next;
}
k->next = new_node;
return head;
}
struct vertex *create_ver(struct vertex *head, int value)
{
struct vertex *new_node = (struct vertex *)malloc(sizeof(struct vertex));
new_node->value = value;
new_node->next = NULL;
if (head == NULL)
{
head = new_node;
return head;
}
struct vertex *k = head;
while (k->next != NULL)
{
k = k->next;
}
k->next = new_node;
return head;
}
void printlist(struct vertex *hea)
{
printf("\nThe Linked List representation is as follows:\n");
while (hea != NULL)
{
printf("%d->", hea->value);
while (hea->head != NULL)
{
printf("%d->", hea->head->value);
hea->head = hea->head->next;
}
printf("NULL");
hea = hea->next;
printf("\n");
}
}
int main()
{
int i, numver, value, n;
obj.vhead = NULL;
printf("----------LINKED LIST IMPLEMENTATION OF GRAPH----------");
printf("\nEnter number of vertices:\t");
scanf("%d", &numver);
for (i = 0; i < numver; i++)
{
printf("Enter the value of Vertex %d:\t", i + 1);
scanf("%d", &value);
obj.vhead = create_ver(obj.vhead, value);
}
struct vertex *j = obj.vhead;
printf("\nEnter the adjecents of vertices:\n");
while (j != NULL)
{
j->head = NULL;
printf("\nEnter the number of adjecents of vertex %d:\t", j->value);
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter adjecent %d:\t", i + 1);
scanf("%d", &value);
j->head = create_adj(j->head, value);
}
j = j->next;
}
printlist(obj.vhead);
return 0;
}