Last active
December 17, 2015 18:09
-
-
Save nicksloan/5651152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct element { | |
int val; | |
struct element * next; | |
} item; | |
item * makeitem(item * curr, int value) { | |
printf("%d\n", value); | |
curr->val = value; | |
return curr; | |
} | |
item * makelist(int *numbers, int length) { | |
item * first, * next, * curr; | |
int i; | |
int val; | |
for (i = 0; i < length; i++) { | |
next = (item *)malloc(sizeof(item)); | |
if (i == 0) { | |
first = next; | |
} else { | |
curr->next = next; | |
} | |
val = numbers[i]; | |
curr = makeitem(next, val); | |
} | |
curr->next = NULL; | |
return first; | |
} | |
int main(int argc, char *argv[]) { | |
int i; | |
int val; | |
// Pointer to the end of what can be parsed to an integer | |
char * end; | |
int values[argc - 1]; | |
int value; | |
for (i = 1; i < argc; i++) { | |
value = strtol(argv[i], &end, 10); | |
if (*end) { | |
printf("Couldn't convert %s.", end); | |
continue; | |
} | |
values[i-1] = value; | |
} | |
item * curr; | |
curr = makelist(values, argc - 1); | |
if (curr == NULL) { | |
printf("We have made a huge mistake!\n"); | |
return 1; | |
} | |
i = 0; | |
while (curr != NULL) { | |
printf("At position %d: %d\n", i, curr->val); | |
curr = curr->next; | |
i++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment