2 // similar to plist.cpp, except uses
5 // Note: the direction, trail stack is defined in bot.cpp
12 int isempty(PtrList
*list
)
15 if (list
->head
== NULL
)
16 return 1; /* first pointer null, list is empty */
20 } /* end of the fcuntion */
25 PtrNode
*CreatePtrNode(void *data
) {
27 PtrNode
*h
= (PtrNode
*) malloc(sizeof(PtrNode
));
34 } // end of the function
39 void DestroyPtrNode(PtrNode
*node
) {
43 } // end of the functino
48 PtrList
*CreatePtrList() {
50 PtrList
*result
= (PtrList
*) malloc(sizeof(PtrList
));
57 } // end of the function
63 void DestroyPtrList(PtrList
*list
) {
78 } // end of the function
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
) {
94 current
= previous
->next
;
98 if (previous
!= NULL
) {
100 previous
->next
= current
->next
;
109 } // end of the if - else
111 } // end of the while
113 } // end of the function
118 void InsertFront(PtrList
*list
, void *data
) {
120 PtrNode
*new_node
= NULL
;
122 new_node
= CreatePtrNode(data
);
126 list
->head
= new_node
;
130 new_node
->next
= list
->head
;
131 list
->head
= new_node
;
137 } // end of the function
142 void *RemoveFront(PtrList
*list
)
145 PtrNode
*temp_ptr
= NULL
;
151 temp_ptr
= list
->head
;
152 if (list
->head
->next
== NULL
)
153 list
->head
= NULL
; /* reset */
155 list
->head
= list
->head
->next
;
157 res
= temp_ptr
->ptr
; // remove from list, but keep ptr
164 } // end of the if-else
166 return NULL
; // we should never get here
168 } // end of the function
174 void PrintPtrList(PtrList
*list
)
176 PtrNode
*current_ptr
;
182 current_ptr
= list
->head
;
184 while(current_ptr
!= NULL
)
187 x
= (int *)current_ptr
->ptr
;
189 printf("<%d>\n", (int &)*x
);
190 current_ptr
= current_ptr
->next
;
193 } // end of the function
198 void PrintPtrListf(PtrList
*list
)
200 PtrNode
*current_ptr
;
206 current_ptr
= list
->head
;
208 while(current_ptr
!= NULL
)
211 x
= (float *)current_ptr
->ptr
;
213 printf("<%0.2f>\n", (float &)*x
);
214 current_ptr
= current_ptr
->next
;
217 } // end of the function
223 void PtrLinkTest(void)
226 int *x
= (int *)malloc(sizeof(int));
227 int *y
= (int *)malloc(sizeof(int));
228 int *z
= (int *)malloc(sizeof(int));
235 //list = CreatePtrList();
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
);
263 InsertFront(list
, (int *)y
);
264 printf("%d PUSHED %d\n", *y
, list
->items
);
266 printf("\n\n New with Added\n");
269 DestroyPtrList(list
);
275 } // end of the function