SHARE
TWEET

linkedList

Mary_99 Jun 11th, 2019 53 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.  
  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 appendNode(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.  
  22. int main (int argc, char **argv)
  23. {
  24.  
  25.     node* list = init();
  26.     push(&list, "ONE");
  27.     appendNode(&list, "TWO");
  28.     display(list);
  29.     printf("\n");
  30.    
  31.     pop(&list);
  32.     reverse(&list);
  33.     node *duplicate = copy(list);
  34.     freeList(&list);
  35.     display(list);
  36.     printf("\n");
  37.    
  38.     display(duplicate);
  39.     printf("\n");
  40.    
  41.     sort(&duplicate);
  42.     display(duplicate);
  43.  
  44.   return(0);
  45. }
  46.  
  47. node* init()//nitializes empty list, returns pointer to newly created list,
  48. {
  49.     node *head = NULL;
  50.     node *second = NULL;
  51.     node *third = NULL;
  52.     head = malloc(sizeof(node));
  53.     second = malloc(sizeof(node));
  54.     third = malloc(sizeof(node));
  55.     head->data = "AAAAAAAAAA";
  56.     head->next = second;
  57.     second->data ="BBBBBBBB";
  58.     second->next = third;
  59.     third->data = "CCCCCCCCCC";
  60.     third->next = NULL;
  61.     return head;
  62. }
  63.  
  64. int length(node* head)// returns number of elements stores on the list identified by parameter
  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)//– shows all elements stored on the list identified by a parameter
  77. {
  78.     node *currentPosition = head;
  79.     if(length(head) == 0)
  80.     {
  81.         printf("NO ELEMNETS\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. void push(node**headPtr, char *data)// inserts new element at the beginning on the list identified by paramete
  94. {
  95.     node *newNode = malloc(sizeof(node));
  96.     newNode->data = data;
  97.     newNode->next = *headPtr;
  98.     *headPtr = newNode;
  99. }
  100.  
  101. void pop(node**headPtr)//removes first element from the end on the list identified by parameter and return storedvalue
  102. {
  103.     node*newNode;
  104.     newNode = *headPtr;
  105.     (*headPtr) = (*headPtr)->next;
  106.     newNode->next = NULL;
  107.     free(newNode);
  108. }
  109.  
  110. void freeList(node** headPtr)// destroys the list identified by parameter
  111. {
  112.     while(*headPtr)
  113.     {
  114.         freeList(&((*headPtr)->next));
  115.         free(*headPtr);
  116.         *headPtr = NULL;
  117.     }
  118. }
  119.  
  120. void appendNode(node**headRef, char *data)// to the end of the list insert element
  121. {
  122.     node* currentPosition = *headRef;
  123.     node* newNode;
  124.     newNode = malloc(sizeof(node));
  125.     newNode->data = data;
  126.     newNode->next = NULL;
  127.     if(currentPosition == NULL)
  128.     {
  129.         *headRef = newNode;
  130.     }
  131.     else
  132.     {
  133.         while(currentPosition->next !=NULL)
  134.         {
  135.             currentPosition = currentPosition->next;
  136.         }
  137.         currentPosition->next = newNode;
  138.     }
  139. }
  140.  
  141. node* copy(node* source)// creates copy of the list identified by parameter
  142. {
  143.     node* head = NULL;
  144.     node** temp = &head;
  145.     while(source)
  146.     {
  147.         *temp = malloc(sizeof(node));
  148.         (*temp)->data = source->data;
  149.         (*temp)->next = NULL;
  150.         source = source->next;
  151.         temp = &((*temp)->next);
  152.     }
  153.     return head;
  154. }
  155.  
  156. void reverse(node **headPtr)//reverses order of elements of the list identified by parameter
  157. {
  158.     node* currentNode = *headPtr;
  159.     node* previous = NULL;
  160.     node* after = NULL;
  161.     while(currentNode != NULL)
  162.     {
  163.         after = currentNode->next;
  164.         currentNode->next = previous;
  165.         previous = currentNode;
  166.         currentNode = after;
  167.     }
  168.     *headPtr = previous;
  169. }
  170. void sort(node **headPtr)//sort elements of the list identified by parameter
  171. {
  172.     int swapped;
  173.     node*newNode;
  174.     node* lastNode = NULL;
  175.  
  176.         do
  177.         {
  178.             swapped = 0;
  179.             newNode = *headPtr;
  180.             while(newNode->next != lastNode)
  181.             {
  182.                 char* firstString = newNode->data;
  183.                 char * secondString = newNode->next->data;
  184.                 if(strcmp(firstString,secondString) == 1)
  185.                 {
  186.                     char * temporaryString = newNode->data;
  187.                     newNode->data = newNode->next->data;
  188.                     newNode->next->data = temporaryString;
  189.                     swapped = 1;
  190.                 }
  191.                 newNode = newNode->next;
  192.             }
  193.             lastNode = newNode;
  194.         }
  195.         while(swapped);
  196.     }
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