Advertisement
Monika__

linked list-test push not working

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