-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.11.6.c
74 lines (57 loc) · 1.37 KB
/
ex.11.6.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
#include <stdio.h>
struct entry
{
int value;
struct entry *previous;
struct entry *next;
};
void insertBefore (struct entry *insertion, struct entry *next)
{
struct entry *previous = next->previous;
if ( previous != (struct entry *) 0 ) {
previous->next = insertion;
}
next->previous = insertion;
insertion->previous = previous;
insertion->next = next;
}
void insertAfter (struct entry *insertion, struct entry *previous)
{
struct entry *next = previous->next;
if ( next != (struct entry *) 0 ) {
next->previous = insertion;
}
previous->next = insertion;
insertion->previous = previous;
insertion->next = next;
}
void roundTrip (const struct entry *ptr)
{
while ( ptr->next != (struct entry *) 0 ) {
printf ("%i\n", ptr->value);
ptr = ptr->next;
}
while ( ptr->previous != (struct entry *) 0 ) {
printf ("%i\n", ptr->value);
ptr = ptr->previous;
}
printf ("%i\n\n", ptr->value);
}
int main (void)
{
struct entry *nNull = (struct entry *) 0;
struct entry n1 = { .value = 1, .previous = nNull };
struct entry n2 = { .value = 2, .previous = &n1 };
struct entry n3 = { .value = 3, .previous = &n2 };
n1.next = &n2;
n2.next = &n3;
n3.next = nNull;
roundTrip (&n1);
struct entry n9 = { .value = 9 };
insertBefore (&n9, &n1);
roundTrip (&n9);
struct entry n8 = { .value = 8 };
insertAfter (&n8, &n3);
roundTrip (&n9);
return 0;
}