Advertisement
Idanref

Untitled

Mar 24th, 2021
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // To add:
  6. // Support for A-Z && a-z  - FIXED
  7. // Insert function: insert node only if alphabet letters - fix: main() will not enter values unless it's a letter
  8. // Double pointer instead of return values
  9. // Make insert() shorter
  10.  
  11. typedef struct Node
  12. {
  13.     char data;
  14.     struct Node* next;
  15. } node;
  16.  
  17. node *create_node(char data)
  18. {
  19.     node* temp = (node*)malloc(sizeof(node));
  20.  
  21.     temp->data = data;
  22.     temp->next = NULL;
  23.  
  24.     return temp;
  25. }
  26.  
  27. char lower(char c)
  28. {
  29.     if (c >= 'A' && c <= 'Z')
  30.     {
  31.         return c + 32;
  32.     }
  33.  
  34.     else if (c >= 'a' && c <= 'z')
  35.     {
  36.         return c;
  37.     }
  38. }
  39.  
  40. node *insert(node* head, node *newnode)
  41. {
  42.     // d->e->a->b ---- INPUT
  43.     // a->b->d->e ---- SORTED
  44.     // Insert(c)
  45.     // a->b->c->d->e ---- NEW SORTED
  46.  
  47.     node* temp = head;
  48.  
  49.     if (head == NULL) // head empty
  50.     {
  51.         head = newnode;
  52.         head->next = NULL;
  53.         return head;
  54.     }
  55.  
  56.     if (head->next == NULL) // 1 item in list
  57.     {
  58.         if (head->data <= newnode->data)
  59.         {
  60.             newnode->next = NULL;
  61.             head->next = newnode;
  62.             return head;
  63.         }
  64.  
  65.         else if (newnode->data <= head->data)
  66.         {
  67.             newnode->next = head;
  68.             head = newnode;
  69.             return head;
  70.         }
  71.     }
  72.  
  73.     if (head != NULL && (newnode->data <= head->data))
  74.     {
  75.         newnode->next = head;
  76.         head = newnode;
  77.         return head;
  78.     }
  79.  
  80.     while (temp != NULL) // normal
  81.     {
  82.         if (temp->next == NULL) // Last item
  83.         {
  84.             newnode->next = NULL;
  85.             temp->next = newnode;
  86.             return head;
  87.         }
  88.  
  89.         else // Not last item
  90.         {
  91.             if (lower(temp->data) <= lower(newnode->data) && lower(newnode->data) <= lower(temp->next->data)) // b<c<d
  92.             {
  93.                 newnode->next = temp->next;
  94.                 temp->next = newnode;
  95.                 return head;
  96.             }
  97.  
  98.             else
  99.                 temp = temp->next;
  100.         }
  101.     }
  102.  
  103.     return head;
  104. }
  105.  
  106. void print_list(node* head)
  107. {
  108.     node* temp = head;
  109.  
  110.     while (temp != NULL)
  111.     {
  112.         printf("%c->", temp->data);
  113.         temp = temp->next;
  114.     }
  115. }
  116.  
  117. node* func()
  118. {
  119.     node* head = NULL;
  120.  
  121.     char input;
  122.     node *input_node;
  123.  
  124.     printf("Insert only a-z letters. Insert any other character to stop.\n");
  125.     printf("Please enter a value: ");
  126.     scanf("%c", &input);
  127.     rewind(stdin);
  128.  
  129.     while ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z'))
  130.     {
  131.         input_node = create_node(input);
  132.         head = insert(head, input_node);
  133.  
  134.         printf("Please enter a value: ");
  135.         scanf("%c", &input);
  136.         rewind(stdin);
  137.     }
  138.  
  139.     return head;
  140. }
  141.  
  142.  
  143. node* reverse(node *head)
  144. {
  145.    
  146. }
  147.  
  148. int main()
  149. {
  150.     node* head = NULL;
  151.  
  152.     head = func();
  153.  
  154.     print_list(head);
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement