memory.c (Appendix 2) 1 #define _GNU_SOURCE 2 #include 3 #include 4 #include 5 6 struct Occurrence { 7 char *word; 8 int wordno; 9 }; 10 11 12 13 struct List { 14 struct Occurrence *occ; 15 struct List *next; 16 }; 17 18 void printList (struct List *mylist) { 19 20 /* print the list */ 21 while (mylist) { 22 printf ("The next word is %s\n", mylist->occ->word); 23 mylist = mylist->next; 24 } 25 } 26 27 int main () { 28 29 struct List *mylist, *tmp; 30 31 char *line = NULL; 32 int i; 33 char *pos, *nextpos; 34 struct Occurrence *occ; 35 int len; 36 37 38 while (getline (&line, &len, stdin) != -1) { 39 /* next line read */ 40 mylist = NULL; 41 pos = line; 42 i = 1; 43 while ((nextpos = strstr (pos, " ")) != NULL ) { 44 /* next word found */ 45 occ->wordno = i; 46 occ->word = strncpy (occ->word, pos, nextpos - pos); 47 occ->word[nextpos - pos] = '\0'; 48 49 /* add element into list */ 50 tmp->occ = occ; 51 tmp->next = mylist; 52 mylist = tmp; 53 54 pos = nextpos + 1; 55 } 56 printList (mylist); 57 } 58 exit (0); 59 }