sp1d3o

ll pičovina

Jan 7th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct Node {
  6.     int data;
  7.     struct Node *next;
  8. } node_t;
  9.  
  10. typedef struct List {
  11.     node_t *b_node;
  12. } list_t;
  13.  
  14. void print_list(list_t *list);
  15. void add_start(list_t *list, int new_data);
  16. void add_end(list_t *list, int new_data);
  17. void del_after(list_t *list, int mark);
  18. void del_before(list_t *list, int mark);
  19. void init_empty();
  20. void del_all(list_t *list);
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.     list_t *List = (list_t *)malloc(sizeof(list_t));
  25.     //list_t *List2 = (list_t *)malloc(sizeof(list_t));
  26.  
  27.  
  28.     node_t *head = (node_t *)malloc(sizeof(node_t));
  29.     node_t *second = (node_t *)malloc(sizeof(node_t));
  30.     node_t *third = (node_t *)malloc(sizeof(node_t));
  31.     node_t *fourth = (node_t *)malloc(sizeof(node_t));
  32.     node_t *fifth = (node_t *)malloc(sizeof(node_t));
  33.  
  34.     List->b_node = head;
  35.  
  36.     /*List2->b_node = NULL;
  37.     List2->c_node = NULL;*/
  38.  
  39.     head->data = 1;
  40.     head->next = second;
  41.  
  42.     second->data = 2;
  43.     second->next = third;
  44.  
  45.     third->data = 3;
  46.     third->next = fourth;
  47.  
  48.     fourth->data = 4;
  49.     fourth->next = fifth;
  50.  
  51.     fifth->data = 5;
  52.     fifth->next = NULL;
  53.  
  54.     print_list(List);
  55.     add_start(List, 9);
  56.     print_list(List);
  57.     add_end(List, 7);
  58.     print_list(List);
  59.     del_after(List, 1);
  60.     print_list(List);
  61.     del_before(List, 1);
  62.     print_list(List);
  63.  
  64.     return 0;
  65. }
  66.  
  67. void add_start(list_t *list, int new_data)
  68. {
  69.     if(NULL == list) {
  70.         fprintf(stderr, "You fucked up!\n");
  71.         fflush(stderr);
  72.         return;
  73.     }
  74.  
  75.     node_t *new = (node_t *)malloc(sizeof(node_t));
  76.  
  77.     new->data = new_data;
  78.  
  79.     new->next = list->b_node;
  80.     list->b_node = new;
  81. }
  82.  
  83. void print_list(list_t *list)
  84. {
  85.     if(NULL == list ) {
  86.         fprintf(stderr, "You fucked up!\n");
  87.         fflush(stderr);
  88.         return;
  89.     }
  90.     node_t  *current = list->b_node;
  91.  
  92.     while(current != NULL) {
  93.         printf("%d\n", current->data);
  94.         current = current->next;
  95.     }
  96.     printf("\n");
  97. }
  98.  
  99. void add_end(list_t *list, int new_data)
  100. {
  101.     if(list == NULL) {
  102.         fprintf(stderr, "You fucked up!\n");
  103.         fflush(stderr);
  104.         return;
  105.     }
  106.  
  107.     node_t *new = (node_t *)malloc(sizeof(node_t));
  108.     node_t *it;
  109.  
  110.     new->data = new_data;
  111.     new->next = NULL;
  112.  
  113.     it = list->b_node;
  114.     while(it != NULL) {
  115.         if(it->next == NULL) {
  116.             break;
  117.         }
  118.         it = it->next;
  119.     }
  120.  
  121.     if(list->b_node == NULL) {
  122.        list->b_node = new;
  123.     }
  124.     else {
  125.         it->next = new;
  126.     }
  127. }
  128.  
  129. void search_list(list_t *list, int mark)
  130. {
  131.     if(list == NULL) {
  132.         fprintf(stderr, "You fucked up!\n");
  133.         fflush(stderr);
  134.         return;
  135.     }
  136.  
  137.     node_t *it = list->b_node;
  138.  
  139.     while(it != NULL) {
  140.         if(it->data == mark) {
  141.             break;
  142.         }
  143.         it = it->next;
  144.     }
  145.  
  146.     list->c_node = it;
  147. }
  148. void del_afte(list_t *list, int mark)                                      //3 ukazuje na 4, 4 ukazuje na 5, 4 mažeme
  149. {
  150.  
  151.     if(list == NULL || list->c_node == NULL) {
  152.         fprintf(stderr, "You fucked up!\n");
  153.         fflush(stderr);
  154.         return;
  155.     }
  156.  
  157.     node_t *tmp = list->c_node->next;
  158.  
  159.     if(tmp != NULL) {
  160.         list->c_node->next = tmp->next;
  161.         free(tmp);
  162.     }
  163. }
  164.  
  165. void del_after(list_t *list, int mark)
  166. {
  167.     if(list == NULL) {
  168.         fprintf(stderr, "You fucked up!\n");
  169.         fflush(stderr);
  170.         return;
  171.     }
  172.  
  173.     node_t *it = list->b_node;
  174.  
  175.     while(it->data != mark) {
  176.         it = it->next;      //it is on the position where the mark is
  177.     }
  178.  
  179.     node_t *tmp = (node_t *)malloc(sizeof(node_t));
  180.  
  181.     tmp = list->b_node;
  182.  
  183.  
  184. }
  185.  
  186. void del_before(list_t *list, int mark) {
  187.     search_list(list, mark);
  188.  
  189.     if (list == NULL || list->c_node == NULL) {
  190.         fprintf(stderr, "You fucked up!\n");
  191.         fflush(stderr);
  192.         return;
  193.     }
  194.  
  195.     if (list->c_node != list->b_node) {
  196.  
  197.         node_t *it = list->b_node;
  198.  
  199.         while (it->next != list->c_node) {
  200.             it = it->next;
  201.         }
  202.  
  203.         if (it == list->b_node) {
  204.             list->b_node = list->c_node;
  205.             free(it);
  206.         }
  207.         else {
  208.             node_t *tmp = it;
  209.  
  210.             it = list->b_node;
  211.  
  212.             while (it->next != tmp) {
  213.                 it = it->next;
  214.             }
  215.  
  216.             it->next = tmp->next;
  217.             free(tmp);
  218.  
  219.         }
  220.     }
  221. }
  222.  
  223. void init_empty()
  224. {
  225.     list_t *empty_list = (list_t *)malloc(sizeof(list_t));
  226.  
  227.     empty_list->b_node = NULL;
  228.     empty_list->c_node = NULL;
  229. }
  230.  
  231. void del_all(list_t *list)
  232. {
  233.     node_t *current = list->b_node;
  234.     node_t *ptr;
  235.  
  236.     while(current != NULL) {
  237.         ptr = current->next;
  238.         free(current);
  239.         current = ptr;
  240.     }
  241.  
  242.     list->b_node = NULL;
  243. }
Add Comment
Please, Sign In to add comment