Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct node_ {
  7.     char* val;
  8.     struct node_ * next;
  9. } node;
  10.  
  11. node *head = 0;
  12. node *curr = 0;
  13.  
  14. node *nodeWithString(char *string) {
  15.     node * out = malloc(sizeof(node));
  16.     out->val = string;
  17.     out->next = 0;
  18.     return out;
  19. }
  20.  
  21. void printList() {
  22.     curr = head;
  23.     while (curr != 0) {
  24.         printf("%s\n", curr->val);
  25.         curr = curr->next;
  26.     }
  27. }
  28.  
  29. int min(int one, int two) {
  30.     int out = one;
  31.     if (two < one) {
  32.         out = two;  
  33.     }
  34.     return out;
  35. }
  36.  
  37. int compareString(char *one, char *two) {
  38.     int minLength = min(strlen(one), strlen(two));
  39.     int i;
  40.     for (i = 0; i < minLength; i++) {
  41.         if (one[i] < two[i]) {
  42.             return -1;
  43.         } else if (one[i] > two[i]) {
  44.             return 1;
  45.         }
  46.     }
  47.     if (strlen(one) > strlen(two)) {
  48.         return 1;
  49.     } else {
  50.         return -1;  
  51.     }
  52. }
  53.  
  54. void insertNode(char *string) {
  55.         node *new = nodeWithString(strdup(string));
  56.    
  57.         node *prev = 0;    
  58.    
  59.         if (head == 0) {
  60.             head = new;  
  61.         } else {
  62.             curr = head;
  63.            
  64.             if (compareString(string, curr->val) < 0) {
  65.                 new->next = curr;
  66.                 head = new;
  67.                 return;
  68.             }
  69.            
  70.             while (curr->next != 0) { // Advance curr until its the last node
  71.                 if (compareString(string, curr->val) < 0) { // Squeeze node in
  72.                     prev->next = new;
  73.                     new->next = curr;
  74.                     return;
  75.                 } else {
  76.                     prev = curr;
  77.                     curr = curr->next;
  78.                 }
  79.             }
  80.             // Place node at the end of list
  81.             curr->next = new;
  82.         }
  83. }
  84.  
  85. int main(int argc, char** argv) {
  86.    
  87.     if (argc != 2) {
  88.         printf("ERROR: This program takes exactly one argument.\n");
  89.         return 1;
  90.     }
  91.        
  92.     int inputLength = strlen(argv[1]);
  93.    
  94.     char buffer[inputLength];
  95.     memset(buffer,0,inputLength); // Clear buffer
  96.            
  97.     int i;
  98.     for (i = 0; i < strlen(argv[1]); i++) {
  99.         char c = argv[1][i];
  100.         if (isalpha(c)) {
  101.             buffer[strlen(buffer)] = c; // Add character to buffer
  102.         } else {
  103.             if (strlen(buffer) != 0) {
  104.                 insertNode(buffer);
  105.             }
  106.             memset(buffer,0,inputLength); // Clear buffer
  107.         }
  108.     }
  109.    
  110.     if (strlen(buffer) != 0) {
  111.         insertNode(buffer);
  112.     }
  113.    
  114.     printList();
  115.        
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement