-
Notifications
You must be signed in to change notification settings - Fork 2
/
5_circular-linked-list.c
262 lines (234 loc) · 4.56 KB
/
5_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Linear Circular Linked List
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Define structure student
struct stud{
char Roll_No[9];
char Name[20];
float CGPA;
struct stud*next;
}*save1,*head=NULL,*temp,*save,*save2;
// Insert at the end of the linked list
void ins_last()
{
char roll[9],name[20];
float cg;
// Get new student data
printf("Enter Student Roll no.:\n");
scanf("%s",roll);
printf("Enter Student Name:\n");
scanf("%s",name);
printf("Enter Student CGPA:\n");
scanf("%f",&cg);
temp=(struct stud*)malloc(sizeof(struct stud));
strcpy(temp->Roll_No,roll);
strcpy(temp->Name,name);
temp->CGPA=cg;
temp->next=head;
// When list is empty
if(head==NULL)
head=temp;
// When list exists
else
{ temp->next=head;
save=head;
do
{
save=save->next;
} while(save->next!=head);
save->next=temp;
}
}
// Insert into the beginning of the linked list
void ins_beg()
{
char roll[9],name[20];
float cg;
// Get new student data
printf("Enter Student Roll no.:\n");
scanf("%s",roll);
printf("Enter Student Name:\n");
scanf("%s",name);
printf("Enter Student CGPA:\n");
scanf("%f",&cg);
temp=(struct stud*)malloc(sizeof(struct stud));
strcpy(temp->Roll_No,roll);
strcpy(temp->Name,name);
temp->CGPA=cg;
temp->next=NULL;
// If list is empty
if(head==NULL)
{
head=temp;
head->next=head;
}
// If list exists
else
{
save=head;
head=temp;
temp->next=save;
save1=save;
do
{
save1=save1->next;
} while(save1->next!=save);
save1->next=temp;
}
}
// Insert in a specified position
void ins_pos()
{
int pos;
char roll[9],name[20];
float cg;
// Get new student data
printf("Enter Student Roll no.:\n");
scanf("%s",roll);
printf("Enter Student Name:\n");
scanf("%s",name);
printf("Enter Student CGPA:\n");
scanf("%f",&cg);
printf("Enter Position:\n");
scanf("%d",&pos);
temp=(struct stud*)malloc(sizeof(struct stud));
strcpy(temp->Roll_No,roll);
strcpy(temp->Name,name);
temp->CGPA=cg;
temp->next=NULL;
// If list is empty
if(head==NULL)
head=temp;
// If list exists
else
{ save=head;
pos-=1;
while(--pos)
save=save->next;
save2=save->next;
save->next=temp;
temp->next=save2;
}
}
// Delete first element of the linked list
void del_beg()
{
if(head->next==head)
head=NULL;
if(head==NULL)
printf("Underflow\n");
else
{
save=head;
do
{
save=save->next;
} while(save->next!=head);
save->next=save->next->next;
head=head->next;
}
}
// Delete element in the given position
void del_pos()
{
int pos;
printf("Enter Position:\n");
scanf("%d",&pos);
if(head==NULL)
printf("Underflow\n");
else
{
save=head;
pos--;
while(--pos)
save=save->next;
save->next=save->next->next;
}
}
// Delete last element in linked list
void del_end()
{
if(head==NULL)
printf("Underflow\n");
else
{
temp=head;
while(temp->next->next!=head)
temp=temp->next;
temp->next=head;
}
}
// Display
void disp()
{
temp=head;
do
{
printf("\n\nStudent Roll no.:%s\n",temp->Roll_No);
printf("Student Name:%s\n",temp->Name);
printf("Student CGPA:%f\n",temp->CGPA);
temp=temp->next;
} while(temp!=head);
}
// Search for an element in the list
void search()
{
char s[100];
int flag=0;
printf("Enter Roll no:\n");
scanf("%s",s);
temp=head;
do
{
if(!strcmp(s,temp->Roll_No))
{
flag=1;
save=temp;
break;
}
else
flag=0;
temp=temp->next;
} while(temp!=head);
if(flag==1)
{
printf("\n\nStudent Roll no.:%s\n",save->Roll_No);
printf("Student Name:%s\n",save->Name);
printf("Student CGPA:%f\n",save->CGPA);
}
else
printf("Not Found\n");
}
// Driver function
int main()
{
int choice=0;
while(choice!=9)
{
printf("1.Insert Beginning\n");
printf("2.Insert Between\n");
printf("3.Insert End\n");
printf("4.Delete Beginning\n");
printf("5.Delete between\n");
printf("6.Delete End\n");
printf("7.Search\n");
printf("8.Display\n");
//printf("4.Peek\n");
printf("9.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:ins_beg();break;
case 2:ins_pos();break;
case 3:ins_last();break;
case 4:del_beg();break;
case 5:del_pos();break;
case 6:del_end();break;
case 7:search();break;
case 8:disp();break;
//case 9:exit(0);break;
}
}
return 0;
}