1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include "LL.h" |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | /* Tested functions: |
---|
8 | LL_New(); |
---|
9 | LL_Rewind(); |
---|
10 | LL_AddNode(); |
---|
11 | LL_Next(); |
---|
12 | LL_Get(); |
---|
13 | LL_Push(); |
---|
14 | LL_Unshift(); |
---|
15 | LL_InsertNode(); |
---|
16 | LL_Roll(); |
---|
17 | LL_UnRoll(); |
---|
18 | LL_SwapNodes(); |
---|
19 | LL_Sort(); |
---|
20 | LL_Find(); |
---|
21 | */ |
---|
22 | |
---|
23 | typedef struct my_struct |
---|
24 | { |
---|
25 | int num; |
---|
26 | char str[16]; |
---|
27 | } my_struct; |
---|
28 | |
---|
29 | |
---|
30 | int list_list(LL *list) |
---|
31 | { |
---|
32 | my_struct *foo; |
---|
33 | |
---|
34 | |
---|
35 | if(!list) return -1; |
---|
36 | |
---|
37 | printf("Listing the list contents...\n"); |
---|
38 | printf(" Items: %i\n", LL_Length(list)); |
---|
39 | |
---|
40 | |
---|
41 | LL_Rewind(list); |
---|
42 | |
---|
43 | do{ |
---|
44 | foo = (my_struct *)LL_Get(list); |
---|
45 | if(!foo) |
---|
46 | { |
---|
47 | printf("Can't read list data\n"); |
---|
48 | return -1; |
---|
49 | } |
---|
50 | printf("\tItem: %i\n", foo->num); |
---|
51 | |
---|
52 | } while(LL_Next(list) == 0); |
---|
53 | |
---|
54 | |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | int compare(void *one, void *two) |
---|
59 | { |
---|
60 | my_struct *a, *b; |
---|
61 | |
---|
62 | if(!one || !two) return 0; |
---|
63 | |
---|
64 | |
---|
65 | a = (my_struct *)one; |
---|
66 | b = (my_struct *)two; |
---|
67 | |
---|
68 | return (a->num - b->num); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | int main() |
---|
75 | { |
---|
76 | LL *list = NULL; |
---|
77 | my_struct *foo; |
---|
78 | my_struct bar; |
---|
79 | int i,j; |
---|
80 | |
---|
81 | |
---|
82 | list = LL_new(); |
---|
83 | if(!list) |
---|
84 | { |
---|
85 | printf("Cannot create list.\n"); |
---|
86 | return -1; |
---|
87 | } |
---|
88 | |
---|
89 | for(i=0; i<10; i++) |
---|
90 | { |
---|
91 | foo = malloc(sizeof(my_struct)); |
---|
92 | if(!foo) |
---|
93 | { |
---|
94 | printf("Can't allocate new struct\n"); |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | foo->num = i; |
---|
99 | |
---|
100 | LL_InsertNode(list, (void *)foo); |
---|
101 | printf("Added node: %i\n", foo->num); |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | if(list_list(list) != 0) return -1; |
---|
106 | |
---|
107 | printf("Sorting the list...\n"); |
---|
108 | |
---|
109 | if(LL_Sort(list, compare) < 0) |
---|
110 | { |
---|
111 | printf("Error sorting.\n"); |
---|
112 | return -1; |
---|
113 | } |
---|
114 | |
---|
115 | if(list_list(list) != 0) return -1; |
---|
116 | |
---|
117 | bar.num = 5; |
---|
118 | |
---|
119 | printf("Searching for item \"5\"\n"); |
---|
120 | LL_Rewind(list); |
---|
121 | |
---|
122 | foo = LL_Find(list, compare, &bar); |
---|
123 | if(foo) |
---|
124 | { |
---|
125 | printf(" Found... %i\n", foo->num); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | printf("Deallocating list...\n"); |
---|
130 | LL_Rewind(list); |
---|
131 | do{ |
---|
132 | foo = LL_Get(list); |
---|
133 | free(foo); |
---|
134 | |
---|
135 | } while(LL_Next(list) == 0); |
---|
136 | |
---|
137 | if(LL_Destroy(list) == 0) return 0; |
---|
138 | |
---|
139 | |
---|
140 | printf("Attempting to access list...\n"); |
---|
141 | |
---|
142 | printf("List = %x\n", list); |
---|
143 | |
---|
144 | LL_Rewind(list); |
---|
145 | |
---|
146 | do{ |
---|
147 | foo = LL_Get(list); |
---|
148 | if(foo) printf(" Item: %i\n", foo->num); |
---|
149 | |
---|
150 | } while(LL_Next(list) == 0); |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | return 0; |
---|
155 | |
---|
156 | } |
---|