view src/main.c @ 4:c9e6f5c0a9b2

Added copyright notices, COPYING and README files.
author Eris Caffee <discordia@eldalin.com>
date Fri, 22 Jul 2011 20:45:30 -0500
parents c6e339846527
children
line source
1 /*******************************************************************************
2 * LinkedList
3 *
4 * A simple doubly linked list class in C.
5 *
6 *******************************************************************************
7 *
8 * Copyright (C) 2010 Sarah Eris Horsley Caffee
9 *
10 * This is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 *******************************************************************************
24 *
25 * Test driver for LinkedList
26 *
27 ******************************************************************************/
29 #include "LinkedList.h"
31 #include <stdio.h>
32 #include <stdlib.h>
35 typedef struct mode_info mode_info;
36 struct mode_info
37 {
38 int sizeid;
39 int w;
40 int h;
41 int r;
42 };
44 int main (int argc, char ** argv)
45 {
46 LinkedList list1;
47 LinkedList list2;
48 LinkedList * list3;
50 int * p_int;
51 mode_info * p_mode;
53 int i;
54 int size;
57 ll_init(&list1);
58 ll_init(&list2);
60 for (i=0; i<10; ++i)
61 {
62 p_int = malloc(sizeof(int));
63 *p_int = i;
64 list1.push(&list1, p_int);
65 }
66 p_int = malloc(sizeof(int));
67 *p_int = 17;
68 list1.push(&list1, p_int);
70 list1.head(&list1);
71 list1.next(&list1);
72 p_int = malloc(sizeof(int));
73 *p_int = 23;
74 list1.insert(&list1, p_int);
76 list1.tail(&list1);
77 p_int = malloc(sizeof(int));
78 *p_int = 109;
79 list1.insert(&list1, p_int);
81 list1.head(&list1);
82 list1.next(&list1);
83 list1.next(&list1);
84 list1.next(&list1);
85 list1.next(&list1);
86 list1.next(&list1);
87 free(list1.remove(&list1));
89 list1.head(&list1);
90 free(list1.remove(&list1));
92 list1.tail(&list1);
93 free(list1.remove(&list1));
95 list1.prev(&list1);
96 list1.prev(&list1);
97 free(list1.remove(&list1));
99 p_int = malloc(sizeof(int));
100 *p_int = 77;
101 list1.insert_at_head(&list1, p_int);
105 p_mode = calloc(1, sizeof(mode_info));
106 p_mode->sizeid = 1;
107 p_mode->w = 640;
108 p_mode->h = 480;
109 p_mode->r = 60;
110 list2.push(&list2, p_mode);
112 p_mode = calloc(1, sizeof(mode_info));
113 p_mode->sizeid = 2;
114 p_mode->w = 800;
115 p_mode->h = 600;
116 p_mode->r = 85;
117 list2.push(&list2, p_mode);
119 p_mode = calloc(1, sizeof(mode_info));
120 p_mode->sizeid = 3;
121 p_mode->w = 1024;
122 p_mode->h = 768;
123 p_mode->r = 72;
124 list2.push(&list2, p_mode);
126 p_mode = calloc(1, sizeof(mode_info));
127 p_mode->sizeid = 4;
128 p_mode->w = 1280;
129 p_mode->h = 1024;
130 p_mode->r = 60;
131 list2.push(&list2, p_mode);
134 printf("list2:\n");
136 list2.head(&list2);
137 size = list2.size(&list2);
138 for (i=0; i<size; ++i)
139 {
140 p_mode = list2.data(&list2);
141 printf("mode %d: %dx%d @ %d MHz\n",
142 p_mode->sizeid, p_mode->w, p_mode->h, p_mode->r);
143 list2.next(&list2);
144 }
145 printf("\n\n");
147 size = list1.size(&list1);
148 printf("List1 has %d elements\n", size);
149 list1.head(&list1);
150 for (i=0; i<size; ++i)
151 {
152 printf("%d ", *((int *) list1.data(&list1)) );
153 fflush(stdout);
154 list1.next(&list1);
155 }
156 printf("\n");
158 for (i=0; i<size; ++i)
159 {
160 p_int = list1.pop(&list1);
161 printf("%d\n", *p_int);
162 free(p_int);
163 }
166 list3 = malloc(sizeof(LinkedList));
167 if (!list3)
168 {
169 printf("malloc failed!\n");
170 exit(1);
171 }
173 p_int = malloc(sizeof(int));
174 *p_int = 21;
175 ll_push(list3, p_int);
176 p_int = malloc(sizeof(int));
177 *p_int = 22;
178 ll_push(list3, p_int);
179 p_int = malloc(sizeof(int));
180 *p_int = 23;
181 ll_push(list3, p_int);
184 ll_head(list3);
185 p_int = (int *) ll_data(list3);
186 printf("list3: ");
187 while(p_int)
188 {
189 printf("%d ", *p_int);
190 p_int = ll_next(list3);
191 }
192 printf("\n");
194 ll_tail(list3);
195 p_int = (int *) ll_data(list3);
196 printf("list3 reversed: ");
197 while(p_int)
198 {
199 printf("%d ", *p_int);
200 p_int = ll_prev(list3);
201 }
202 printf("\n");
204 printf("Clearing list3: ");
205 while (p_int = (int *) ll_pop(list3))
206 {
207 printf("%d ", *p_int);
208 }
209 printf("\n");
211 return 0;
212 }