daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct list_node
  5. {
  6.     int data;
  7.     struct list_node *next;
  8. };
  9.  
  10. struct list_node *add_first(struct list_node *pointer_list,int liczba)
  11. {
  12.     struct list_node *new_node = (struct list_node*)malloc(sizeof(struct list_node));
  13.     if(new_node)
  14.     {
  15.         new_node->data = liczba;
  16.         new_node->next = pointer_list;
  17.         pointer_list= new_node;
  18.     }
  19.  
  20.     return pointer_list;
  21. }
  22.  
  23. struct list_node *add_at_last(struct list_node *pointer_list, struct list_node *new_node)
  24. {
  25.     new_node->next = pointer_list;
  26.  
  27.  
  28.     return new_node;
  29. }
  30.  
  31. struct list_node *looking_for_prev(struct list_node *pointer_list, int liczba)
  32. {
  33.     struct list_node* prev = NULL;
  34.     while(pointer_list && pointer_list->data > liczba)
  35.     {
  36.         prev = pointer_list;
  37.         pointer_list = pointer_list->next;
  38.     }
  39.     return prev;
  40. }
  41.  
  42. void add_in_middle_or_back(struct list_node *prev, struct list_node *new_node)
  43. {
  44.     new_node->next=prev->next;
  45.     prev->next = new_node;
  46. }
  47.  
  48. struct list_node *add_node(struct list_node *pointer_list,int liczba)
  49. {
  50.     struct list_node *new_node = (struct list_node*)malloc(sizeof(struct list_node));
  51.  
  52.     if(new_node && pointer_list)
  53.     {
  54.         new_node->data=liczba;
  55.         if(pointer_list->data < liczba)
  56.         {
  57.             return add_at_last(pointer_list,new_node);
  58.         }else
  59.         {
  60.             struct list_node *prev = looking_for_prev(pointer_list,liczba);
  61.             add_in_middle_or_back(prev,new_node);
  62.         }
  63.     }
  64.     return pointer_list;
  65. }
  66.  
  67. struct list_node *delete_front(struct list_node *pointer_list)
  68. {
  69.     struct list_node *tmp = pointer_list->next;
  70.     free(pointer_list);
  71.     return tmp;
  72. }
  73.  
  74.  
  75. struct list_node *looking_for_prev_delete(struct list_node *pointer_list, int liczba)
  76. {
  77.     struct list_node *prev = NULL;
  78.     while(pointer_list && pointer_list->data != liczba)
  79.     {
  80.         prev = pointer_list;
  81.         pointer_list=pointer_list->next;
  82.     }
  83.     return prev;
  84. }
  85.  
  86. void delete_middle_or_back(struct list_node *prev)
  87. {
  88.     struct list_node *tmp = prev->next;
  89.     prev->next=tmp->next;
  90.     free(tmp);
  91. }
  92.  
  93.  
  94.  
  95. struct list_node *delete_node(struct list_node *pointer_list,int liczba)
  96. {
  97.     if(pointer_list)
  98.     {
  99.         if(pointer_list->data == liczba)
  100.         {
  101.             return delete_front(pointer_list);
  102.         }else
  103.         {
  104.             struct list_node *prev = looking_for_prev_delete(pointer_list,liczba);
  105.             delete_middle_or_back(prev);
  106.  
  107.         }
  108.     }
  109.     return pointer_list;
  110. }
  111.  
  112. void print_list(struct list_node *pointer_list)
  113. {
  114.     while(pointer_list)
  115.     {
  116.         printf("%d ",pointer_list->data);
  117.         pointer_list=pointer_list->next;
  118.     }
  119.     puts("");
  120. }
  121.  
  122. void remove_all(struct list_node **pointer_list)
  123. {
  124.     struct list_node *tmp=(*pointer_list)->next;
  125.     free(*pointer_list);
  126.     *pointer_list = tmp;
  127. }
  128.  
  129. int main()
  130. {
  131.     struct list_node *pointer_list = NULL;
  132.  
  133.  
  134.     pointer_list = add_first(pointer_list,2);
  135.     print_list(pointer_list);
  136.  
  137.     pointer_list = add_node(pointer_list,0);
  138.     pointer_list = add_node(pointer_list,3);
  139.     pointer_list = add_node(pointer_list,7);
  140.     pointer_list = add_node(pointer_list,8);
  141.     print_list(pointer_list);
  142.  
  143.     pointer_list = add_node(pointer_list,4);
  144.     pointer_list = add_node(pointer_list,5);
  145.     pointer_list = add_node(pointer_list,6);
  146.     print_list(pointer_list);
  147.  
  148.     pointer_list = delete_node(pointer_list,0);
  149.     print_list(pointer_list);
  150.  
  151.  
  152.  
  153.  
  154.     while(pointer_list)
  155.     {
  156.         remove_all(&pointer_list);
  157.     }
  158.  
  159.  
  160.     return 0;
  161. }
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