-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_Insertion_Deletion.cpp
274 lines (230 loc) · 6.09 KB
/
Circular_Insertion_Deletion.cpp
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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
struct Node *InsertAtFirst(struct Node *head)
{
struct Node *temp = head;
int data;
printf("Enter the data you want to insert in list\n");
scanf("%d", &data);
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
while(temp->next!=head) {
temp=temp->next;
}
temp->next = ptr;
ptr->next = head;
head = ptr;
return head;
}
struct Node *InsertAtIndex(struct Node *head,int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
struct Node *temp;
temp = head;
int index, i = 0;
// printf("Enter the data you want to insert in list\n");
// scanf("%d", &data);
printf("enter the index you want to insert data in\n");
scanf("%d", &index);
do{
temp = temp->next;
i++;
}while (i != index - 1);
ptr->next = temp->next;
temp->next = ptr;
return head;
}
struct Node *InsertAtEnd(struct Node *head, int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
struct Node *temp;
temp = head->next;
while (temp->next != head)
{
temp = temp->next;
}
ptr->next = temp->next;
temp->next = ptr;
ptr->next = head;
return head;
}
struct Node *DeletionAtFirst(struct Node *head)
{
struct Node *ptr = head;
struct Node * temp;
temp=head->next;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=head->next;
head=ptr;
head=head->next;
free(ptr);
return head;
}
struct Node *DeletionAtIndex(struct Node *head)
{
struct Node *ptr = head;
struct Node *q = head->next;
int i, index;
printf("Enter the index at which you want to delete the node\n");
scanf("%d", &index);
for (i = 0; i < index - 1; i++)
{
ptr = ptr->next;
q = q->next;
}
ptr->next = q->next;
free(q);
return head;
}
struct Node *DeletionAtValue(struct Node *head)
{
struct Node *ptr = head;
struct Node *q = head->next;
int value;
printf("Enter the value you want to delete the node\n");
scanf("%d", &value);
while (q->data != value)
{
q = q->next;
}
if (q->data != value)
{
printf("NO such value found\n");
}
else
{
ptr->next = q->next;
free(q);
}
return head;
}
struct Node *DeletionAtEnd(struct Node *head)
{
struct Node *p = head;
struct Node *q = head->next;
while (q->next != head)
{
q = q->next;
p = p->next;
}
p->next = head;
free(q);
return head;
}
void PrintList()
{
struct Node *temp;
temp = head;
int i=0;
do
{
printf("the value at index %d is %d\n",i, temp->data);
temp = temp->next;
i++;
} while (temp!=head);
printf("\n");
}
void CreateList()
{
struct Node *temp;
int choice = 1;
do {
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
printf("enter the value in node : \n");
int x;
scanf("%d", &x);
new_node->data = x;
if (head == NULL)
{
head = temp = new_node;
}
else
{
temp->next = new_node;
temp = temp->next;
}
temp->next = head;
printf("Do you want to enter another value (1/0) : \n");
scanf("%d", &choice);
} while(choice!=0);
}
int main()
{
int data, n=1;
printf("MENU\n");
printf("1.to create List\n");
printf("2.to display the list\n");
printf("3.to Insert data at first\n");
printf("4.to Insert data at index\n");
printf("5.to Insert data at end\n");
printf("6.to delete node at first\n");
printf("7.to delete Node at given index\n");
printf("8.to delete Node at given value\n");
printf("9.to delete Node at end\n");
printf("0.Exit\n");
while(1)
{
printf("Enter the 'n' to get your choice : ");
scanf("%d", &n);
// int choice = 1;
// struct Node *temp = head;
switch (n)
{
case 1:
CreateList();
break;
case 2:
printf("Circular List is :\n");
PrintList();
break;
case 3:
head = InsertAtFirst(head);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 4:
printf("Enter the data you want to insert in list\n");
scanf("%d", &data);
head = InsertAtIndex(head,data);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 5:
printf("Enter the data you want to insert in list\n");
scanf("%d", &data);
head = InsertAtEnd(head, data);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 6:
head = DeletionAtFirst(head);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 7:
head = DeletionAtIndex(head);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 8:
head = DeletionAtValue(head);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 9:
head = DeletionAtEnd(head);
printf("LIST UPDATED SUCCESFULLY\n");
break;
case 0:
printf("exiting the program.....\n"); //you can press 0 to exit the given program
return 0;
default:
printf("INVALID CHOICE, TRY AGAIN\n");
}
}
}