sp1d3o

Untitled

Jan 3rd, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 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.     node_t *c_node;
  13. } list_t;
  14.  
  15. void print_list(list_t *list);
  16. void add_start(list_t *list, int new_data);
  17. void add_end(list_t *list, int new_data);
  18. void del_after(list_t *list);
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     list_t *List = (list_t *)malloc(sizeof(list_t));
  23.     //list_t *List2 = (list_t *)malloc(sizeof(list_t));
  24.  
  25.  
  26.     node_t *head = (node_t *)malloc(sizeof(node_t));
  27.     node_t *second = (node_t *)malloc(sizeof(node_t));
  28.     node_t *third = (node_t *)malloc(sizeof(node_t));
  29.     node_t *fourth = (node_t *)malloc(sizeof(node_t));
  30.     node_t *fifth = (node_t *)malloc(sizeof(node_t));
  31.  
  32.     List->b_node = head;
  33.     List->c_node = NULL;
  34.  
  35.     /*List2->b_node = NULL;
  36.     List2->c_node = NULL;*/
  37.  
  38.     head->data = 1;
  39.     head->next = second;
  40.  
  41.     second->data = 2;
  42.     second->next = third;
  43.  
  44.     third->data = 3;
  45.     third->next = fourth;
  46.  
  47.     fourth->data = 4;
  48.     fourth->next = fifth;
  49.  
  50.     fifth->data = 5;
  51.     fifth->next = NULL;
  52.  
  53.  
  54.     add_end(List, 6);
  55.     print_list(List);
  56.  
  57.     return 0;
  58. }
  59.  
  60. void add_start(list_t *list, int new_data)
  61. {
  62.     if(NULL == list) {
  63.         fprintf(stderr, "You fucked up!\n");
  64.         fflush(stderr);
  65.         return;
  66.     }
  67.  
  68.     node_t *new = (node_t *)malloc(sizeof(node_t));
  69.  
  70.     new->data = new_data;
  71.  
  72.     new->next = list->b_node;
  73.     list->b_node = new;
  74. }
  75.  
  76. void print_list(list_t *list)
  77. {
  78.     if(NULL == list ) {
  79.         fprintf(stderr, "You fucked up!\n");
  80.         fflush(stderr);
  81.         return;
  82.     }
  83.     node_t  *current = list->b_node;
  84.  
  85.     while(current != NULL) {
  86.         printf("%d\n", current->data);
  87.         current = current->next;
  88.     }
  89. }
  90.  
  91. void add_end(list_t *list, int new_data)
  92. {
  93.     if(list == NULL) {
  94.         fprintf(stderr, "You fucked up!\n");
  95.         fflush(stderr);
  96.         return;
  97.     }
  98.  
  99.     node_t *new = (node_t *)malloc(sizeof(node_t));
  100.     node_t *it;
  101.  
  102.     new->data = new_data;
  103.     new->next = NULL;
  104.  
  105.     it = list->b_node;
  106.     while(it != NULL) {
  107.         if(it->next == NULL) {
  108.             break;
  109.         }
  110.         it = it->next;
  111.     }
  112.  
  113.     if(list->b_node == NULL) {
  114.        list->b_node = new;
  115.     }
  116.     else {
  117.         it->next = new;
  118.     }
  119. }
  120.  
  121. void search_list(list_t *list, int mark)
  122. {
  123.     if(list == NULL) {
  124.         fprintf(stderr, "You fucked up!\n");
  125.         fflush(stderr);
  126.         return;
  127.     }
  128.  
  129.     node_t *it = list->b_node;
  130.  
  131.     while(it != NULL) {
  132.         if(it->data == mark) {
  133.             break;
  134.         }
  135.         it = it->next;
  136.     }
  137.  
  138.     list->c_node = it;
  139. }
  140.  
  141. void del_after(list_t *list)
  142. {
  143.     if(list == NULL || list->c_node == NULL) {
  144.         fprintf(stderr, "You fucked up!\n");
  145.         fflush(stderr);
  146.         return;
  147.     }
  148.  
  149. }
Add Comment
Please, Sign In to add comment