Skip to content

Instantly share code, notes, and snippets.

@gitmarek
Last active April 24, 2019 11:38
Show Gist options
  • Select an option

  • Save gitmarek/5e102bb548aba8bce6bfd1043c1517fe to your computer and use it in GitHub Desktop.

Select an option

Save gitmarek/5e102bb548aba8bce6bfd1043c1517fe to your computer and use it in GitHub Desktop.

Revisions

  1. gitmarek revised this gist Apr 24, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions list.c
    Original 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 = (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; ) ;
    for (e=l; e=e->next; ) printf("%s", (char*) e->data);

    // e.g.
    for (e=l, n=0; e=e->next; n++);
  2. gitmarek revised this gist Apr 24, 2019. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions list.c
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@
    #include <stdlib.h>


    typedef struct {
    typedef struct elem {
    void *data;
    void *next;
    struct elem *next;
    } elem_t;


    @@ -16,25 +16,25 @@ int main(int argc, char **argv) {
    elem_t *e, *l;

    // allocate list l
    l = malloc(sizeof(elem_t));
    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; ) {
    // append line to l
    e->next = malloc(sizeof(elem_t));
    e = e->next;
    e->data = line;
    for (e=l; getline(&line, &n, stdin) != -1; e = e->next) {

    // make getline allocate resources for another round
    // 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;
    n=0;
    }


    // this is how you traverse l
    for (e=l; e=e->next; ) ;
    for (e=l; e=e->next; ) ;

    // e.g.
    for (e=l, n=0; e=e->next; n++);
  3. gitmarek created this gist Apr 24, 2019.
    53 changes: 53 additions & 0 deletions list.c
    Original 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;
    }