-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
146 lines (121 loc) · 3.33 KB
/
test.c
File metadata and controls
146 lines (121 loc) · 3.33 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
void data_free(void *d);
void print_list(linked_list *list);
void data_free(void *d)
{
printf("free: %p (%d)\n", d, *(int*)d);
free(d);
}
void print_list(linked_list *list)
{
linked_list_node *node;
if(!list)
{
puts("no list to print");
return;
}
printf(" list size: %d\n", ll_size(list));
printf(" top: %p bottom: %p\n", list->top, list->bottom);
node=list->top;
while(node)
{
printf(" node %p data %p (%d)\n", node, node->data, *(int*)node->data);
node=node->next;
}
}
int main(int argc, char *argv[])
{
ll_error error;
linked_list *list;
int *i, j;
/**************************************************************************/
puts("#### create & delete");
list=ll_new();
if(!list)
puts("failed to create list");
print_list(list);
error=ll_delete(list, data_free);
printf("delete error %d\n", error);
/**************************************************************************/
puts("\n#### create, append & delete");
list=ll_new();
if(!list)
puts("failed to create list");
i=malloc(sizeof(int));
if(!i)
puts("malloc failed");
*i=12;
error=ll_append(list, i);
printf("append error %d\n", error);
print_list(list);
error=ll_delete(list, data_free);
printf("delete error %d\n", error);
/**************************************************************************/
puts("\n#### create, insert & delete");
list=ll_new();
if(!list)
puts("failed to create list");
i=malloc(sizeof(int));
if(!i)
puts("malloc failed");
*i=12;
error=ll_insert(list, i, 0);
printf("insert at 0 error %d\n", error);
print_list(list);
i=malloc(sizeof(int));
if(!i)
puts("malloc failed");
*i=13;
error=ll_insert(list, i, 0);
printf("insert at 0 error %d\n", error);
print_list(list);
i=malloc(sizeof(int));
if(!i)
puts("malloc failed");
*i=14;
error=ll_insert(list, i, 1);
printf("insert at 1 error %d\n", error);
print_list(list);
error=ll_delete(list, data_free);
printf("delete error %d\n", error);
/**************************************************************************/
puts("\n#### queue");
list=ll_new();
if(!list)
puts("failed to create list");
/* insert 10 at start */
for(j=0; j<10; ++j)
{
i=malloc(sizeof(int));
if(!i)
puts("malloc failed");
*i=1+j;
error=ll_insert(list, i, 0);
if(error!=ll_ESUCCESS)
printf("insert %d error %d\n", j, error);
}
/* add one and remove one */
for(j=0; j<10; ++j)
{
i=malloc(sizeof(int));
if(!i)
puts("malloc failed");
*i=10+j;
error=ll_insert(list, i, 0);
if(error!=ll_ESUCCESS)
printf("insert %d error %d\n", j+10, error);
error=ll_remove(list, ll_size(list)-1, (void**)&i);
if(error!=ll_ESUCCESS)
printf("remove %d error %d\n", j, error);
if(*i!=1+j)
printf("data %d\n", *i);
free(i);
}
/* delete remaining */
print_list(list);
error=ll_delete(list, data_free);
printf("delete error %d\n", error);
return 0;
}