Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. struct sll_node
  6. {
  7.     int data;
  8.     struct sll_node *next;
  9. };
  10. bool insert_before(struct sll_node **node, int data)
  11. {
  12.     struct sll_node *new_node = (struct sll_node *)malloc(sizeof(struct sll_node));
  13.     if(NULL!=new_node){
  14.         new_node->data = data;
  15.         new_node->next = *node;
  16.         * node = new_node;
  17.         return true;
  18.     }
  19.     return false;
  20. }
  21. bool  insert_back(struct sll_node **node, int data)
  22. {
  23.     *node = (struct sll_node *)malloc(sizeof(struct sll_node));
  24.     if(NULL!= *node){
  25.         (*node)->data = data;
  26.         (*node)->next = NULL;
  27.         return true;
  28.     }
  29.     return false;
  30. }
  31. bool insert_node(struct sll_node **node, int data)
  32. {
  33.     if(NULL!= *node){
  34.         if((*node)->data<=data)
  35.             return insert_before(node, data);
  36.         else
  37.             return insert_node (&(*node)->next, data );
  38.     }
  39.     else
  40.         return insert_back(node, data);
  41. }
  42. void delete_node(struct sll_node **node, int data)
  43. {
  44.     if(NULL!= *node){
  45.         if((*node)->data == data){
  46.                 struct sll_node *next = (*node)->next;
  47.                 free(*node);
  48.                 *node = next;
  49.         }
  50.         else
  51.             delete_node(&(*node)->next, data);
  52.     }
  53. }
  54. void print_list(struct sll_node *node)
  55. {
  56.     if(NULL!=node){
  57.         printf(" %d ", node->data);
  58.         print_list(node->next);
  59.     }
  60.     else
  61.         printf("\n");
  62. }
  63. void print_list_backwards(struct sll_node *node)
  64. {
  65.     if(NULL!=node){
  66.         print_list_backwards(node->next);
  67.         printf(" %d ", node->data);
  68.  
  69.     }
  70. }
  71. void remove_list(struct sll_node **node)
  72. {
  73.     if(NULL!= *node){
  74.         remove_list(&(*node)->next);
  75.         free(*node);
  76.     }
  77. }
  78. void usun(struct sll_node **node)
  79. {
  80.  
  81.     if(NULL!= *node){
  82.         if(NULL!= (*node)->next){
  83.         if((*node)->data == (*node)->next->data){
  84.             usun(&(*node)->next);
  85.         }
  86.         else{
  87.             struct sll_node *tmp = (*node)->next;
  88.             delete_node(node, (*node)->data);
  89.             *node = tmp;
  90.             usun(&(*node)->next);
  91.  
  92.     }
  93.     }
  94.     else {
  95.         delete_node(node, (*node)->next);
  96.     }
  97.  
  98.     }
  99. }
  100.  
  101. int main()
  102. {
  103.     struct sll_node *front = NULL;
  104.     int i;
  105.     for(i=1; i<5; i++)
  106.         if(! insert_node (&front, i))
  107.             fprintf(stderr, " Blad dodawania elementu do listy!\n");
  108.     for(i=6; i<10; i++)
  109.         if(! insert_node (&front, i))
  110.             fprintf(stderr, " Blad dodawania elementu do listy!\n");
  111.     print_list(front);
  112.     printf("\n");
  113.  
  114.     if(! insert_node(&front, 5))
  115.         fprintf (stderr , " Blad dodawania elementu do listy!\n");
  116.     if(! insert_node(&front, 7))
  117.         fprintf (stderr , " Blad dodawania elementu do listy!\n");
  118.     print_list(front);
  119.  
  120.     usun(&front);
  121.     puts("");
  122.     print_list(front);
  123.  
  124.     remove_list(&front);
  125.     front = NULL;
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement