-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.c
89 lines (69 loc) · 2.32 KB
/
sample.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
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <alloca.h>
#include "ll.h"
#define LEN(A) (sizeof(A) / sizeof(A[0]))
enum {ELNUM = 11};
#define listprint(nodeT, l) do { \
nodeT *L = (nodeT *)(l); \
SingleLink_for_all (L) \
printf("%zu ", L->val); \
putchar('\n'); \
} while(0)
#define nxt(node) (void *)((node).nxt)
static void DoubleLink_test(void)
{
typedef struct { DoubleLink lnk; size_t val; } dlnode;
puts("DoubleLink_test()...");
dlnode *head = &(dlnode) {.lnk = DoubleLink_INIT};
DoubleLink *p = &head->lnk;
for (size_t i = 0; i < ELNUM; i++, p = p->nxt) {
dlnode *node = alloca(sizeof(*node));
*node = (dlnode) {DoubleLink_INIT, i};
DoubleLink_insert(p, &node->lnk, &node->lnk);
}
head = nxt(head->lnk);
puts("Original List :");
listprint(dlnode, head);
DoubleLink_reverse(&head->lnk, p);
puts("Reversed :");
listprint(dlnode, p);
DoubleLink_remove(DoubleLink_get(p, 1, false), DoubleLink_get(p, 2, false));
puts("3rd element deleted : ");
listprint(dlnode, p);
DoubleLink *newp = p->nxt;
DoubleLink_swap(p, p, p->nxt, p->nxt);
puts("First and second element swapped :");
listprint(dlnode, newp);
}
static void SingleLink_test(void)
{
typedef struct { SingleLink lnk; size_t val; } slnode;
puts("SingleLink_test()...");
slnode *head = &(slnode) {.lnk = SingleLink_INIT};
SingleLink *p = &head->lnk;
for (size_t i = 0; i < ELNUM; i++, p = p->nxt) {
slnode *node = alloca(sizeof(*node));
*node = (slnode) {SingleLink_INIT, i};
SingleLink_insert(p, &node->lnk, &node->lnk);
}
head = nxt(head->lnk);
puts("Original List :");
listprint(slnode, head);
SingleLink_reverse(NULL, &head->lnk, p);
puts("Reversed :");
listprint(slnode, p);
SingleLink_remove(SingleLink_get(p, 1), SingleLink_get(p, 2));
puts("3rd element deleted : ");
listprint(slnode, p);
SingleLink *newp = p->nxt;
SingleLink_swap(NULL, p, p, p, p->nxt, p->nxt);
puts("First and second element swapped :");
listprint(slnode, newp);
}
int main(void)
{
SingleLink_test();
DoubleLink_test();
}