SHARE
TWEET

linkedlist heheh

Mary_99 Jun 12th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. struct Node{
  6.     const char *txt;
  7.     struct Node *next;
  8. };
  9.  
  10. void handle_error(const char *txt);
  11. struct Node *create_new_element(const char *txt, struct Node *next);
  12. void swap_elements(struct Node *a, struct Node *b);
  13.  
  14. struct Node *init();
  15. int length(struct Node *head);
  16. void display(struct Node *head);
  17. void push(struct Node **head, const char *txt);
  18. const char *pop(struct Node **head);
  19. void destroy(struct Node **head_ptr);
  20. void append(struct Node **head, const char *txt);
  21. struct Node *copy(struct Node *head);
  22. void reverse(struct Node **head);
  23. void sort(struct Node *head);
  24. ///------------------------------------------------------------
  25.  
  26.  
  27.  
  28. void handle_error(const char *txt){
  29.     printf("%s\n", txt);
  30.     exit(0);
  31. }
  32.  
  33.  
  34. struct Node *create_new_element(const char *txt, struct Node *next){
  35.     struct Node *new_element = malloc(sizeof (struct Node));
  36.     if(new_element == NULL){
  37.         handle_error("Failed to create a new element");
  38.     }
  39.     new_element -> txt = txt;
  40.     new_element -> next = next;
  41.    
  42.     return new_element;
  43. }
  44.  
  45.  
  46. struct Node *init(){
  47.     return NULL;
  48. }
  49.  
  50.  
  51. void push(struct Node **head, const char *txt){
  52.     struct Node *new_node = create_new_element(txt, *head);
  53.     *head = new_node;
  54. }
  55.  
  56.  
  57. int length(struct Node *head){
  58.     int res = 0;
  59.     struct Node *current_element = head;
  60.     while(current_element != NULL){
  61.         res += 1;
  62.         current_element = current_element -> next;
  63.     }
  64.     return res;
  65. }
  66.  
  67.  
  68. void display(struct Node *head){
  69.     printf("length: %d\n", length(head));
  70.     if(length(head) == 0 ){
  71.         printf("the list is empty\n\n");
  72.         return;
  73.     }
  74.     struct Node *current_element = head;
  75.     printf("elements of the list:--------------\n");
  76.     while(current_element != NULL){
  77.         printf("%s\n", current_element -> txt);
  78.         current_element = current_element -> next;
  79.     }
  80.     printf("-----------------------------------\n\n");
  81. }
  82.  
  83.  
  84. const char *pop(struct Node **head_ptr){
  85.     struct Node *previous = NULL;
  86.     struct Node *current_element = *head_ptr;
  87.    
  88.     while(current_element -> next != NULL){// i go to the end
  89.         previous = current_element;
  90.         current_element = current_element -> next;
  91.     }
  92.    
  93.     if(previous != NULL){//remove connection
  94.         previous -> next = NULL;
  95.     }
  96.    
  97.     const char *buf = current_element -> txt;
  98.     free(current_element);
  99.     return buf;
  100. }
  101.  
  102. void destroy(struct Node **head_ptr){
  103.     while (*head_ptr){
  104.         struct Node *t = (*head_ptr) -> next;
  105.         free (*head_ptr);
  106.         *head_ptr = t;
  107.     };
  108.     *head_ptr = NULL;
  109. }
  110.  
  111.  
  112. void append(struct Node **head_ptr, const char *txt){
  113.     struct Node *current_element = *head_ptr;
  114.    
  115.     struct Node *new_element = create_new_element(txt, NULL);
  116.     if((*head_ptr) == NULL){
  117.         *head_ptr = new_element;
  118.         return;
  119.     }
  120.    
  121.     while(current_element -> next != NULL){
  122.         current_element = current_element -> next; 
  123.     }
  124.    
  125.     current_element -> next = new_element;
  126. }
  127.  
  128.  
  129. struct Node *copy(struct Node *head){
  130.     struct Node *new_head = NULL;
  131.     struct Node **current_element = &new_head;
  132.    
  133.     while (head){
  134.         *current_element = create_new_element(head -> txt, NULL);
  135.         head = head -> next;
  136.         current_element = &((*current_element)->next);
  137.     }
  138.     return new_head;
  139. }
  140.  
  141.  
  142. void reverse(struct Node **head){
  143.     struct Node *previous = NULL;
  144.     struct Node *current_element = *head;
  145.     struct Node *next_element;
  146.    
  147.     while(current_element != NULL){
  148.         next_element = current_element -> next;
  149.         current_element -> next = previous;
  150.         previous = current_element;
  151.         current_element = next_element;
  152.     }
  153.     *head = previous;
  154. }
  155.  
  156. void swap_elements(struct Node *a, struct Node *b){
  157.     const char *buf = a -> txt;
  158.     a -> txt = b->txt;
  159.     b -> txt = buf;
  160. }
  161.  
  162.  
  163. void sort(struct Node *head){
  164.     if(head -> next == NULL)
  165.         return;
  166.    
  167.     int not_sorted;
  168.     struct Node *current_element;
  169.    
  170.     do{
  171.         not_sorted = 0;
  172.         current_element = head;
  173.        
  174.         while(current_element -> next != NULL){
  175.             if(strcmp(current_element -> txt, current_element -> next -> txt) > 0){
  176.                 swap_elements(current_element, current_element -> next);
  177.                 not_sorted = 1;
  178.             }
  179.             current_element = current_element -> next;
  180.         }
  181.     }while(not_sorted);
  182.    
  183. }
  184.  
  185.  
  186.  
  187. int main(){
  188.    
  189.     struct Node *myList = init();
  190.    
  191.     display(myList);
  192.    
  193.     printf("Appending to myList\n");
  194.     append(&myList, "jeden");
  195.     display(myList);
  196.    
  197.     append(&myList, "dwa");
  198.     display(myList);
  199.    
  200.     append(&myList, "trzy");
  201.     display(myList);
  202.    
  203.     append(&myList, "cztery");
  204.     display(myList);
  205.    
  206.     printf(" pushing to myList\n");
  207.     push(&myList, "zero");
  208.     display(myList);
  209.    
  210.     push(&myList, "minus jeden");
  211.     display(myList);
  212.    
  213.  
  214.     printf("Copying myList to otherList\n");
  215.     struct Node *otherList = copy(myList);
  216.     printf("otherList:\n");
  217.     display(otherList);
  218.    
  219.    
  220.     printf("Reversing myList\n");
  221.     reverse(&myList);
  222.     display(myList);
  223.    
  224.    
  225.     printf("sorting myList\n");
  226.     sort(myList);
  227.     display(myList);
  228.    
  229.    
  230.     printf("############################################## popping el. of myList\n");
  231.     pop(&myList);
  232.     display(myList);
  233.    
  234.     printf("destroying myList\n");
  235.     destroy(&myList);
  236.     display(myList);
  237.    
  238.     printf("otherList:\n");
  239.     display(otherList);
  240.    
  241.     printf(" destroying otherList\n");
  242.     destroy(&otherList);
  243.     display(otherList);
  244.    
  245.     return 0;
  246. }
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