Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 7th, 2020 163 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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((*node)->data == (*node)->next->data){
  83.             usun(&(*node)->next);
  84.         }
  85.         else{
  86.             struct sll_node *tmp = (*node)->next->next;
  87.             delete_node((*node)->next, tmp);
  88.             (*node)->next = tmp;
  89.             usun(&(*node)->next);
  90.     }
  91.     }
  92.  
  93. }
  94.  
  95. int main()
  96. {
  97.     struct sll_node *front = NULL;
  98.     int i;
  99.     for(i=1; i<5; i++)
  100.         if(! insert_node (&front, i))
  101.             fprintf(stderr, " Blad dodawania elementu do listy!\n");
  102.     for(i=6; i<10; i++)
  103.         if(! insert_node (&front, i))
  104.             fprintf(stderr, " Blad dodawania elementu do listy!\n");
  105.     print_list(front);
  106.     printf("\n");
  107.  
  108.     if(! insert_node(&front, 5))
  109.         fprintf (stderr , " Blad dodawania elementu do listy!\n");
  110.     if(! insert_node(&front, 7))
  111.         fprintf (stderr , " Blad dodawania elementu do listy!\n");
  112.     print_list(front);
  113.  
  114.     usun(&front);
  115.     puts("");
  116.     print_list(front);
  117.  
  118.     remove_list(&front);
  119.     front = NULL;
  120.     return 0;
  121. }
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