-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleLinked.c
331 lines (295 loc) · 6.79 KB
/
doubleLinked.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void addv(int newData);
void addi(int newData, int index);
void delv(int oldData);
void deli(int index);
void printList(void);
void revs();
void addi(int newData, int index);
void linking(int newData);
typedef char STRING[10];
struct LIST
{
int value;
struct LIST * next;
struct LIST * prev;
};
struct LIST *headNode = NULL;
struct LIST *tailNode = NULL;
int main(void)
{
STRING instruction;
int userNum;
int data;
int index;
int newData;
int i = 0;
printf("Size of list: ");
scanf("%d", &userNum);
clock_t tic = clock();
for (i = 0; i < userNum; i++)
linking(i);
clock_t toc = clock();
printf("Operation time: %.20f\n", (double)(toc - tic) / (CLOCKS_PER_SEC));
//printList();
printf("Feed the precious instructions\n");
scanf("%s", instruction);
while (strcmp(instruction, "end") != 0)
{
if (strcmp(instruction, "addv") == 0)
{
scanf("%d", &data);
clock_t debut = clock();
addv(data);
clock_t fin = clock();
printf("Operation time: %.20f\n", (double)(fin - debut) / (CLOCKS_PER_SEC));
//printList();
}
else if (strcmp(instruction, "addi") == 0)
{
scanf("%d %d", &newData, &index);
addi(newData, index);
//printList();
}
else if (strcmp(instruction, "delv") == 0)
{
scanf("%d", &newData);
clock_t tip = clock();
delv(newData);
clock_t top = clock();
printf("Operation time: %.20f\n", (double)(top - tip) / (CLOCKS_PER_SEC));
//printList();
}
else if (strcmp(instruction, "deli") == 0)
{
scanf("%d", &index);
clock_t begin = clock();
deli(index);
clock_t end = clock();
printf("Operation time: %.20f\n", (double)(end - begin) / (CLOCKS_PER_SEC));
//printList();
}
else if (strcmp(instruction, "revs") == 0)
{
clock_t now = clock();
revs();
clock_t then = clock();
printf("Operation time: %.20f\n", (double)(then - now) / (CLOCKS_PER_SEC));
//printList();
}
else
printList();
scanf("%s", instruction);
}
return 0;
}
void addv(int newData)
{
struct LIST *newNode, *tempNode;
newNode = (struct LIST*)malloc(sizeof(struct LIST));
newNode->value = newData;
newNode->next = NULL;
if (headNode == NULL)
{
headNode = newNode;
headNode->prev = NULL;
}
else
{
tempNode = headNode;
while (tempNode->next != NULL)
{
//tempNode->prev = tempNode; //following node pointed to by tempNode-> prev
tempNode = tempNode->next;
}
tempNode->next = newNode;
newNode->prev = tempNode;
newNode->next = NULL;
}
return;
}
void addi(int newData, int index)
{
struct LIST *newNode, *tempNode, *next;
int i = 0;
newNode = (struct LIST*)malloc(sizeof(struct LIST));
newNode->value = newData;
newNode->next = NULL;
if (headNode == NULL)
{
headNode = newNode;
headNode->prev = NULL;
}
else
{
tempNode = headNode;
while (i != (index-1) && tempNode->next != NULL)
{
tempNode = tempNode->next;
i++;
}
if (i == (index-1) && tempNode->next!=NULL)
{
newNode->next = tempNode->next;
next = tempNode->next;
tempNode->next = newNode;
newNode->prev = tempNode;
next->prev = newNode;
}
else
{
tempNode->next = newNode;
newNode->prev = tempNode;
}
}
return;
}
void printList(void)
{
struct LIST *tempNode, *last;
printf("Printing...\n");
if (headNode != NULL)
{
tempNode = headNode;
while (tempNode != NULL)
{
printf("%d ", tempNode->value);
last = tempNode;
tempNode = tempNode->next;
}
printf("\n");
while (last != NULL)
{
printf("%d ", last->value);
last = last->prev;
}
printf("\n");
}
}
void delv(int oldData)
{
struct LIST *tempNode, *shadowNode = NULL, *next;
int i = 0;
if (headNode != NULL)
{
tempNode = headNode;
while (tempNode->value != oldData && tempNode->next != NULL)
{
shadowNode = tempNode;
tempNode = tempNode->next;
//i++;
}
if (tempNode->value == oldData && shadowNode != NULL && tempNode->next != NULL)
{
printf("Deleted: %d\n", tempNode->value);
free(tempNode);
shadowNode = tempNode->prev;
shadowNode->next = tempNode->next;
next = tempNode->next;
next->prev = shadowNode;
tempNode = NULL;
}
else if (tempNode->value == oldData && tempNode->next == NULL )
{
//printf("Else if\n");
printf("Deleted: %d\n", tempNode->value);
free(tempNode);
shadowNode = tempNode->prev;
tempNode = NULL;
shadowNode->next = NULL;
/*next = tempNode->next;
next->prev = shadowNode;
tempNode = NULL;*/
}
else if (shadowNode == NULL)
{
free(headNode);
headNode = tempNode->next;
headNode->prev = NULL;
tempNode = NULL;
}
}
return;
}
void deli(int index)
{
struct LIST *tempNode, *shadowNode = NULL, *next;
int i = 0;
if (headNode != NULL)
{
tempNode = headNode;
while (i != index && tempNode->next != NULL)
{
shadowNode = tempNode;
tempNode = tempNode->next;
i++;
}
if (i == (index) && shadowNode != NULL && tempNode->next != NULL)
{
free(tempNode);
shadowNode = tempNode->prev;
shadowNode->next = tempNode->next;
next = tempNode->next;
next->prev = shadowNode;
tempNode = NULL;
}
else if (i == index && tempNode->next == NULL)
{
//printf("Else if\n");
free(tempNode);
shadowNode = tempNode->prev;
tempNode = NULL;
shadowNode->next = NULL;
/*next = tempNode->next;
next->prev = shadowNode;
tempNode = NULL;*/
}
else if (shadowNode == NULL)
{
free(headNode);
headNode = tempNode->next;
headNode->prev = NULL;
tempNode = NULL;
}
}
}
void revs()
{
struct LIST *prev = NULL; // Previous pointer set to NULL (before head)
struct LIST *current = headNode; //Current Node initialized to head Node
struct LIST *next = NULL; //Next node
while (current != NULL) // Iterating until current Node is at the end of list
{
prev = current->prev;
next = current->next; //store address of next node
current->next = prev; // Next field in node points at previous node (reverse)
current->prev = next;
//prev = current; //move prev to following node
current = next; //move current to next node
}
headNode = prev->prev; //Head points to first node in reserved list
}
void linking(int newData)
{
struct LIST *newNode, *tempNode, *next;
newNode = (struct LIST*)malloc(sizeof(struct LIST));
newNode->value = newData;
newNode->next = NULL;
if (headNode == NULL)
{
headNode = newNode;
tailNode = newNode;
tailNode->prev = NULL;
tailNode->next = NULL;
}
else
{
tailNode->next = newNode;
newNode->prev = tailNode;
tailNode = newNode;
tailNode->next = NULL;
}
return;
}