SHARE
TWEET

Untitled

a guest Feb 14th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef LINKEDLIST_H_
  2. #define LINKEDLIST_H_
  3. /**
  4.  *  Authors: Raj Gadhia, Grady McDowell
  5.  *  Date: 2/14/20
  6.  *  Pledge: I pledge my honor that I have abided by the Stevens Honor System.
  7.  *
  8.  *
  9.  **/
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "node.h"
  13.  
  14. typedef struct linked_list {
  15.     node *head;
  16.     node *tail;
  17.     size_t num_nodes;
  18. } linked_list;
  19.  
  20. linked_list* create_linked_list() {
  21.     return (linked_list *)calloc(1, sizeof(linked_list));
  22. }
  23.  
  24. /**
  25.  * Inserts a node into a non-NULL linked list.
  26.  * The node is inserted so that the data in the linked list is in
  27.  * non-decreasing order.
  28.  * A node with a value already in the list is inserted AFTER the node(s)
  29.  * already in the list.
  30.  */
  31. void insert_in_order(linked_list *list, node *n,
  32.                      int (*cmp)(const void*, const void*)) {
  33.        
  34.     list -> num_nodes ++;
  35.     if (list->head == NULL){
  36.         list->head = n;
  37.         list->tail = n;
  38.         return;
  39.     }
  40.     else if(cmp(list->head->data, n->data) >= 0){
  41.         n->next= list->head;
  42.         n->next->prev = n;
  43.         list->head = n;
  44.     } else {
  45.     node *curr = list->head;
  46.     while(curr -> next != NULL && cmp(curr -> next -> data, n-> data) < 0 ){
  47.         curr = curr -> next;
  48.     }
  49.     n->next = curr ->next;
  50.     if(curr ->next != NULL){
  51.         n->next->prev = n;
  52.     }
  53.     curr -> next = n;
  54.     n-> prev = curr;
  55.     }
  56.    
  57.     node *temp = list->head;
  58.     while(temp -> next != NULL){
  59.         temp = temp -> next;
  60.     }
  61.     list->tail = temp;
  62.    
  63. }  
  64.  
  65.  
  66. void print_list(linked_list *list, void (*print_function)(void*)) {
  67.     putchar('[');
  68.     node *cur = list->head;
  69.     if (cur != NULL) {
  70.         print_function(cur->data);
  71.         cur = cur->next;
  72.     }
  73.     for ( ; cur != NULL; cur = cur->next) {
  74.         printf(", ");
  75.         print_function(cur->data);
  76.     }
  77.     printf("]\n{length: %lu, head->data: ", list->num_nodes);
  78.     list->head != NULL ? print_function(list->head->data) :
  79.                          (void)printf("NULL");
  80.     printf(", tail->data: ");
  81.     list->tail != NULL ? print_function(list->tail->data) :
  82.                          (void)printf("NULL");
  83.     printf("}\n\n");
  84. }
  85.  
  86. /**
  87.  * Frees a list starting from the tail.
  88.  * This will check if your previous pointers have been set up correctly.
  89.  */
  90. void free_list(linked_list *list, void (*free_data)(void *)) {
  91.     while (list->tail != NULL) {
  92.         node *prev = list->tail->prev;
  93.         free_node(list->tail, free_data);
  94.         list->tail = prev;
  95.     }
  96. }
  97.  
  98. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top