Advertisement
namagaii

Untitled

Aug 15th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /*  Linked list struct stores the length of the string, the string(up to 4 chars), and the next node in the list */
  5. typedef struct listnode listnode;
  6. struct listnode {
  7.     int length;
  8.     char *val;
  9.     listnode *next;
  10. };
  11.  
  12. listnode linkedlist[10];
  13. listnode head;
  14. listnode el1;
  15. listnode el2;
  16. listnode el3;
  17. listnode el4;
  18. listnode el5;
  19. listnode el6;
  20. listnode el7;
  21. listnode el8;
  22. listnode el9;
  23. static char newstrings[10][10];
  24.  
  25. void convert_words_to_list();
  26.  
  27. void init_listnode(int listNodeIndex, int len, char *val);
  28.  
  29. void get_words(char *line);
  30.  
  31. void sort_list_alphabetic();
  32.  
  33. void print_list();
  34.  
  35. void merge_sort(listnode **source);
  36.  
  37. listnode* merge_and_sort(listnode *a, listnode *b);
  38.  
  39. char tolowercase(char ch);
  40.  
  41. int compare_words(char *str1, char *str2);
  42.  
  43. void split(listnode *source, listnode **frontRef, listnode **backRef);
  44.  
  45. int main(int argc, char **argv)
  46. {
  47.     linkedlist[0] = head;
  48.     linkedlist[1] = el1;
  49.     linkedlist[2] = el2;
  50.     linkedlist[3] = el3;
  51.     linkedlist[4] = el4;
  52.     linkedlist[5] = el5;
  53.     linkedlist[6] = el6;
  54.     linkedlist[7] = el7;
  55.     linkedlist[8] = el8;
  56.     char str[1024];
  57.     printf("Input a sentence please.\n");
  58.     scanf("%1023[^\n]", str);
  59.     get_words(str);
  60.     convert_words_to_list();
  61.     listnode *a = &linkedlist[0];
  62.     merge_sort(&a);
  63.     print_list();
  64.     return 0;
  65. }
  66.  
  67. /* converts a line from the console into a string */
  68. void convert_words_to_list(){
  69.     int i = 0;
  70.     for(i = 0; i<10;i++){
  71.         char *word = newstrings[i];
  72.         init_listnode(i, strlen(word), word);
  73.     }
  74. }
  75.  
  76. void get_words(char *line){
  77.     int i = 0;
  78.     int j = 0;
  79.     int ctr =0;
  80.     for (i=0;i<=(strlen(line));i++){
  81.         /*If space or termination character is found do skip*/
  82.         if(line[i]==' '||line[i]=='|'){
  83.             ctr++;
  84.             j=0;
  85.             continue;
  86.         }
  87.         else if (j < 4)
  88.         {
  89.             newstrings[ctr][j]=line[i];
  90.             j++;
  91.         }
  92.     }
  93. }
  94.  
  95. void init_listnode(int listNodeIndex, int len, char *val){
  96.     linkedlist[listNodeIndex].length = len;
  97.     linkedlist[listNodeIndex].val = val;
  98.     if (listNodeIndex > 0){
  99.         linkedlist[listNodeIndex - 1].next = &linkedlist[listNodeIndex];
  100.     }
  101. }
  102.  
  103. void print_list(){
  104.     int i = 0;
  105.     listnode *current = &linkedlist[0];
  106.     while(current->next != NULL){
  107.         printf("Index: %d Length: %d Value: %s\n", i, current->length, current->val);
  108.         current = current->next;
  109.         i++;
  110.     }
  111. }
  112.  
  113. void merge_sort(listnode **source){
  114.     listnode *head = *source;
  115.     listnode *a;
  116.     listnode *b;
  117.  
  118.     if (head == NULL || head->next == NULL){
  119.         return;
  120.     }
  121.  
  122.     split(head, &a, &b);
  123.  
  124.     *source = merge_and_sort(a, b);
  125. }
  126.  
  127. listnode* merge_and_sort(listnode *a, listnode *b){
  128.     listnode * result = NULL;
  129.    
  130.     /* Recursion exit cases */
  131.     if (a == NULL){
  132.         return b;
  133.     } else if (b == NULL){
  134.         return a;
  135.     }
  136.  
  137.     /* Sort by criteria */
  138.     if (compare_words(a->val, b->val) == 1){
  139.         result = a;
  140.         result->next = merge_and_sort(a->next, b);
  141.     } else {
  142.         result = b;
  143.         result->next = merge_and_sort(b->next, a);
  144.     }
  145.     return result;
  146. }
  147.  
  148. /* Takes char as argument if its uppercase make it lowercase and return char otherwise do nothing and return the char */
  149. char tolowercase(char ch){
  150.     /* if is uppercase make lowercase*/
  151.     if (ch >= 'A' && ch <= 'Z'){
  152.         return ch + 32;
  153.     } else {
  154.         return ch;
  155.     }
  156. }
  157.  
  158. /*
  159. *    Finds the greater of 1 and 2
  160. *    if 1 return 1
  161. *    if 2 return 0
  162. */
  163. int compare_words(char *str1, char *str2){
  164.     int i = 0;
  165.     for (i = 0; i < strlen(str1); i++){
  166.         //if all chars are the same the shorter one goes first
  167.         if (i >= strlen(str2)){
  168.             return 0;
  169.         }
  170.         // If not same do something otherwise do nothing
  171.         if (tolowercase(str1[i]) != tolowercase(str2[i])){
  172.             // The lesser char is higher in alphabetical order return true if str1 otherwise false
  173.             if (tolowercase(str1[i]) < tolowercase(str2[i])){
  174.                 return 1;
  175.             } else {
  176.                 return 0;
  177.             }
  178.         }
  179.     }
  180.     // if all chars are the same the shorter one goes first
  181.     return 1;
  182. }
  183.  
  184. void split(listnode *source, listnode **frontRef, listnode **backRef){
  185.     listnode *fast;
  186.     listnode *slow;
  187.     slow = source;
  188.     fast = source->next;
  189.  
  190.     while(fast != NULL){
  191.         fast = fast->next;
  192.         if (fast != NULL){
  193.             slow = slow->next;
  194.             fast = fast->next;
  195.         }
  196.     }
  197.     *frontRef = source;
  198.     *backRef = slow->next;
  199.     slow->next = NULL;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement