Last active
April 24, 2019 11:38
-
-
Save gitmarek/5e102bb548aba8bce6bfd1043c1517fe to your computer and use it in GitHub Desktop.
Revisions
-
gitmarek revised this gist
Apr 24, 2019 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ int main(int argc, char **argv) { for (e=l; getline(&line, &n, stdin) != -1; e = e->next) { // append line to l e->next = (elem_t*) malloc(sizeof(elem_t)); e->next->data = line; // make getline allocate resources for another round @@ -34,7 +34,7 @@ int main(int argc, char **argv) { // this is how you traverse l for (e=l; e=e->next; ) printf("%s", (char*) e->data); // e.g. for (e=l, n=0; e=e->next; n++); -
gitmarek revised this gist
Apr 24, 2019 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,9 @@ #include <stdlib.h> typedef struct elem { void *data; struct elem *next; } elem_t; @@ -16,25 +16,25 @@ int main(int argc, char **argv) { elem_t *e, *l; // allocate list l l = (elem_t*) malloc(sizeof(elem_t)); // and its metadata l->data = malloc(sizeof(int)); // read all lines from STDIN for (e=l; getline(&line, &n, stdin) != -1; e = e->next) { // append line to l e->next = (elem_t*) malloc(sizeof(elem_t)); e->next->data = line; // make getline allocate resources for another round line=NULL; n=0; } // this is how you traverse l for (e=l; e=e->next; ) ; // e.g. for (e=l, n=0; e=e->next; n++); -
gitmarek created this gist
Apr 24, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ #include <stdio.h> #include <stdlib.h> typedef struct { void *data; void *next; } elem_t; int main(int argc, char **argv) { char *line; size_t n; elem_t *e, *l; // allocate list l l = malloc(sizeof(elem_t)); // and its metadata l->data = malloc(sizeof(int)); // read all lines from STDIN for (e=l; getline(&line, &n, stdin) != -1; ) { // append line to l e->next = malloc(sizeof(elem_t)); e = e->next; e->data = line; // make getline allocate resources for another round line=NULL; n=0; } // this is how you traverse l for (e=l; e=e->next; ) ; // e.g. for (e=l, n=0; e=e->next; n++); printf("No. of lines read: %ld\n", n); // free l and its elements while (l) { e = l->next; free(l->data); free(l); l = e; } return 0; }