Advertisement
Guest User

Wzor

a guest
Apr 4th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 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.  
  79. int main()
  80. {
  81.     struct sll_node *front = NULL;
  82.     int i;
  83.     for(i=1; i<5; i++)
  84.         if(! insert_node (&front, i))
  85.             fprintf(stderr, " Error while inserting %d!\n", i);
  86.     for(i=6; i<10; i++)
  87.         if(! insert_node (&front, i))
  88.             fprintf(stderr, " Error while inserting %d!\n", i);
  89.     printf("List elements:\n");
  90.     print_list(front);
  91.     printf("List elements printed backwards:\n");
  92.     print_list_backwards(front);
  93.     printf("\n");
  94.  
  95.     if(! insert_node(&front, 0))
  96.         fprintf (stderr , " Error while inserting 0!\n");
  97.     printf("List elements after insertion of 0:\n");
  98.     print_list(front);
  99.     if(! insert_node(&front, 5))
  100.         fprintf (stderr , " Error while inserting 5!\n");
  101.     printf("List elements after insertion of 5:\n");
  102.     print_list(front);
  103.     if(! insert_node(&front, 7))
  104.         fprintf (stderr , " Error while inserting 7!\n");
  105.     printf("List elements after insertion of 7:\n");
  106.     print_list(front);
  107.     if(! insert_node(&front, 10))
  108.         fprintf (stderr , " Error while inserting 10!\n");
  109.     printf("List elements after insertion of 10:\n");
  110.     print_list(front);
  111.  
  112.     delete_node(&front, 0);
  113.     printf("List elements after deletion of 0:\n");
  114.     print_list(front);
  115.     delete_node(&front, 1);
  116.     printf("List elements after deletion of 1:\n");
  117.     print_list(front);
  118.     delete_node(&front, 1);
  119.     printf("List elements after deletion of 1:\n");
  120.     print_list(front);
  121.     delete_node(&front, 5);
  122.     printf("List elements after deletion of 5:\n");
  123.     print_list(front);
  124.     delete_node(&front, 7);
  125.     printf("List elements after deletion of 7:\n");
  126.     print_list(front);
  127.     delete_node(&front, 10);
  128.     printf("List elements after deletion of 10:\n");
  129.     print_list(front);
  130.  
  131.     remove_list(&front);
  132.     front = NULL;
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement