-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_LinkedList.c
More file actions
307 lines (216 loc) · 5.19 KB
/
Copy pathCircular_LinkedList.c
File metadata and controls
307 lines (216 loc) · 5.19 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//Circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
//NODE CREATION
struct node* create_node(int val){
static struct node*temp;
static struct node*head=NULL;
//if the list is empty
if (head==NULL){
//allocate memory for the head
head=(struct node*)malloc(sizeof(struct node));
//temp and head are the same
temp=head;
//add a value to first half of head
temp->data=val;
//initialize the second half of head to itself
temp->next=NULL;
}
else{
//allocate memory for temp
temp->next=(struct node*)malloc(sizeof(struct node));
//move the pointer to the next location
temp=temp->next;
//add value to the first part of temp;
temp->data=val;
//initialize the second half of temp to NULL
temp->next=NULL;
}
//establish the looping
temp->next=head;
return head;
}
//DISPLAYING THE CIRCULAR LINKED LIST
void display(struct node*head){
struct node*temp;
int loops=0;
for (temp=head;loops<3;temp=temp->next){
printf("%d ->", temp->data);
if (temp->next == head){
printf(" ");
loops++;
}
}
}
//INSERTION AT THE BEGINNING/ENDING OF THE LIST
struct node* insert_at_beg(struct node*head ,int val){
struct node*temp=head;
//allocate memory
struct node*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
//put in values
newnode->data=val;
//make newnode point to head
newnode->next=head;
//iterate until the node just before the head node
for (temp=head->next;temp->next!=head;temp=temp->next);
//after reaching the penultimate node, make that point to newnode
temp->next=newnode;
//reallocate head
head=newnode;
//return the head
return head;
}
//INSERTION IN THE MIDDLE
struct node* insert_at_pos(struct node*head, int val, int pos){
struct node*temp;
struct node* newnode;
int i;
temp=head;
for (i=0;i<pos-1;i++){
if (temp->next!=NULL){
temp=temp->next;
}
}
//assign values to newnode
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=temp->next;
//make temp pointer point to newnode
temp->next=newnode;
return head;
}
//DELETION AT THE BEGINNING/ENDING OF THE LIST
struct node* delete_at_beg(struct node* head){
struct node*temp=head;
//mark node to be deleted
struct node*delnode=head;
//move the head pointer to the next pos
head=head->next;
//iterate around till the next node is the one to be deleted
for (temp=head; temp->next!=delnode;temp=temp->next);
//point temp to the new head
temp->next=head;
//free memory
free(delnode);
return head;
}
//DELETION IN THE MIDDLE
struct node* delete_at_pos(struct node*head, int pos){
int i=0;
struct node*temp=head;
struct node*remove=NULL;
for (i=0;i<pos-1;i++){
temp=temp->next;
}
remove=temp->next; //remove is the node to be deleted
temp->next=temp->next->next;
free(remove);
return head;
}
//SEARCHING FOR A NODE
void search_node(struct node*head, int val){
struct node *temp;
int i=1;
int flag=0;
temp=head;
if (temp->data==val){
flag=1;
temp=temp->next;
}
else{
temp=temp->next;
while (temp!=head){
if (temp->data==val){
flag=1;
}
temp=temp->next;
}
}
if (flag==0){
printf("\nElement not found");
}
else{
printf("\nElement found");
}
}
void main(){
static struct node*head=NULL;
int status=0;
int num;
int ch;
int updated_head;
int pos;
int ele;
int search;
while (status==0){
printf("1: Creation of a node\n2: Add node at the beginning/end\n3: Insert in the middle of list\n4: Delete at the beginning/end\n5: Delete in the middle of the list\n6: Display circular linked list\n7: Search for an element");
printf("\nEnter your choice:");
scanf("%d", &ch);
if (ch==1){
printf("Creation of a node");
printf("\nEnter an element");
scanf("%d", &num);
head=create_node(num);
display(head);
printf("\n");
}
else if (ch==2){
printf("\nAddition of a node at the beginning/end");
printf("\nEnter the node to insert at the beginning");
scanf("%d",&updated_head);
head=insert_at_beg(head,updated_head);
display(head);
printf("\n");
}
else if (ch==3){
printf("\nInsert in the middle of the list");
printf("\nEnter the element to insert");
scanf("%d",&ele);
printf("Enter the position to insert the element");
scanf("%d",&pos);
head=insert_at_pos(head,ele,pos);
display(head);
printf("\n");
}
else if (ch==4){
printf("\nDeletion at the beginning/end");
head=delete_at_beg(head);
display(head);
printf("\n");
}
else if (ch==5){
printf("\nDeletion in the middle");
printf("\nEnter the position to delete the element from");
scanf("%d",&pos);
head=delete_at_pos(head,pos);
printf("\n");
display(head);
printf("\n");
}
else if (ch==6){
if (head==NULL){
printf("\nNo elements in the list as of now");
}
else{
printf("Displaying the circular linked list");
display(head);
}
}
else if (ch==7){
printf("\nEnter the searching element");
scanf("%d",&search);
search_node(head, search);
}
else{
printf("Invalid Choice");
}
printf("\n");
printf("\nEnter 0 to continue operations");
scanf("%d", &status);
}
}