Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct list_node{
- struct list_node *p_next;
- void *p_data;
- };
- struct list_header{
- struct list_node *p_begin;
- unsigned int size;
- };
- void list_init( struct list_header *list );
- void list_node_init( struct list_node *node );
- void list_push_back( struct list_header *list, void *data );
- void list_reverse( struct list_header *list );
- int main( int argc, char *argv[] ){
- struct list_header list1;
- struct list_node *p_node;
- int arr[] = { 1,2,3,4,5,6,7,8,9 };
- int i;
- unsigned char arr_size = 9;
- list_init(&list1);
- for( i = 0; i < arr_size; ++i ){
- list_push_back( &list1, (void*)(&arr[ i ]) );
- }
- list_reverse(&list1);
- p_node = list1.p_begin;
- while(p_node != NULL){
- printf("%d ", *((int*)p_node->p_data) );
- p_node = p_node->p_next;
- }
- printf("\n");
- return 0;
- }
- void list_init( struct list_header *list ){
- list->p_begin = NULL;
- list->size = 0;
- }
- void list_node_init( struct list_node *node ){
- node->p_next = NULL;
- node->p_data = NULL;
- }
- void list_push_back( struct list_header *list, void *data ){
- struct list_node *p_cursor = NULL;
- if( list->p_begin == NULL ){
- /* Make first item */
- list->p_begin = malloc( sizeof( struct list_node ) );
- if( list->p_begin == NULL ){
- /* print error */
- exit( 0 );
- }else{
- p_cursor = list->p_begin;
- }
- }else{
- /* Roll cursor to the end */
- p_cursor = list->p_begin;
- while( p_cursor->p_next != NULL ){
- p_cursor = p_cursor->p_next;
- }
- p_cursor->p_next = malloc( sizeof( struct list_node ) );
- if( p_cursor->p_next == NULL ){
- /* print error */
- exit( 0 );
- }else{
- p_cursor = p_cursor->p_next;
- }
- }
- list_node_init( p_cursor );
- p_cursor->p_data = data;
- (list->size)++;
- }
- void list_reverse( struct list_header *list ){
- struct list_node *p_tmp, *p_reversed;
- p_tmp = NULL;
- p_reversed = NULL;
- if( list->p_begin == NULL ){
- return ;
- }
- while( list->p_begin != NULL ){
- p_tmp = list->p_begin;
- list->p_begin = list->p_begin->p_next;
- p_tmp->p_next = p_reversed;
- p_reversed = p_tmp;
- }
- list->p_begin = p_reversed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement