Initial Import
[glAntsMech.git] / glants_mech / win32 / glAntsV05 / plist.cpp
blobe749e9d64b8557e62423c7f89da0a814a8ac8bb0
1 //
2 // similar to plist.cpp, except uses
3 //
5 // Note: the direction, trail stack is defined in bot.cpp
7 #include <stdio.h>
8 #include <stdlib.h>
10 #include "plist.h"
12 int isempty(PtrList *list)
15 if (list->head == NULL)
16 return 1; /* first pointer null, list is empty */
17 else
18 return 0;
20 } /* end of the fcuntion */
23 // CreatePtrNode
25 PtrNode *CreatePtrNode(void *data) {
27 PtrNode *h = (PtrNode *) malloc(sizeof(PtrNode));
29 h->ptr = data;
30 h->next = NULL;
32 return h;
34 } // end of the function
37 // DestroyPtrNode
39 void DestroyPtrNode(PtrNode *node) {
41 free(node);
43 } // end of the functino
45 //
46 // Create PtrList
48 PtrList *CreatePtrList() {
50 PtrList *result = (PtrList *) malloc(sizeof(PtrList));
52 result->head = NULL;
53 result->items = 0;
55 return result;
57 } // end of the function
61 // DestroyPtrList
63 void DestroyPtrList(PtrList *list) {
65 PtrNode *pos, *next;
66 pos = list->head;
68 while(pos != NULL) {
69 next = pos->next;
71 free(pos);
73 pos = next;
74 } // end of the while
76 free(list);
78 } // end of the function
82 // Delete Ptr
83 //
84 void DeletePtrNode(PtrList *list, void *val)
86 PtrNode *current = list->head;
87 PtrNode *previous = NULL;
89 while (current != NULL ) {
91 if( current->ptr != val) {
93 previous = current;
94 current = previous->next;
96 } else {
98 if (previous != NULL ) {
100 previous->next = current->next;
102 } // end of the if
104 list->items--;
106 free(current);
107 break;
109 } // end of the if - else
111 } // end of the while
113 } // end of the function
117 // Insert Front
118 void InsertFront(PtrList *list, void *data) {
120 PtrNode *new_node = NULL;
122 new_node = CreatePtrNode(data);
124 if (isempty(list))
126 list->head = new_node;
128 else {
130 new_node->next = list->head;
131 list->head = new_node;
133 } // end if
135 list->items++;
137 } // end of the function
140 // Remove Front
142 void *RemoveFront(PtrList *list)
145 PtrNode *temp_ptr = NULL;
146 void *res = NULL;
148 if (isempty(list))
149 return NULL;
150 else {
151 temp_ptr = list->head;
152 if (list->head->next == NULL)
153 list->head = NULL; /* reset */
154 else
155 list->head = list->head->next;
157 res = temp_ptr->ptr; // remove from list, but keep ptr
159 free(temp_ptr);
160 list->items--;
162 return res;
164 } // end of the if-else
166 return NULL; // we should never get here
168 } // end of the function
172 // PrintTest
174 void PrintPtrList(PtrList *list)
176 PtrNode *current_ptr;
177 int *x;
179 if (isempty(list))
180 return;
182 current_ptr = list->head;
184 while(current_ptr != NULL)
186 // interesting
187 x = (int *)current_ptr->ptr;
189 printf("<%d>\n", (int &)*x);
190 current_ptr = current_ptr->next;
191 } // end of while
193 } // end of the function
196 // PrintList
198 void PrintPtrListf(PtrList *list)
200 PtrNode *current_ptr;
201 float *x;
203 if (isempty(list))
204 return;
206 current_ptr = list->head;
208 while(current_ptr != NULL)
210 // interesting
211 x = (float *)current_ptr->ptr;
213 printf("<%0.2f>\n", (float &)*x);
214 current_ptr = current_ptr->next;
215 } // end of while
217 } // end of the function
221 // PtrListTest
223 void PtrLinkTest(void)
225 PtrList *list;
226 int *x = (int *)malloc(sizeof(int));
227 int *y = (int *)malloc(sizeof(int));
228 int *z = (int *)malloc(sizeof(int));
229 int *res;
231 *x = 9;
232 *y = 3;
233 *z = 4;
235 //list = CreatePtrList();
236 list = CREATE_STACK;
238 //InsertFront(list, (int *)x);
239 PUSH_STACK(list, (int *)x);
240 printf("%d PUSHED\n", *x);
242 //InsertFront(list, (int *)y);
243 PUSH_STACK(list, (int *)y);
244 printf("%d PUSHED\n", *y);
246 InsertFront(list, (int *)z);
247 printf("%d PUSHED\n", *z);
249 //PrintPtrList(list);
251 // Delete a node pointer
252 //DeletePtrNode(list, y);
253 //res = (int *)RemoveFront(list);
254 res = (int *)POP_STACK(list);
255 printf("%d POPPED %d\n", *res, list->items);
257 res = (int *)RemoveFront(list);
258 printf("%d POPPED %d\n", *res, list->items);
260 printf("\n\n New List %d\n", list->items);
261 PrintPtrList(list);
263 InsertFront(list, (int *)y);
264 printf("%d PUSHED %d\n", *y, list->items);
266 printf("\n\n New with Added\n");
267 PrintPtrList(list);
269 DestroyPtrList(list);
271 free(x);
272 free(y);
273 free(z);
275 } // end of the function