Mercurial > LinkedList
changeset 2:c6e339846527
Rewrote ll_next and ll_prev to fix bug and implement NULL return when end of list is reached.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Fri, 31 Dec 2010 12:47:46 -0600 |
parents | 112164f48f30 |
children | 34f1d2f54d37 |
files | src/LinkedList.c src/main.c |
diffstat | 2 files changed, 34 insertions(+), 13 deletions(-) [+] |
line diff
1.1 --- a/src/LinkedList.c Wed Dec 29 02:44:09 2010 -0600 1.2 +++ b/src/LinkedList.c Fri Dec 31 12:47:46 2010 -0600 1.3 @@ -236,36 +236,37 @@ 1.4 1.5 1.6 /******************************************************************************* 1.7 - * Select the next element in the list if there is one. 1.8 - * Return data pointer of new current element, or NULL is list is empty. 1.9 + * Return NULL if the list is empty or we are already at the tail, otherwise 1.10 + * set Curr to the next element and return its data pointer. 1.11 */ 1.12 void * ll_next(LinkedList * this) 1.13 { 1.14 if (!this) return NULL; 1.15 1.16 - if (this->Curr->next) 1.17 + if (this->Curr && this->Curr->next) 1.18 + { 1.19 this->Curr = this->Curr->next; 1.20 - 1.21 - if (this->Curr) 1.22 return this->Curr->data; 1.23 + } 1.24 else 1.25 return NULL; 1.26 + 1.27 } 1.28 1.29 1.30 /******************************************************************************* 1.31 - * Select previous element in the list if there is one. 1.32 - * Return data pointer of new current element, or NULL is list is empty. 1.33 + * Return NULL if the list is empty or we are already at the head, otherwise 1.34 + * set Curr to the prev element and return its data pointer. 1.35 */ 1.36 void * ll_prev(LinkedList * this) 1.37 { 1.38 if (!this) return NULL; 1.39 1.40 - if (this->Curr->prev) 1.41 + if (this->Curr && this->Curr->prev) 1.42 + { 1.43 this->Curr = this->Curr->prev; 1.44 - 1.45 - if (this->Curr) 1.46 return this->Curr->data; 1.47 + } 1.48 else 1.49 return NULL; 1.50 }
2.1 --- a/src/main.c Wed Dec 29 02:44:09 2010 -0600 2.2 +++ b/src/main.c Fri Dec 31 12:47:46 2010 -0600 2.3 @@ -162,10 +162,30 @@ 2.4 *p_int = 23; 2.5 ll_push(list3, p_int); 2.6 2.7 - printf("\n\nlist3: "); 2.8 - for (i=0, size=ll_size(list3); i<size; ++i) 2.9 + 2.10 + ll_head(list3); 2.11 + p_int = (int *) ll_data(list3); 2.12 + printf("list3: "); 2.13 + while(p_int) 2.14 { 2.15 - p_int = (int *) ll_pop(list3); 2.16 + printf("%d ", *p_int); 2.17 + p_int = ll_next(list3); 2.18 + } 2.19 + printf("\n"); 2.20 + 2.21 + ll_tail(list3); 2.22 + p_int = (int *) ll_data(list3); 2.23 + printf("list3 reversed: "); 2.24 + while(p_int) 2.25 + { 2.26 + printf("%d ", *p_int); 2.27 + p_int = ll_prev(list3); 2.28 + } 2.29 + printf("\n"); 2.30 + 2.31 + printf("Clearing list3: "); 2.32 + while (p_int = (int *) ll_pop(list3)) 2.33 + { 2.34 printf("%d ", *p_int); 2.35 } 2.36 printf("\n");