SHARE
TWEET

linked roboczo deepcopy

Mary_99 Jun 17th, 2019 15 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. typedef struct node{
  8.     char *data;
  9.     struct node *next;
  10. }node;
  11.  
  12. node *init();
  13. node *allocate();
  14.  
  15. int lengthMeasure(node* head);
  16. void display(node* head);
  17. void push(node**head, char *data);
  18. void destroy(node **head);
  19. void appendNode(node**head, char *data);
  20. node *copy(node *src);
  21. char *pop(node**head);
  22. void reverse(node **head);
  23. void swapData(node **newNode);
  24. void sort(node **head);
  25.  
  26. int main (int argc, char **argv)
  27. {
  28.     node *list = init();
  29.     push(&list, "Word");
  30.     push(&list, "4");
  31.     push(&list, "6");
  32.     push(&list, "4");
  33.     push(&list, "7");
  34.     appendNode(&list, "5");
  35.     display(list);
  36.     printf("Length: %d\n", lengthMeasure(list));
  37.     printf("Popped value: %s\n", pop(&list));
  38.     display(list);
  39.     reverse(&list);
  40.     node *duplicate = copy(list);
  41.     printf("Destroy\n");
  42.     destroy(&list);
  43.     display(list);
  44.     display(duplicate);
  45.     sort(&duplicate);
  46.     display(duplicate);
  47.     destroy(&duplicate);
  48.     return(0);
  49. }
  50.  
  51. node *init()
  52. {
  53.     return NULL;
  54. }
  55.  
  56. node *allocate()
  57. {
  58.     node *head = malloc(sizeof(node));
  59.     return head;
  60. }
  61.  
  62.  
  63. int lengthMeasure(node *head)
  64. {
  65.     node *position = head;
  66.     int count = 0;
  67.     while(position != NULL)
  68.     {
  69.         count++;
  70.         position = position->next;
  71.     }
  72.     return count;
  73. }
  74.  
  75. void display(node *head)
  76. {
  77.     node *position = head;
  78.     if(head == NULL){
  79.         printf("Lack of elements\n");
  80.     }
  81.     else
  82.     {
  83.     while(position != NULL)
  84.         {
  85.             printf("%s ", position->data);
  86.             position = position->next;
  87.         }
  88.         printf("\n");
  89.     }
  90. }
  91.  
  92. void push(node **head, char *data)
  93. {
  94.     struct node *newNode = malloc (sizeof (struct node));
  95.     newNode->data = malloc(sizeof(data));
  96.     strcpy(newNode->data, data);
  97.     newNode->next = *head;
  98.     *head = newNode;
  99. }
  100.  
  101. char *pop(node **head)
  102. {
  103.     char *temp = (*head)->data;
  104.     node *newNode = *head;
  105.     (*head) = (*head)->next;
  106.     newNode->next = NULL;
  107.     free(newNode->data);
  108.     free(newNode);
  109.     return temp;
  110. }
  111.  
  112. void destroy(node **head)
  113. {
  114.     if(*head != NULL)
  115.     {
  116.         destroy(&(*head)->next);
  117.         free((*head)->data);
  118.         (*head)->data = NULL;
  119.         free(*head);
  120.         *head = NULL;
  121.     }
  122. }
  123.  
  124. void appendNode(node **head, char *data)
  125. {
  126.     node *position = *head;
  127.     node *newNode;
  128.     newNode = allocate();
  129.     newNode->data = malloc(sizeof(data));
  130.     strcpy(newNode->data, data);
  131.     newNode->next = NULL;
  132.     if(position == NULL)
  133.     {
  134.         *head = newNode;
  135.     }
  136.     else
  137.     {
  138.         while(position->next !=NULL)
  139.         {
  140.             position = position->next;
  141.         }
  142.         position->next = newNode;
  143.     }
  144. }
  145.  
  146. node *copy(node *src)
  147. {
  148.     node *head = NULL;
  149.     node **dst = &head;
  150.     while(src)
  151.     {
  152.         *dst = allocate();
  153.         (*dst)->data = malloc(sizeof(src->data));
  154.         strcpy((*dst)->data, src->data);
  155.         (*dst)->next = NULL;
  156.         src = src->next;
  157.         dst = &((*dst)->next);
  158.     }
  159.     return head;
  160. }
  161.  
  162. void reverse(node **head)
  163. {
  164.     node *currentNode = *head;
  165.     node *previous = NULL;
  166.     node *after = NULL;
  167.     while(currentNode != NULL)
  168.     {
  169.         after = currentNode->next;
  170.         currentNode->next = previous;
  171.         previous = currentNode;
  172.         currentNode = after;
  173.     }
  174.     *head = previous;
  175. }
  176.  
  177. void swapData(node **newNode)
  178. {
  179.         char *temporaryString = (*newNode)->data;
  180.         (*newNode)->data = (*newNode)->next->data;
  181.         (*newNode)->next->data = temporaryString;
  182. }
  183.  
  184. void sort(node **head)
  185. {
  186.     bool swapped;
  187.     node *newNode;
  188.     node *lastNode = NULL;
  189.     do
  190.     {
  191.         swapped = false;
  192.         newNode = *head;
  193.         while(newNode->next != lastNode)
  194.         {
  195.             if(strcmp(newNode->data, newNode->next->data) >= 0)
  196.             {
  197.                 swapData(&newNode);
  198.                 swapped = true;
  199.             }
  200.             newNode = newNode->next;
  201.         }
  202.         lastNode = newNode;
  203.     }while(swapped);
  204. }
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