-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprog.17.2.c
52 lines (39 loc) · 990 Bytes
/
prog.17.2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
struct entry
{
int value;
struct entry *next;
};
// add new entry to the end of linked list
struct entry *addEntry (struct entry *listPtr)
{
// find the end of the list
while ( listPtr->next != NULL )
listPtr = listPtr->next;
// get storage for new entry
listPtr->next = (struct entry *) malloc (sizeof (struct entry));
// add null to the new end of the list
if ( listPtr->next != NULL )
(listPtr->next)->next = (struct entry *) NULL;
return listPtr->next;
}
int main (int argc, char *argv[])
{
struct entry listHead = { .value = 1, .next = (struct entry *) 0 };
struct entry *listPtr = &listHead;
for (int i = 1; i < argc; ++i) {
struct entry *newEntry = addEntry (listPtr);
if ( newEntry != NULL ) {
newEntry->value = atoi (argv[i]);
}
listPtr = newEntry;
}
listPtr = &listHead;
while (listPtr != NULL) {
printf ("%i\n", listPtr->value);
listPtr = listPtr->next;
}
return 0;
}