-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular_Linked_List.c
97 lines (91 loc) · 1.91 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
96
97
// 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;
node *position;
void insert(node *list,int e)
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
if(list->next==list)
{
list->next=newnode;
newnode->next=list;
position=newnode;
}
else
{
newnode->next=list;
position->next=newnode;
position=newnode;
}
}
void del(node *list,int e)
{
node *position=list,*tempnode;
while(position->next->element!=e &&position->next->next!=list)
position=position->next;
tempnode=position->next;
position->next=tempnode->next;
free(tempnode);
}
void display(node *list)
{
if(list->next!=list)
{
node *position=list->next;
while(position!=list)
{
printf("%d ",position->element);
position=position->next;
}
}
}
void count(node *list)
{
if(list->next!=list)
{
int count=1;
node *position=list->next;
while(position->next!=list)
{
count++;
position=position->next;
}
printf("\n%d",count);
}
}
int main()
{
int n,e;
node *list=malloc(sizeof(node));
list->next=list;
do
{
scanf("%d ",&n);
switch(n)
{
case 1: scanf("%d ",&e);
insert(list,e);
break;
case 2: scanf("%d",&e);
del(list,e);
break;
case 3: display(list);
break;
case 4: count(list);
break;
case 5: break;
}
}while(n!=5);
}