SHARE
TWEET

LINKED LIST DEEP COPY DO ODDANIa

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