Facebook
From MichaiƂ Kubik, 5 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 193
  1. //
  2. // Created by kubik on 7 lip 2018.
  3. //
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #include "linked_list.h"
  8.  
  9. struct linked_list_t *ll_create() {
  10.     return malloc(sizeof(List));
  11. }
  12.  
  13. int ll_push_back(struct linked_list_t *ll, int value) {
  14.     if (ll == NULL) return 1;
  15.     Node *n = malloc(sizeof(Node));
  16.     if (n == NULL) {
  17.         return 2;
  18.     }
  19.     n->data = value;
  20.     n->next = NULL;
  21.     if (ll->head == NULL) {
  22.         ll->head = n;
  23.         ll->tail = n;
  24.     } else {
  25.         ll->tail->next = n;
  26.         ll->tail = n;
  27.     }
  28.     return 0;
  29. }
  30.  
  31. int ll_push_front(struct linked_list_t *ll, int value) {
  32.     if (ll == NULL) return 1;
  33.     Node *n = malloc(sizeof(Node));
  34.     if (n == NULL) {
  35.         return 2;
  36.     }
  37.     n->data = value;
  38.     if (ll->head == NULL) {
  39.         ll->head = n;
  40.         ll->tail = n;
  41.         n->next = NULL;
  42.     } else {
  43.         n->next = ll->head;
  44.         ll->head = n;
  45.     }
  46.     return 0;
  47. }
  48.  
  49. int ll_pop_front(struct linked_list_t *ll, int *err_code) {
  50.     if (ll == NULL || ll->head == NULL) {
  51.         if (err_code != NULL) {
  52.             *err_code = 1;
  53.         }
  54.         return 0;
  55.     }
  56.     if (err_code != NULL) {
  57.         *err_code = 0;
  58.     }
  59.     int ret = ll->head->data;
  60.     Node *n = ll->head;
  61.     ll->head = ll->head->next;
  62.     free(n);
  63.     if (ll->head == NULL) {
  64.         ll->tail = NULL;
  65.     }
  66.     return ret;
  67. }
  68.  
  69. int ll_pop_back(struct linked_list_t *ll, int *err_code) {
  70.     if (ll == NULL || ll->head == NULL) {
  71.         if (err_code != NULL) {
  72.             *err_code = 1;
  73.         }
  74.         return 0;
  75.     }
  76.     if (err_code != NULL) {
  77.         *err_code = 0;
  78.     }
  79.     int ret = ll->tail->data;
  80.     if (ll->head == ll->tail) {
  81.         free(ll->head);
  82.         ll->head = NULL;
  83.         ll->tail = NULL;
  84.         return ret;
  85.     }
  86.     Node *n = ll->head;
  87.     for (; n->next != ll->tail; n = n->next);
  88.     free(ll->tail);
  89.     ll->tail = n;
  90.     n->next = NULL;
  91.     return ret;
  92. }
  93.  
  94. int ll_back(const struct linked_list_t *ll, int *err_code) {
  95.     if (ll == NULL || ll->head == NULL) {
  96.         if (err_code != NULL) {
  97.             *err_code = 1;
  98.         }
  99.         return 0;
  100.     }
  101.     if (err_code != NULL) {
  102.         *err_code = 0;
  103.     }
  104.     return ll->tail->data;
  105. }
  106.  
  107. int ll_front(const struct linked_list_t *ll, int *err_code) {
  108.     if (ll == NULL || ll->head == NULL) {
  109.         if (err_code != NULL) {
  110.             *err_code = 1;
  111.         }
  112.         return 0;
  113.     }
  114.     if (err_code != NULL) {
  115.         *err_code = 0;
  116.     }
  117.     return ll->head->data;
  118. }
  119.  
  120. struct node_t *ll_begin(struct linked_list_t *ll) {
  121.     if (ll == NULL || ll->head == NULL) {
  122.         return NULL;
  123.     }
  124.     return ll->head;
  125. }
  126.  
  127. struct node_t *ll_end(struct linked_list_t *ll) {
  128.     if (ll == NULL || ll->head == NULL) {
  129.         return NULL;
  130.     }
  131.     return ll->tail;
  132. }
  133.  
  134. int ll_size(const struct linked_list_t *ll) {
  135.     if (ll == NULL) {
  136.         return -1;
  137.     }
  138.     if (ll->head == NULL) {
  139.         return 0;
  140.     }
  141.     int i;
  142.     Node *n = ll->head;
  143.     for (i = 1; n != ll->tail; n = n->next, i++);
  144.     return i;
  145. }
  146.  
  147. int ll_is_empty(const struct linked_list_t *ll) {
  148.     if (ll == NULL) {
  149.         return -1;
  150.     }
  151.     if (ll->head == NULL) {
  152.         return 1;
  153.     }
  154.     return 0;
  155. }
  156.  
  157. int ll_at(const struct linked_list_t *ll, unsigned int index, int *err_code) {
  158.     if (ll == NULL || ll->head == NULL) {
  159.         if (err_code != NULL) {
  160.             *err_code = 1;
  161.         }
  162.         return 0;
  163.     }
  164.     if (err_code != NULL) {
  165.         *err_code = 0;
  166.     }
  167.     Node *n = ll->head;
  168.     unsigned int i;
  169.     for (i = 0; i < index; i++) {
  170.         n = n->next;
  171.         if (n == NULL) {
  172.             if (err_code) {
  173.                 *err_code = 1;
  174.             }
  175.             return 0;
  176.         }
  177.     }
  178.     return n->data;
  179. }
  180.  
  181. int ll_insert(struct linked_list_t *ll, unsigned int index, int value) {
  182.     if (ll == NULL || index > (unsigned int) ll_size(ll)) {
  183.         return 1;
  184.     }
  185.     Node *n = malloc(sizeof(Node));
  186.     if (n == NULL) {
  187.         return 2;
  188.     }
  189.     n->data = value;
  190.     if (index == 0) {
  191.         n->next = ll->head;
  192.         ll->head = n;
  193.         if (ll->tail == NULL) {
  194.             ll->tail = n;
  195.         }
  196.         return 0;
  197.     }
  198.     if (index == (unsigned int) ll_size(ll)) {
  199.         n->next = NULL;
  200.         ll->tail->next = n;
  201.         ll->tail = n;
  202.         return 0;
  203.     }
  204.     Node *node = ll->head;
  205.     unsigned int i;
  206.     for (i = 0; i < index - 1; i++) {
  207.         node = node->next;
  208.     }
  209.     n->next = node->next;
  210.     node->next = n;
  211.     if (ll->tail == node) {
  212.         ll->tail = n;
  213.     }
  214.     return 0;
  215. }
  216.  
  217. int ll_remove(struct linked_list_t *ll, unsigned int index, int *err_code) {
  218.     if (ll == NULL || ll->head == NULL || index >= (unsigned int) ll_size(ll)) {
  219.         if (err_code != NULL) {
  220.             *err_code = 1;
  221.         }
  222.         return 0;
  223.     }
  224.     if (err_code != NULL) {
  225.         *err_code = 0;
  226.     }
  227.     Node *node = ll->head;
  228.     int ret;
  229.     if (index == 0) {
  230.         if (ll->tail == ll->head) {
  231.             ll->tail = NULL;
  232.         }
  233.         ll->head = ll->head->next;
  234.         ret = node->data;
  235.         free(node);
  236.         return ret;
  237.     }
  238.     unsigned int i;
  239.     for (i = 0; i < index - 1; i++) {
  240.         node = node->next;
  241.     }
  242.     Node *next = node->next;
  243.     if (next->next == NULL) {
  244.         ll->tail = node;
  245.     }
  246.     node->next = node->next->next;
  247.     ret = next->data;
  248.     free(next);
  249.     return ret;
  250. }
  251.  
  252. void ll_clear(struct linked_list_t *ll) {
  253.     if (ll == NULL || ll->head == NULL) {
  254.         return;
  255.     }
  256.     Node *node = ll->head;
  257.     ll->head = NULL;
  258.     ll->tail = NULL;
  259.     Node *next = node;
  260.     while (next != NULL) {
  261.         next = node->next;
  262.         free(node);
  263.         node = next;
  264.     }
  265. }
  266.  
  267. void ll_display(const struct linked_list_t *ll) {
  268.     if (ll == NULL || ll->head == NULL) {
  269.         return;
  270.     }
  271.     Node *node = ll->head;
  272.     while (node != NULL) {
  273.         printf("%d ", node->data);
  274.         node = node->next;
  275.     }
  276. }