-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-e06.c
120 lines (88 loc) · 2.87 KB
/
11-e06.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
// functions for doubly linked lists
#include <stdio.h>
struct entry {
int value;
struct entry *next, *prev;
};
int main(void) {
void insertEntry(struct entry * const newEntry, struct entry * const prevEntry);
void removeEntry(struct entry * const entry);
void displayList (struct entry * leader, char dir);
struct entry n0_1, n1, n2, n2_1, n3, n3_1, listStart, listEnd;
listStart.next = &n1;
listStart.prev = (struct entry *) 0;
listEnd.next = (struct entry *) 0;
listEnd.prev = &n3;
n1.value = 100;
n1.next = &n2;
n1.prev = &listStart;
n2.value = 200;
n2.next = &n3;
n2.prev = &n1;
n3.value = 300;
n3.next = &listEnd; // !
n3.prev = &n2;
printf("original list: ");
displayList(listStart.next, 'f'); // original list
n2_1.value = 250;
insertEntry(&n2_1, &n2); // first insertion
printf("\n\nafter 1st insertion: ");
displayList(listStart.next, 'f');
n0_1.value = 50;
insertEntry(&n0_1, &listStart); // second insertion
printf("\nafter 2nd insertion: ");
displayList(listStart.next, 'f');
n3_1.value = 350;
insertEntry(&n3_1, &n3); // third insertion
printf("\nafter 3rd insertion: ");
displayList(listStart.next, 'f');
printf(" backwards: ");
displayList(listEnd.prev, 'b');
removeEntry(&n1); // first deletion
printf("\n\nafter 1st deletion: ");
displayList(listStart.next, 'f');
removeEntry(&n0_1); // second deletion
printf("\nafter 2nd deletion: ");
displayList(listStart.next, 'f');
removeEntry(&n3_1); // third deletion
printf("\nafter 3rd deletion: ");
displayList(listStart.next, 'f');
printf(" backwards: ");
displayList(listEnd.prev, 'b');
printf("\n");
return 0;
}
void insertEntry(struct entry * const newEntry, struct entry * const prevEntry) { // insert newEntry after prevEntry
newEntry->next = prevEntry->next;
newEntry->prev = prevEntry;
prevEntry->next = newEntry;
struct entry *leader = newEntry->next, *temp = newEntry;
while (leader != (struct entry *) 0) {
leader->prev = temp;
temp = leader;
leader = leader->next;
}
}
void removeEntry(struct entry * const entry) { // remove entry
entry->prev->next = entry->next;
entry->next->prev = entry->prev;
}
void displayList (struct entry * leader, char dir) { // display a list
switch (dir) {
case 'f':
while (leader->next != (struct entry *) 0) {
printf("%i ", leader->value);
leader = leader->next;
}
break;
case 'b':
while (leader->prev != (struct entry *) 0) {
printf("%i ", leader->value);
leader = leader->prev;
}
break;
default:
printf("\nwrong direction parameter\n");
break;
}
}