-
Notifications
You must be signed in to change notification settings - Fork 3
/
linkedlist.c
358 lines (314 loc) · 6.79 KB
/
linkedlist.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct NODE *next;
}*node;
typedef struct STACK
{
node n;
}stack;
typedef struct QUEUE
{
node enter;
node exit;
}queue;
void getnode(node *n);
void display(node n);
void insert(node *n, int pos, int x);
void insertfront(node *n, int x);
void insertend(node *n, int x);
int delete(node *n, int pos);
int deletevalue(node *n, int x);
int basic_main();
void push(stack *a, int d);
int pop(stack *a);
int stack_main();
void enqueue(queue *q, int d);
int dequeue(queue *q);
void dispQueue(queue q);
int queue_main();
int main()
{
//choice allows you to choose if you want to implement a stack or queue or just use the linked list
//basic_main(), stack_main() etc. can also be used as the main function if you want just one implementations
//to do that rename those functions to main() and delete the current main function
int choice;
printf("1 - basic, 2 - stack, 3 - circ queue\n");
scanf("%d", &choice);
if (choice == 1)
{
return basic_main();
}
else if (choice == 2)
{
return stack_main();
}
else if (choice == 3)
{
return queue_main();
}
else
{
return 0;
}
}
void getnode(node *n)
{
*n = (struct NODE *)malloc(sizeof(struct NODE));
}
void insert(node *n, int pos, int x)
{
//if position is neg or exceeds the num of elements in list, insert end
while(*n != NULL && pos != 0)
{
n = &((*n)->next);
pos--;
}
node temp = *n;
getnode(n);
(*n)->data = x;
(*n)->next = temp;
}
void insertfront(node *n, int x)
{
node temp = *n;
getnode(n);
(*n)->data = x;
(*n)->next = temp;
}
void insertend(node *n, int x)
{
while(*n != NULL)
n = &((*n)->next);
getnode(n);
(*n)->data = x;
(*n)->next = NULL;
}
void display(node n)
{
//this prints a message for empty linked-list
if(n == NULL)
{
printf("no elements present");
}
while(n != NULL)
{
printf("-> %d ", n->data);
n = n->next;
}
printf("\n");
}
int delete(node *n, int pos)
{
//this checks for list empty condition
if(*n == NULL)
{
//the list is empty, cant delete
return -1;
}
//if position is neg or exceeds the num of elements in list, delete end
while ((*n)->next != NULL && pos != 0)
{
n = &((*n)->next);
pos--;
}
node temp = *n;
int temp2 = temp->data;
*n = (*n)->next;
free(temp);
return temp2;
}
int deletevalue(node *n, int x)
{
int count = 0;
while(*n != NULL)
{
if ((*n)->data == x)
{
node temp = *n;
*n = (*n)->next;
free(temp);
count++;
}
else
{
n = &((*n)->next);
}
}
return count;
}
int basic_main()
{
//this is an implementation of basic linked list
//currently, for insertion and deletion, the program asks the user for the position
node a = NULL;
while (1)
{
int n;
printf("1 - insert, 2 - display, 3 - delete: ");
scanf("%d", &n);
if (n == 1)
{
int d;
printf("Enter data: ");
scanf("%d", &d);
//use the following to insert at the front
//insertfront(&a, d);
//use the following to insert at the end
//insertback(&a, d);
//use the following to insert in user input position
//for insert at front input pos = 0
//for end input pos = negative (or pos exceeds number of elements in the list)
int pos;
printf("Enter position: ");
scanf("%d", &pos);
insert(&a, pos, d);
}
else if (n == 2)
{
display(a);
}
else if (n == 3)
{
//use the following to delete user input value(s) from the list
/*int d;
printf("Enter int: ");
scanf("%d", &d);
printf("%d elements deleted\n", deletevalue(&a, d));*/
//use the following to delete from user input position
//for delete at front input pos = 0
//for end input pos = negative (or pos exceeds number of elements in the list)
int pos;
printf("Enter position: ");
scanf("%d", &pos);
delete(&a, pos);
}
else
{
return 0;
}
}
}
void push(stack *a, int d)
{
insert(&(a->n), 0, d);
}
int pop(stack *a)
{
return delete(&(a->n), 0);
}
int stack_main()
{
//this is an implementation of stack using linked-lists
stack a;
a.n = NULL;
while (1)
{
int n;
printf("1 - push, 2 - display, 3 - pop: ");
scanf("%d", &n);
if (n == 1)
{
int d;
printf("Enter data: ");
scanf("%d", &d);
push(&a, d);
}
else if (n == 2)
{
display(a.n);
}
else if (n == 3)
{
printf("Popped: %d\n", pop(&a));
}
else
{
return 0;
}
}
}
void enqueue(queue *q, int d)
{
if (q->enter == NULL)
{
getnode(&(q->enter));
q->enter->next = q->enter;
q->exit = q->enter->next;
q->enter->data = d;
}
else
{
insert(&(q->enter), 1, d);
q->enter = q->enter->next;
}
}
int dequeue(queue *q)
{
if (q->enter == NULL)
{
return -1;
}
else if (q->enter == q->exit)
{
int temp = q->enter->data;
free(q->enter);
q->enter = NULL;
q->exit = NULL;
return temp;
}
else
{
q->exit = q->exit->next;
return delete(&(q->enter), 1);
}
}
void dispQueue(queue q)
{
//this prints q message for empty queue
if(q.enter == NULL)
{
printf("no elements present");
}
while(q.exit != q.enter)
{
printf("%d <- ", q.exit->data);
q.exit = q.exit->next;
}
printf("%d", q.enter->data);
printf("\n");
}
int queue_main()
{
//this is an implementation of circular queue
queue q;
q.enter = NULL;
q.exit = NULL;
while (1)
{
int n;
printf("1 - enqueue, 2 - display, 3 - dequeue: ");
scanf("%d", &n);
if (n == 1)
{
int d;
printf("Enter data : ");
scanf("%d", &d);
enqueue(&q, d);
}
else if (n == 2)
{
dispQueue(q);
}
else if (n == 3)
{
printf("Dequeued: %d\n", dequeue(&q));
}
else
{
return 0;
}
}
}