SHARE
TWEET

linkeddddddd

Mary_99 Jun 12th, 2019 78 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. int length(node* head);
  15. void display(node* head);
  16. void push(node**headPtr, char *data);
  17. void freeList(node** head);
  18. void appendNode(node**headRef, char *data);
  19. node* copy(node* source);
  20. void pop(node**headPtr);
  21. void reverse(node **headPtr);
  22. void sort(node **headPtr);
  23. void handleError(const char *data);
  24. struct node *createNewElement(char *data, struct node *next);
  25.  
  26. int main (int argc, char **argv)
  27. {
  28.  
  29.     node* list = init();
  30.    
  31.   //  char* text = malloc;
  32.   //  push(&list, text);
  33.   //  free(text);
  34.        
  35.    
  36.     push(&list, "ONE");
  37.    
  38.    
  39.     appendNode(&list, "TWO");
  40.     display(list);
  41.     printf("\n");
  42.     pop(&list);
  43.     reverse(&list);
  44.     node *duplicate = copy(list);
  45.     freeList(&list);
  46.     display(list);
  47.     printf("\n");
  48.    
  49.     display(duplicate);
  50.     printf("\n");
  51.    
  52.     sort(&duplicate);
  53.     display(duplicate);
  54.     freeList(&duplicate);
  55.  
  56.   return(0);
  57. }
  58.  
  59. node* init()
  60. {
  61.     return NULL;
  62. }
  63.  
  64. int length(node* head)
  65. {
  66.     node *currentPosition = head;
  67.     int count = 0;
  68.     while(currentPosition != NULL)
  69.     {
  70.         count++;
  71.         currentPosition = currentPosition->next;
  72.     }
  73.     return count;
  74. }
  75.  
  76. void display(node* head)
  77. {
  78.     node *currentPosition = head;
  79.     if(length(head) == 0)
  80.     {
  81.         printf("NO ELEMNETS TO DISPALAY \n");
  82.     }
  83.     else
  84.     {
  85.     while(currentPosition != NULL)
  86.         {
  87.             printf("%s\n", currentPosition->data);
  88.             currentPosition = currentPosition->next;
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94. void handleError(const char *data)
  95. {
  96.     printf("%s\n", data);
  97.     exit(0);
  98. }
  99.  
  100.  
  101. struct node *createNewElement(char *data, struct node *next)
  102. {
  103.     struct node *newElement = malloc(sizeof (struct node));
  104.     if(newElement == NULL)
  105.     {
  106.         handleError("CANT CREATE A NEW ELEMNT");
  107.     }
  108.     newElement -> data = data;// here is a prblem to solve
  109.     newElement -> next = next;
  110.    
  111.     return newElement;
  112. }
  113.  
  114.  
  115. void push(node**headPtr, char *data)
  116. {
  117.     struct node *newNode = createNewElement(data, *headPtr);
  118.     *headPtr = newNode;
  119. }
  120.  
  121. void pop(node**headPtr)
  122. {
  123.     node*newNode;
  124.     newNode = *headPtr;
  125.     (*headPtr) = (*headPtr)->next;
  126.     newNode->next = NULL;
  127.     free(newNode);
  128. }
  129.  
  130. void freeList(node** headPtr)
  131. {
  132.     while(*headPtr)
  133.     {
  134.         freeList(&((*headPtr)->next));
  135.         free(*headPtr);
  136.         *headPtr = NULL;
  137.     }
  138. }
  139.  
  140. void appendNode(node**headRef, char *data)
  141. {
  142.  
  143.     struct node *currentElement = *headRef;
  144.    
  145.     struct node *newElement = createNewElement(data, NULL);
  146.     if((*headRef) == NULL)
  147.     {
  148.         *headRef = newElement;
  149.         return;
  150.     }
  151.    
  152.     while(currentElement -> next != NULL)
  153.     {
  154.         currentElement = currentElement -> next;
  155.     }
  156.    
  157.     currentElement -> next = newElement;
  158. }
  159.  
  160.  
  161.  
  162. node* copy(node* source)
  163. {
  164.     node* head = NULL;
  165.     node** temp = &head;
  166.     while(source)
  167.     {
  168.         *temp = malloc(sizeof(node));
  169.         (*temp)->data = source->data;
  170.         (*temp)->next = NULL;
  171.         source = source->next;
  172.         temp = &((*temp)->next);
  173.     }
  174.     return head;
  175.    
  176.  
  177. /**node current = source;
  178. *node headOfNewList = NULL;
  179. *node lastNodeInNewList = NULL;
  180.  
  181. while(current!= NULL)
  182. {
  183.     if(headOfNewList == NULL)
  184.     {
  185.         headOfNewList = (node*)malloc(sizeof(node));
  186.         headOfNewListent->data = current->data;
  187.         headOfNewList->next = NULL;
  188.         lastNodeInNewList = headOfNewList;
  189.     }
  190.     else
  191.     {
  192.         lastNodeInNewList->next = ( node*)malloc(sizeof( node));
  193.         lastNodeInNewList = lastNodeInNewList->next;
  194.         lastNodeInNewList->data = current->data;
  195.         lastNodeInNewList->next = NULL;
  196.     }
  197.     current = current->next;
  198. }
  199.  
  200.     return  headOfNewList;
  201. }*/
  202.    
  203.  
  204. void sort(node **headPtr)
  205. {
  206.     bool swapped;
  207.     node*newNode;
  208.     node* lastNode = NULL;
  209.  
  210.     do
  211.     {
  212.         swapped = false;
  213.         newNode = *headPtr;
  214.         while(newNode->next != lastNode)
  215.         {
  216.             char* firstString = newNode->data;
  217.             char * secondString = newNode->next->data;
  218.             if(strcmp(firstString,secondString) > false)
  219.             {
  220.                 char * temporaryString = newNode->data;
  221.                 newNode->data = newNode->next->data;
  222.                 newNode->next->data = temporaryString;
  223.                 swapped = true;
  224.             }
  225.             newNode = newNode->next;
  226.         }
  227.         lastNode = newNode;
  228.     }
  229.         while(swapped);
  230. }
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