Advertisement
Thefaceofbo

Linked Lists (C)

Nov 16th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.37 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct llnode {            /* since the llnode is a self-referential structure */
  5.    int val;                /* we can not compile the struct and typedef declaration into one */
  6.    struct llnode *next;
  7. };
  8. typedef struct llnode llnode;
  9.  
  10. int ll_find_by_value(llnode *, int);
  11. int ll_del_by_value(llnode **, int);  
  12. int ll_del_from_head( llnode **);
  13. int ll_del_from_tail( llnode **);
  14. int ll_add_to_head( llnode **head, int val);
  15. int ll_add_to_tail( llnode **head, int val);
  16. int ll_insert_in_order(llnode **, int);
  17. int ll_concat(llnode **, llnode **);
  18. int ll_sort(llnode **);
  19. int ll_print( llnode *p);
  20. int ll_free( llnode *p);
  21.  
  22. /* One of the lessons here is to see that if we want to use a function to malloc something that
  23.  * is a POINTER in the CALLER of the function, then we must send in the ADDRESS of the POINTER
  24.  * to that function.
  25.  *
  26.  * Recap: if we want to use a function to modify a VARIABLE in the caller
  27.  *        then the CALLER needs to send in the ADDRESS of the VARIABLE
  28.  *
  29.  * Similarly: if we want to use a function to modify a POINTER in the caller
  30.  *            then the CALLER needs to send in the ADDRESS of the POINTER
  31.  *
  32.  * In the code below, ll_add_to_head and ll_add_to_tail are dynamically creating new
  33.  * nodes to be added to the linked list. Any dynamic creation of a node must be via
  34.  * malloc.
  35.  */
  36.  
  37.  int main(void) {
  38.     llnode *root = NULL;
  39.     llnode *root2 = NULL;
  40.     int r=0;
  41.     int i=0;
  42.  
  43.     printf("List A\n");
  44.     r=ll_add_to_tail(&root,100);
  45.     r=ll_add_to_tail(&root,200);
  46.     r=ll_add_to_tail(&root,300);
  47.     for(i=0;i<10;i=i+1){
  48.        r=ll_add_to_tail(&root,i*100);
  49.     }
  50.     r=ll_print(root);
  51.      /* r=ll_free(root); */
  52.  
  53.     printf("List B\n");
  54.     // root=NULL;
  55.     r=ll_add_to_tail(&root2,100);
  56.     r=ll_add_to_tail(&root2,200);
  57.     r=ll_add_to_tail(&root2,300);
  58.     for(i=0;i<10;i=i+1){
  59.        r=ll_add_to_head(&root2,i*100);
  60.     }
  61.     r=ll_print(root2);
  62.     printf("Deleting Head\n");
  63.     r=ll_sort(&root2);
  64.     r=ll_insert_in_order(&root2, 350);
  65.     r=ll_print(root2);
  66.     r=ll_find_by_value(root2, 20);
  67.     printf("Search Result: %d \n", r);
  68.     r=ll_free(root2);
  69.     return 0;
  70.  }
  71.  
  72. int ll_del_from_head(llnode **ppList) {
  73.   llnode *old_head;
  74.   if (ppList == NULL || (*ppList)->next == NULL) {
  75.     return -1;
  76.   }
  77.   old_head = (llnode *)malloc(sizeof(llnode));
  78.   *old_head = **ppList;
  79.   *ppList = ((*ppList)->next);
  80.   free(old_head);
  81.   return 0;
  82. }
  83.  
  84. int ll_del_from_tail(llnode **ppList) {
  85.    if (ppList == NULL) {
  86.       return -1;
  87.    }
  88.    if ((*ppList)->next == NULL) {
  89.      free((*ppList));
  90.      return 0;
  91.    } else if ((*ppList)->next->next == NULL) {
  92.      free((*ppList)->next);
  93.      (*ppList)->next = NULL;
  94.      return 0;
  95.    } else {
  96.      return ll_del_from_tail(&((*ppList)->next));
  97.    }
  98.    return 0;
  99. }
  100.  
  101. int ll_del_by_value(llnode **ppList, int val) {
  102.   llnode *deleter;
  103.   if (((*ppList)->next) == NULL) {
  104.     return -1;
  105.   }
  106.   if ((*ppList)->next->val == val) {
  107.      /* Do the delete */
  108.      deleter = (llnode *)malloc(sizeof(llnode));
  109.      deleter = (*ppList)->next;
  110.      /* Change next link */
  111.      if (deleter->next != NULL) {
  112.        (*ppList)->next = deleter->next;
  113.      } else {
  114.        (*ppList)->next = NULL;
  115.      }
  116.      free(deleter);
  117.      return 0;
  118.   } else {
  119.      if ((*ppList)->next == NULL) { return -1; }
  120.      return ll_del_by_value(&((*ppList)->next), val);
  121.   }
  122.   return -1;
  123. }
  124.  
  125. int ll_find_by_value(llnode *pList, int val) {
  126.    if (pList->val == val) {
  127.       return 0;
  128.    } else {
  129.       if (pList->next == NULL) { return -1; }
  130.       return ll_find_by_value(pList->next, val);
  131.    }
  132.    return -1;
  133. }
  134.  
  135. int ll_add_to_head( llnode **head, int val) {
  136.     llnode *old_head;
  137.    if (head == NULL) {
  138.       return -1;
  139.    }
  140.         old_head = *head;
  141.  
  142.    *head = ( llnode *) malloc(sizeof( llnode));
  143.    (*head) -> val = val;
  144.         (*head) -> next = old_head;
  145.         return 0;
  146. }
  147.  
  148. int ll_add_to_tail( llnode **head, int val) {
  149.    if (head == NULL) {
  150.       return -1;
  151.    }
  152.    if (*head == NULL) {
  153.       *head = ( llnode *) malloc(sizeof( llnode));
  154.       (*head) -> val = val;
  155.       (*head) -> next = NULL;
  156.       return 0;
  157.    } else { /* recursively call ll_add_to_tail until we get to the tail which is the point where the pointer is NULL */
  158.       return ll_add_to_tail(&((*head)->next), val);
  159.    }
  160. }
  161.  
  162. int ll_insert_in_order(llnode **ppList, int val) {
  163.    llnode **a = NULL;
  164.    llnode *n = NULL;
  165.    llnode *new_node = NULL;
  166.    new_node = (llnode *)malloc(sizeof(llnode));
  167.    if (ppList == NULL) {
  168.       return -1;
  169.    }
  170.    /* Start by sorting the list */
  171.    ll_sort(ppList);
  172.    a = ppList;
  173.    while ((*a)->next->val > val) {
  174.       a = &((*a)->next);
  175.       if ((*a)->next == NULL) {
  176.          break;
  177.       }
  178.    }
  179.    /* Apply the Insert */
  180.    n = (*a)->next;
  181.    new_node->next = n;
  182.    new_node->val = val;
  183.    (*a)->next = new_node;
  184.    return 0;
  185. }
  186.  
  187. int ll_concat(llnode **a, llnode **b) {
  188.   if (a == NULL || b == NULL) {
  189.     return -1;
  190.   }
  191.   if ((*a)->next == NULL) {
  192.     (*a)->next = *b;
  193.     return 0;
  194.   } else {
  195.     return ll_concat(&((*a)->next), b);
  196.   }
  197.   return 0;
  198. }
  199.  
  200. int ll_sort(llnode **ppList) {
  201.   llnode *items = NULL;
  202.   int x,y,i = 0;
  203.   int size = 0; /* For my purposes to iterate from one is more intuitive in the instance */
  204.   int *vals = NULL;
  205.   llnode *a = NULL;
  206.   if (ppList == NULL) {
  207.     return -1;
  208.   }
  209.   a = (llnode *)malloc(sizeof(llnode));
  210.   /* Count List */
  211.   a = *ppList;
  212.   while (a != NULL) {
  213.     a = a->next;
  214.     size = size + 1;
  215.   }
  216.   vals = (int *)malloc(sizeof(int)*size);
  217.   /* Add Values to a list */
  218.   a = *ppList;
  219.   i = 0;
  220.   while (a != NULL) {
  221.      vals[i] = a->val;
  222.      i = i + 1;
  223.      a = a->next;
  224.   }
  225.   /* Do the Sort */
  226.   for (x=0; x<size; x++) {
  227.      for (y=0; y<size; y++) {
  228.         if (vals[x] > vals[y]) {
  229.            i = vals[x];
  230.            vals[x] = vals[y];
  231.            vals[y] = i;
  232.         }
  233.      }
  234.   }
  235.   /* Make new llnode chain */
  236.   for (i=0; i<size; i++) {
  237.      ll_add_to_tail(&items, vals[i]);
  238.   }
  239.   *ppList = items;
  240.   return 0;
  241. }
  242.  
  243. int ll_print( llnode *p) {
  244.    if (p==NULL) {
  245.       return 0;
  246.    } else {
  247.       printf("val = %d\n",p->val);
  248.       return ll_print(p->next);
  249.    }
  250. }
  251.  
  252. int ll_free( llnode *p) {
  253.    if (p==NULL) {
  254.       return -1;
  255.    } else {
  256.       llnode *f=p->next;
  257.       free(p);
  258.       return ll_free(f);
  259.    }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement