Facebook
From Social Capybara, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 218
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"
  4.  
  5.  
  6. List* create_list() //creates and allocates memory for an empty list.
  7. {
  8.  
  9.     List* L = (List)malloc(sizeof(List));
  10.     L->head = NULL;
  11.     L->tail = NULL;
  12.     return(L);
  13. }
  14. Node create_node(int key) //creates a node with a key and allocates memory for it.
  15. {
  16.  
  17.     Node* N = (Node)malloc(sizeof(Node));
  18.     N->key = key;
  19.     N->next = NULL;
  20.     N->prev = NULL;
  21.     return(N);
  22. }
  23. int isEmpty(List L)
  24. {
  25.     return L->head == NULL; //If there is no head, the list is empty and it will either return 1 or 0
  26. }
  27. void insert(List* L, Node* N) //Inserts a node into a list.
  28. {
  29.  
  30.     if (L->head != NULL)    //If there is no head set the node N to head
  31.     {
  32.         L->head->prev = N;
  33.     }
  34.     if (L->tail == NULL)    //And if there is no tail set N to tail
  35.     {
  36.         L->tail = N;
  37.     }
  38.     N->next = L->head;
  39.     N->prev = NULL;
  40.     L->head = N;
  41. }
  42. Node* search(const List* L, int key)    //Searching for a node using its key.
  43. {
  44.     Node* temp_pointer = L->head;
  45.     while (temp_pointer != NULL)
  46.     {
  47.  
  48.         if (temp_pointer->key == key)    //checking if the nodes key matches the searched key.
  49.         {
  50.             break;
  51.         }
  52.         temp_pointer = temp_pointer->next;
  53.     }
  54.     return(temp_pointer);
  55. }
  56. Node* node_delete(List* L, Node* N)        //removes a node from the list and returns it
  57. {
  58.     if(N->prev != NULL)
  59.     {
  60.         N->prev->next = N->next;    //makes the previous node point to the next from where it's currently at.
  61.     }
  62.     else
  63.     {
  64.         L->head = N->next;            //if the previous is NULL set the one it's currently at to head
  65.     }
  66.     if (N->next != NULL)
  67.     {
  68.         N->next->prev = N->prev;    //the next node will now point to the previous one from where it's currently at.
  69.     }
  70.     if (L->tail == N)
  71.     {
  72.         L->tail = N->prev;        //keeping the tail up to date
  73.     }
  74.     return(N);
  75. }
  76. Node* maximum(List* L)    //Searching for the node with the highest "key" value by comparing every key to the previous one, saving the largest
  77. {
  78.     Node* temp_pointer = L->head,max = L->head;
  79.  
  80.  
  81.     while (temp_pointer!= NULL)
  82.     {
  83.         if (temp_pointer->key > max->key) //if the temp key is larger than the previous max set max to that temp node
  84.         {
  85.             max = temp_pointer;
  86.  
  87.         }
  88.         temp_pointer = temp_pointer->next;
  89.  
  90.     }
  91.     return(max);
  92.  
  93. }
  94. Node minimum(List* L)    //Same as maximum except searching for the minimum
  95. {
  96.     Node* temp_pointer = L->head, *min = L->head;
  97.  
  98.  
  99.     while (temp_pointer != NULL)
  100.     {
  101.         if (temp_pointer->key < min->key)
  102.         {
  103.             min = temp_pointer;
  104.  
  105.         }
  106.         temp_pointer = temp_pointer->next;
  107.  
  108.     }
  109.     return(min);
  110.  
  111. }