diff options
Diffstat (limited to 'list.c')
-rw-r--r-- | list.c | 142 |
1 files changed, 142 insertions, 0 deletions
@@ -0,0 +1,142 @@ +#include "list.h" + +struct List* llist_new() +{ + struct List *list=MALLOC( sizeof(struct List) ); + list->first = NULL; + list->last = NULL; + list->count = 0; + return list; +} + + +struct ListNode* llist_newn( void *ptr ) +{ + struct ListNode *node = MALLOC( sizeof( struct ListNode ) ); + node->next = NULL; + node->val = ptr; + return node; +} + + +int llist_length( struct List *list ) +{ + int len; + struct ListNode *node=list->first; + for ( len=0; node; node=node->next,len++); + return len; +} + + +int llist_index( struct List *list, void *ptr ) +{ + int index; + struct ListNode *node = list->first; + for ( index=0; node; node=node->next, index++) + if ( node->val == ptr ) + return index; + return -1; +} + + +void llist_reverse( struct List **list ) +{ + //abort(0); +} + + +void llist_append( struct List *list, void *ptr ) +{ + //abort(0); +} + + +void llist_appendn( struct List **list, void *ptr, struct ListNode *node ) +{ + //abort(0); +} + + +void* llist_pop( struct List *list ) +{ + struct ListNode *node = list->last; + struct ListNode *iter = list->first; + if ( node ) + { + /* + if ( node->prev != NULL ) + { + void *ptr = node->val; + //node = node->prev; + //FREE( node->next ); + //node->next = NULL; + //list->last = node; + return ptr; + } + */ + + while (iter->next != node) + { + iter = iter->next; + } + void *ptr = node->val; + FREE(iter->next); + iter->next = NULL; + list->last = iter; + return ptr; + + } + return NULL; +} + + +void llist_push( struct List *list, void *ptr ) +{ + struct ListNode *node = MALLOC( sizeof(struct ListNode) ); + node->val = ptr; + node->next = NULL; + if ( list->last ) + { + list->last->next = node; + list->last = node; + } + else + { + list->first = node; + list->last = node; + } +} + + + +void llist_free( struct List *list ) +{ + struct ListNode *node = list->first, *prev; + + while ( node != list->last ) + { + prev = node; + node = node->next; + if (prev != NULL) + { + FREE( prev->val ); + FREE( prev ); + } + } + if ( node ) + { + if ( node->val ) + FREE( node->val ); + FREE( node ); + } + list->first = NULL; + list->last = NULL; + FREE( list ); + list = NULL; +} + +void llist_freen( struct ListNode *node ) +{ +} + + |