Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- typedef struct node_ {
- char* val;
- struct node_ * next;
- } node;
- node *head = 0;
- node *curr = 0;
- node *nodeWithString(char *string) {
- node * out = malloc(sizeof(node));
- out->val = string;
- out->next = 0;
- return out;
- }
- void printList() {
- curr = head;
- while (curr != 0) {
- printf("%s\n", curr->val);
- curr = curr->next;
- }
- }
- int min(int one, int two) {
- int out = one;
- if (two < one) {
- out = two;
- }
- return out;
- }
- int compareString(char *one, char *two) {
- int minLength = min(strlen(one), strlen(two));
- int i;
- for (i = 0; i < minLength; i++) {
- if (one[i] < two[i]) {
- return -1;
- } else if (one[i] > two[i]) {
- return 1;
- }
- }
- if (strlen(one) > strlen(two)) {
- return 1;
- } else {
- return -1;
- }
- }
- void insertNode(char *string) {
- node *new = nodeWithString(strdup(string));
- node *prev = 0;
- if (head == 0) {
- head = new;
- } else {
- curr = head;
- if (compareString(string, curr->val) < 0) {
- new->next = curr;
- head = new;
- return;
- }
- while (curr->next != 0) { // Advance curr until its the last node
- if (compareString(string, curr->val) < 0) { // Squeeze node in
- prev->next = new;
- new->next = curr;
- return;
- } else {
- prev = curr;
- curr = curr->next;
- }
- }
- // Place node at the end of list
- curr->next = new;
- }
- }
- int main(int argc, char** argv) {
- if (argc != 2) {
- printf("ERROR: This program takes exactly one argument.\n");
- return 1;
- }
- int inputLength = strlen(argv[1]);
- char buffer[inputLength];
- memset(buffer,0,inputLength); // Clear buffer
- int i;
- for (i = 0; i < strlen(argv[1]); i++) {
- char c = argv[1][i];
- if (isalpha(c)) {
- buffer[strlen(buffer)] = c; // Add character to buffer
- } else {
- if (strlen(buffer) != 0) {
- insertNode(buffer);
- }
- memset(buffer,0,inputLength); // Clear buffer
- }
- }
- if (strlen(buffer) != 0) {
- insertNode(buffer);
- }
- printList();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement