view src/list_test.c @ 10:68f85ffc6029

Finished rbtree. Reworked the iterators in list. Minor tweaks to others.
author Eris Caffee <discordia@eldalin.com>
date Fri, 28 Sep 2012 18:24:53 -0500
parents abdba37f67a2
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "list.h"
6 #define MAX 10
8 int main(int argc, char** argv)
9 {
10 struct list * l = list_new();
11 if (NULL == l)
12 {
13 perror("list_new");
14 exit(EXIT_FAILURE);
15 }
18 /////////////////////////////////////
19 // Push and pop from back
20 for (int i = 0; i < MAX ; i++)
21 {
22 int * ip = malloc(sizeof(int));
23 if (NULL == ip)
24 {
25 perror("malloc");
26 exit(EXIT_FAILURE);
27 }
29 *ip = i;
30 if (NULL == list_push_back(l, ip))
31 {
32 perror("list_push_back");
33 exit(EXIT_FAILURE);
34 }
35 }
37 printf("size %zd\n", list_size(l));
38 while (0 != list_size(l))
39 {
40 int * ip = list_pop_back(l);
41 printf("%d\n", *ip);
42 free(ip);
43 }
45 /////////////////////////////////////
46 // Push and pop from front
47 for (int i = 0; i < MAX ; i++)
48 {
49 int * ip = malloc(sizeof(int));
50 if (NULL == ip)
51 {
52 perror("malloc");
53 exit(EXIT_FAILURE);
54 }
56 *ip = i;
57 if (NULL == list_push_front(l, ip))
58 {
59 perror("list_push_front");
60 exit(EXIT_FAILURE);
61 }
62 }
64 printf("size %zd\n", list_size(l));
65 while (0 != list_size(l))
66 {
67 int * ip = list_pop_front(l);
68 printf("%d\n", *ip);
69 free(ip);
70 }
73 /////////////////////////////////////
74 // Push back and pop front
75 for (int i = 0; i < MAX ; i++)
76 {
77 int * ip = malloc(sizeof(int));
78 if (NULL == ip)
79 {
80 perror("malloc");
81 exit(EXIT_FAILURE);
82 }
84 *ip = i;
85 if (NULL == list_push_back(l, ip))
86 {
87 perror("list_push_back");
88 exit(EXIT_FAILURE);
89 }
90 }
92 printf("size %zd\n", list_size(l));
93 while (0 != list_size(l))
94 {
95 int * ip = list_pop_front(l);
96 printf("%d\n", *ip);
97 free(ip);
98 }
100 /////////////////////////////////////
101 // push back, walk front to back
102 for (int i = 0; i < MAX ; i++)
103 {
104 int * ip = malloc(sizeof(int));
105 if (NULL == ip)
106 {
107 perror("malloc");
108 exit(EXIT_FAILURE);
109 }
111 *ip = i;
112 if (NULL == list_push_back(l, ip))
113 {
114 perror("list_push_back");
115 exit(EXIT_FAILURE);
116 }
117 }
119 printf("size %zd\n", list_size(l));
121 struct list_iterator * lit;
122 struct list_iterator * next;
123 int * ip;
125 printf("Walking front to back\n");
126 for (next = lit = list_begin(l); NULL != next; next = list_next(lit))
127 printf("%d\n", *((int *) list_value(lit)));
128 free(lit);
130 printf("Inserting 15 after 5\n");
131 for (next = lit = list_begin(l); NULL != next ; next = list_next(lit))
132 {
133 if (5 == *((int *) list_value(lit)))
134 {
135 int * newip = malloc(sizeof(int));
136 if (NULL == newip)
137 {
138 perror("malloc");
139 exit(EXIT_FAILURE);
140 }
141 *newip = 15;
142 if (NULL == list_insert_after(lit, newip))
143 {
144 perror("malloc");
145 exit(EXIT_FAILURE);
146 }
147 break;
148 }
149 }
150 free(lit);
152 printf("Inserting 20 before 8\n");
153 for (next = lit = list_begin(l); NULL != next ; next = list_next(lit))
154 {
155 if (8 == *((int *) list_value(lit)))
156 {
157 int * newip = malloc(sizeof(int));
158 if (NULL == newip)
159 {
160 perror("malloc");
161 exit(EXIT_FAILURE);
162 }
163 *newip = 20;
164 if (NULL == list_insert_before(lit, newip))
165 {
166 perror("malloc");
167 exit(EXIT_FAILURE);
168 }
169 break;
170 }
171 }
172 free(lit);
174 printf("size %zd\n", list_size(l));
176 printf("Walking back to front\n");
177 for (next = lit = list_end(l); NULL != next ; next = list_prev(lit))
178 printf("%d\n", *((int *) list_value(lit)));
179 free(lit);
181 printf("Erasing front to back\n");
182 lit = list_begin(l);
183 while (NULL != (ip = list_remove(lit)))
184 {
185 printf("%d\n", *ip);
186 free(ip);
187 }
188 free(lit);
189 printf("size %zd\n", list_size(l));
191 list_delete(l);
192 exit(EXIT_SUCCESS);
194 }