-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_linked_list.c
192 lines (190 loc) · 4.62 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// C PROGRAM TO IMPLEMENT CIRCULAR LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int element;
struct node *next;
};
typedef struct node node;
int isempty(node *list)
{
if(list->next==list)
return 1;
else
return 0;
}
int islast(node *list,node *position)
{
if(position->next==list)
return 1;
else
return 0;
}
node *find(node *list,int e)
{
node *position=list->next;
while(position!=list &&position->element!=e)
position=position->next;
return position;
}
node *findprev(node *list,int e)
{
node *position=list->next;
while(position->next->element!=e)
position=position->next;
return position;
}
node *findnext(node *list,int e)
{
node *position=find(list,e);
return position->next;
}
void insertbeg(node *list,int e)
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
if (isempty(list))
{
newnode->next=list;
list->next=newnode;
}
else
{
newnode->next=list->next;
list->next=newnode;
}
}
void insertmid(node *list,int e,int p)
{
if(isempty(list))
insertbeg(list,e);
else
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
node *position=findprev(list,p);
newnode->next=position->next;
position->next=newnode;
}
}
void insertlast(node *list,int e)
{
if(isempty(list))
insertbeg(list,e);
else
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
node *position=list->next;
while(position->next!=list)
position=position->next;
position->next=newnode;
newnode->next=list;
}
}
void delbeg(node *list)
{
if(isempty(list))
printf("List is empty\n");
else
{
node *tempnode=list->next;
list->next=tempnode->next;
printf("Deleted element:%d\n",tempnode->element);
free(tempnode);
}
}
void delmid(node *list,int p)
{
if(isempty(list))
printf("List is empty\n");
else
{
node *position=findprev(list,p);
node *tempnode=position->next;
position->next=tempnode->next;
printf("Deleted element:%d\n",tempnode->element);
free(tempnode);
}
}
void dellast(node *list)
{
if(isempty(list))
printf("List is empty\n");
else
{
node *position=list->next;
while(position->next->next!=list)
position=position->next;
node *tempnode=position->next;
position->next=list;
printf("Deleted element: %d\n",tempnode->element);
free(tempnode);
}
}
void print(node *list)
{
if(isempty(list))
printf("List is empty\n");
else
{
node *position=list->next;
while(position!=list)
{
printf("%d ",position->element);
position=position->next;
}
printf("\n");
}
}
int main()
{
node *list=malloc(sizeof(node));
list->next=list;
int e,p,n;
printf("\n1.Insert beginning\n2.Insert middle\n3.Insert last\n4.Delete beginning\n5.Delete middle\n6.Delete last\n7.Find\n8.Print\n9.Exit\n");
do
{
printf("Enter your choice: ");
scanf("%d",&n);
switch(n)
{
case 1: printf("Enter element: ");
scanf("%d",&e);
insertbeg(list,e);
break;
case 2: printf("Enter element: ");
scanf("%d",&e);
printf("Enter position: ");
scanf("%d",&p);
insertmid(list,e,p);
break;
case 3: printf("Enter element: ");
scanf("%d",&e);
insertlast(list,e);
break;
case 4: delbeg(list);
break;
case 5: printf("Enter position: ");
scanf("%d",&p);
delmid(list,p);
break;
case 6: dellast(list);
break;
case 7: printf("Enter element: ");
scanf("%d",&e);
if(find(list,e)!=list)
printf("Element is found\n");
else
printf("Element not found\n");
break;
case 8: print(list);
break;
case 9: printf("Exited");
break;
default:printf("Invalid\n");
}
}while(n!=9);
return 0;
}