Guest User

Untitled

a guest
Mar 2nd, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include "list.h"
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. void list_node_init( struct List_Node* node, int val)
  7. {
  8.     node->data = val;
  9.     node->next = 0;
  10. }
  11.  
  12. void list_init ( Linked_List* list )
  13. {
  14.     list->nodeCount = 0;
  15.     list->first     = 0;
  16.     list->last      = 0;
  17. }
  18.  
  19. void list_clear ( Linked_List* list )
  20. {
  21.     while ( list->first != 0 ) {
  22.         list_erase( list, 0);
  23.     }
  24. }
  25.  
  26. void list_push_back ( Linked_List* l, u_int val )
  27. {
  28.     struct List_Node* newNode = (struct List_Node* ) malloc ( sizeof ( newNode ) );
  29.     list_node_init( newNode, val);
  30.  
  31.     if ( l->first == 0 ) {
  32.         l->first = newNode;
  33.     }
  34.     if( l->last != 0 ) {
  35.         l->last->next = newNode;
  36.     }
  37.     l->last = newNode;
  38.  
  39.     l->nodeCount++;
  40. }
  41.  
  42. void list_erase ( Linked_List* l, u_int where )
  43. {
  44.     struct List_Node* temp = 0;
  45.  
  46.     if ( where == 0 ) {
  47.         temp = l->first;
  48.  
  49.         l->first = l->first->next;
  50.         l->nodeCount--;
  51.  
  52.         free ( temp );
  53.         return;
  54.     }
  55.  
  56.     struct List_Node* conductor = l->first;
  57.     for (int i = 0; i < where - 1; i++ ) {
  58.         conductor = conductor->next;
  59.     }
  60.     temp = conductor->next;
  61.     conductor->next = conductor->next->next;
  62.     free ( conductor );
  63.     l->nodeCount--;
  64. }
  65.  
  66. int list_get_node_val ( Linked_List* list, int where )
  67. {
  68.     struct List_Node* conductor = list->first;
  69.  
  70.     for (int i = 0; i < where; i++ ) {
  71.         conductor = conductor->next;
  72.     }
  73.  
  74.     return conductor->data;
  75. }
Add Comment
Please, Sign In to add comment