-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_linked_list.c
95 lines (89 loc) · 1.71 KB
/
circular_linked_list.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
// Write a Program to implement Circular Linked List which performs the following operations based on the users input.
// 1 -> Insertion
// 2 -> Deletion
// 3 -> Display
// 4 -> Find the Number of Elements
// 5 -> EXIT
#include <stdio.h>
#include <stdlib.h>
struct node
{
int element;
struct node *next;
};
typedef struct node node;
void insert(node *list, int x)
{
node *newnode = malloc(sizeof(node));
newnode->element = x;
newnode->next = list;
node *pos = list;
while (pos->next != list)
{
pos = pos->next;
}
pos->next = newnode;
}
void delete (node *list, int x)
{
node *pos = list;
while (pos->next != list && pos->next->element != x)
{
pos = pos->next;
}
node *tempnode = pos->next;
pos->next = tempnode->next;
free(tempnode);
}
void findnoofelements(node *list)
{
node *pos = list;
int c = 0;
while (pos->next != list)
{
pos = pos->next;
c++;
}
printf("%d\n", c);
}
void traverse(node *list)
{
node *pos = list;
while (pos->next != list)
{
pos = pos->next;
printf("%d ", pos->element);
}
printf("\n");
}
int main()
{
int ch;
node *list = malloc(sizeof(node));
list->next = list;
int x;
do
{
scanf("%d", &ch);
switch (ch)
{
case 1:
scanf("%d", &x);
insert(list, x);
break;
case 2:
scanf("%d", &x);
delete (list, x);
break;
case 3:
traverse(list);
break;
case 4:
findnoofelements(list);
break;
default:
break;
}
} while (ch < 5);
return 0;
}