Advertisement
Redxone

(C) A simple Linked-List utility

Jan 19th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.    int val;
  7.    struct node *next;
  8. } node_l;
  9.  
  10.  
  11. node_l *initNodes( void );
  12. void addNode (node_l*, int);
  13. void pushNode(node_l **, int);
  14. int  popNode(node_l **);
  15. void print_nodes(node_l *);
  16. void insertNode(node_l **, int, int);
  17. int  removeNode(node_l **, int);
  18. int  searchNode(node_l *, int);
  19. void *tryalloc(int);
  20.  
  21. int main()
  22. {
  23.     node_l *mylist = initNodes();
  24.     mylist->val = 10;
  25.     addNode(mylist, 20);
  26.     addNode(mylist, 30);
  27.     insertNode(&mylist, 2, 15);
  28.     removeNode(&mylist,searchNode(mylist,15));
  29.     print_nodes(mylist);
  30.     free(mylist);
  31.     return 0;
  32. }
  33.  
  34. void *tryalloc(int s)
  35. {
  36.     void *ptr;
  37.     ptr = malloc(s);
  38.     if(ptr == NULL)printf("Failed to allocate %d bytes. Memory is full!",s);
  39.     return ptr;
  40. }
  41.  
  42. void print_nodes(node_l *base)
  43. {
  44.     node_l *current = base;
  45.     while(current != NULL)
  46.     {
  47.         printf("List Item: %d \n",current->val);
  48.         current = current->next;
  49.     }
  50. }
  51.  
  52. int searchNode(node_l *base, int val)
  53. {
  54.     node_l *current = base;
  55.     register int i = 0;
  56.     while(current != NULL)
  57.     {
  58.         i++;
  59.         if(current->val == val) return i-1;
  60.         current = current->next;
  61.     }
  62.     return -1;
  63. }
  64.  
  65. int removeNode(node_l **base, int index)
  66. {
  67.     // Verify list is with-in index bounds.
  68.     if(index == 0)
  69.     {
  70.         return popNode(base);
  71.     }
  72.     int ret = -1;
  73.     register int i = 0;
  74.     node_l *tmp = NULL;
  75.     node_l *current = *base;
  76.     while(current != NULL && (i < index-1))
  77.     {
  78.         i++;
  79.         current = current->next;
  80.     }
  81.     if(i < index-1)
  82.     {
  83.         puts("Invalid node index.");
  84.         return -1;
  85.     }
  86.     // Get selected node from index.
  87.     tmp = current->next;
  88.     ret = tmp->val;
  89.     // Set the next node in our list to the node after the selected one we're going to delete.
  90.     current->next = tmp->next;
  91.     // Deallocate selected node.
  92.     free(tmp);
  93.     return ret;
  94. }
  95.  
  96. void insertNode(node_l **base, int index, int val)
  97. {
  98.     // Verify list is with-in index bounds.
  99.     if(index == 0)
  100.     {
  101.         return pushNode(base, val);
  102.     }
  103.     register int i = 0;
  104.     node_l *new_node = NULL;
  105.     node_l *current = *base;
  106.     while(current != NULL && (i < index-1))
  107.     {
  108.         i++;
  109.         current = current->next;
  110.     }
  111.     if(i < index-1)
  112.     {
  113.         puts("Invalid node index.");
  114.         return;
  115.     }
  116.  
  117.     // Locate nodes.
  118.     new_node = tryalloc(sizeof(node_l));
  119.     new_node->val = val;
  120.     new_node->next = current->next;
  121.     current->next = new_node;
  122. }
  123.  
  124. int popNode(node_l **base)
  125. {
  126.     int result = -1;
  127.     // We need to remove the first node and instead point it to the second.
  128.     if(*base == NULL)
  129.     {
  130.         // If we only have 1 item in the list, delete the list.
  131.         return -1;
  132.     }
  133.     // Get next node and assign that to the new header node.
  134.     node_l *next_node = (*base)->next;
  135.     // Assign return result to value.
  136.     result = (*base)->val;
  137.     // Deallocate the memory for previous header node.
  138.     free(*base);
  139.     // Set header node (first node our base points to) to next node in list.
  140.     *base = next_node;
  141.     return result;
  142. }
  143.  
  144. void pushNode(node_l **base, int value)
  145. {
  146.     // Double pointer used here so we can modify the base pointer.
  147.     node_l *new_base;
  148.     new_base = tryalloc(sizeof(node_l));
  149.  
  150.     new_base->val = value;
  151.     new_base->next = *base;
  152.     *base = new_base;
  153. }
  154.  
  155. void addNode(node_l *base, int value)
  156. {
  157.     node_l *current = base;
  158.     while(current->next != NULL)
  159.     {
  160.         current = current->next;
  161.     }
  162.     current->next = NULL;
  163.     current->next = tryalloc(sizeof(node_l));
  164.     current->next->val  = value;
  165.     current->next->next = NULL;
  166. }
  167.  
  168. node_l *initNodes()
  169. {
  170.     // Create pointer to first item in linked-list.
  171.     node_l *head = NULL;
  172.     // Get location in Heap where allocated space is available for first item in this linked list.
  173.     head = tryalloc(sizeof(node_l));
  174.     head->next = NULL;
  175.     return head;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement