linked list example

Discussion in 'C Programming' started by Bill Cunningham, Jun 11, 2010.

  1. Is this a very simple example of a linked list or is the concept I am
    expressing in error again.

    #include "c.h"

    struct cat kat, *p;

    int main(void)
    {
    for (p = p; p != NULL; p = p->next) {
    kat.name = "striper";
    kat.color = "striped";
    if (p == NULL) {
    printf("no more lists");
    return 0;
    }
    return 0;
    }
    }

    c.h just contains a 3 member struct and stdio, stdlib and string.h.

    Bill
     
    Bill Cunningham, Jun 11, 2010
    #1
    1. Advertising

  2. Bill Cunningham

    Twixer Xev Guest

    On 06/11/2010 02:06 PM, Bill Cunningham wrote:
    > Is this a very simple example of a linked list or is the concept I am
    > expressing in error again.
    >
    > #include "c.h"
    >
    > struct cat kat, *p;
    >
    > int main(void)
    > {
    > for (p = p; p != NULL; p = p->next) {


    The pointer p was never initialized. What does it point to? Nobody
    knows.

    > kat.name = "striper";
    > kat.color = "striped";
    > if (p == NULL) {


    The pointer p will never equal NULL at this stage of the loop.

    > printf("no more lists");
    > return 0;
    > }
    > return 0;


    If the loop manages to run at all, it will only go once due to this
    unconditional return.

    > }
    > }
    >
    > c.h just contains a 3 member struct and stdio, stdlib and string.h.
    >
    > Bill
     
    Twixer Xev, Jun 11, 2010
    #2
    1. Advertising

  3. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > Is this a very simple example of a linked list or is the concept I am
    > expressing in error again.
    >
    > #include "c.h"
    >
    > struct cat kat, *p;
    >
    > int main(void)
    > {
    > for (p = p; p != NULL; p = p->next) {
    > kat.name = "striper";
    > kat.color = "striped";
    > if (p == NULL) {
    > printf("no more lists");
    > return 0;
    > }
    > return 0;
    > }
    > }
    >
    > c.h just contains a 3 member struct and stdio, stdlib and string.h.


    Please don't post things that don't compile. There is no definition for
    struct cat.
     
    osmium, Jun 11, 2010
    #3
  4. Twixer Xev wrote:
    > On 06/11/2010 02:06 PM, Bill Cunningham wrote:
    >> Is this a very simple example of a linked list or is the
    >> concept I am expressing in error again.
    >>
    >> #include "c.h"
    >>
    >> struct cat kat, *p;
    >>
    >> int main(void)
    >> {
    >> for (p = p; p != NULL; p = p->next) {

    >
    > The pointer p was never initialized. What does it point to? Nobody
    > knows.


    Ok. Whew. Would I want it to point to the kat of struct type?

    >> kat.name = "striper";
    >> kat.color = "striped";
    >> if (p == NULL) {

    >
    > The pointer p will never equal NULL at this stage of the loop.


    ok

    >> printf("no more lists");
    >> return 0;
    >> }
    >> return 0;

    >
    > If the loop manages to run at all, it will only go once due to this
    > unconditional return.
    >
    >> }
    >> }
    >>
    >> c.h just contains a 3 member struct and stdio, stdlib and string.h.
    >>
    >> Bill


    I'm getting closer to the linked list. I pulled this from an online
    example. I have been reading kandr2 lately that goes into detail with a
    binary tree. Isn't a pointer usually initialized to NULL now that I
    remember?

    Bill
     
    Bill Cunningham, Jun 11, 2010
    #4
  5. On 2010-06-11, Bill Cunningham <> wrote:
    > Is this a very simple example of a linked list or is the concept I am
    > expressing in error again.
    >
    > #include "c.h"
    >
    > struct cat kat, *p;
    >
    > int main(void)
    > {
    > for (p = p; p != NULL; p = p->next) {
    > kat.name = "striper";
    > kat.color = "striped";
    > if (p == NULL) {
    > printf("no more lists");
    > return 0;
    > }
    > return 0;
    > }
    > }
    >
    > c.h just contains a 3 member struct and stdio, stdlib and string.h.


    ....and it would be very helpful here to see what it is you are
    trying to do. The above code seems to be a mishmash of various
    parts of linked list operation. On the one hand it appears to be
    trying to populate a list and on the other it appears to assume
    the list is already structured. You should start by separating
    out the two parts into separate fucntions - one allocates space
    for a new entry using malloc(), populates it with whatever is needed
    and links it into the list. The second reads out the list.

    Also look at the interaction between that loop and your if statement
    - the if condition will never be satisfied since the loop will stop
    iterating when p is NULL. Perhaps a do...while loop would be the
    more natural way of expressing what you were attempting.

    Examples of those functions - not tested, just quickly knocked out
    so they may not be perfect:

    struct cat add_entry(name, color, old_list)
    {
    struct cat *new;

    if ((new = malloc(sizeof(struct cat))) == NULL)
    abort() /* or whatever error handling */

    new->name = name;
    new->color = color;

    new->next = old_list;

    return new;
    }

    void print_entries(struct cat *list)
    {
    while (list != NULL) {
    printf("Cat: %s color: %s\n", list->name, list->color);
    list = list->next;
    }
    }

    --
    Andrew Smallshaw
     
    Andrew Smallshaw, Jun 11, 2010
    #5
  6. osmium wrote:

    > Please don't post things that don't compile. There is no definition
    > for struct cat.


    It compiled for me. Of course then it didn't do anything.

    Bill
     
    Bill Cunningham, Jun 11, 2010
    #6
  7. Andrew Smallshaw wrote:

    > ...and it would be very helpful here to see what it is you are
    > trying to do. The above code seems to be a mishmash of various
    > parts of linked list operation.


    It is.

    On the one hand it appears to be
    > trying to populate a list and on the other it appears to assume
    > the list is already structured. You should start by separating
    > out the two parts into separate fucntions - one allocates space
    > for a new entry using malloc(), populates it with whatever is needed
    > and links it into the list.


    ok

    The second reads out the list.
    >
    > Also look at the interaction between that loop and your if statement
    > - the if condition will never be satisfied since the loop will stop
    > iterating when p is NULL. Perhaps a do...while loop would be the
    > more natural way of expressing what you were attempting.
    >
    > Examples of those functions - not tested, just quickly knocked out
    > so they may not be perfect:
    >
    > struct cat add_entry(name, color, old_list)
    > {
    > struct cat *new;
    >
    > if ((new = malloc(sizeof(struct cat))) == NULL)
    > abort() /* or whatever error handling */
    >
    > new->name = name;
    > new->color = color;
    >
    > new->next = old_list;
    >
    > return new;
    > }
    >
    > void print_entries(struct cat *list)
    > {
    > while (list != NULL) {
    > printf("Cat: %s color: %s\n", list->name, list->color);
    > list = list->next;
    > }
    > }
     
    Bill Cunningham, Jun 11, 2010
    #7
  8. Twixer Xev <> writes:
    > On 06/11/2010 02:06 PM, Bill Cunningham wrote:
    >> Is this a very simple example of a linked list or is the concept I am
    >> expressing in error again.
    >>
    >> #include "c.h"
    >>
    >> struct cat kat, *p;
    >>
    >> int main(void)
    >> {
    >> for (p = p; p != NULL; p = p->next) {

    >
    > The pointer p was never initialized. What does it point to? Nobody
    > knows.


    Since p was declared at file scope, it has static storage duration and
    is therefore implicitly initialized to NULL. The loop body will never
    execute.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 11, 2010
    #8
  9. "Bill Cunningham" <> writes:
    > osmium wrote:
    >> Please don't post things that don't compile. There is no definition
    >> for struct cat.

    >
    > It compiled for me. Of course then it didn't do anything.


    It compiled for you because you have a copy of "c.h". We don't.

    Are you still using examples from that same book, the one you've
    already been told is ful of serious errors? If so, why?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 11, 2010
    #9
  10. Keith Thompson <> writes:

    > Twixer Xev <> writes:
    >> On 06/11/2010 02:06 PM, Bill Cunningham wrote:
    >>> Is this a very simple example of a linked list or is the concept I am
    >>> expressing in error again.
    >>>
    >>> #include "c.h"
    >>>
    >>> struct cat kat, *p;
    >>>
    >>> int main(void)
    >>> {
    >>> for (p = p; p != NULL; p = p->next) {

    >>
    >> The pointer p was never initialized. What does it point to? Nobody
    >> knows.

    >
    > Since p was declared at file scope, it has static storage duration and
    > is therefore implicitly initialized to NULL. The loop body will never
    > execute.


    I don't think we can know that. I am not much of a fan of puzzles but
    maybe it will grab someone's interest. I have this source file:

    #include "cat.h"

    struct cat kat, *p;

    int main(void)
    {
    for (p = p; p != NULL; p = p->next)
    printf("%d\n", p->age);
    return 0;
    }

    $ gcc -std=c89 -pedantic -o cat cat.c
    $ ./cat
    2
    14
    12

    What is in cat.h?

    --
    Ben.
     
    Ben Bacarisse, Jun 11, 2010
    #10
  11. Bill Cunningham

    Twixer Xev Guest

    On 06/11/2010 03:17 PM, Ben Bacarisse wrote:
    [...]
    > I don't think we can know that. I am not much of a fan of puzzles but
    > maybe it will grab someone's interest. I have this source file:
    >
    > #include "cat.h"
    >
    > struct cat kat, *p;
    >
    > int main(void)
    > {
    > for (p = p; p != NULL; p = p->next)
    > printf("%d\n", p->age);
    > return 0;
    > }
    >
    > $ gcc -std=c89 -pedantic -o cat cat.c
    > $ ./cat
    > 2
    > 14
    > 12
    >
    > What is in cat.h?
    >


    Right. "cat.h" might have another definition of p which points to a
    valid (or possibly malformed) linked list. That is what makes the
    OP's program ambiguous.
     
    Twixer Xev, Jun 11, 2010
    #11
  12. Keith Thompson wrote:

    [...]

    > Are you still using examples from that same book, the one you've
    > already been told is ful of serious errors? If so, why?


    I search the net for examples. Kandr2 in accordance to what I've read
    doesn't have an example that I can understand anyway for linked lists. It
    does the binary tree as per "Now is the time for all good men to come to the
    aid of thier party" or something similar. I probably should've initialied to
    NULL. A copy of c.h

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    struct cat{
    char *name;
    char *color;
    };

    Bill
     
    Bill Cunningham, Jun 12, 2010
    #12
  13. Richard Heathfield wrote:

    > Take a guess.
    >
    > Linked lists are easy. K&R2 covers the subject in adequate detail.
    >
    > Of course, if linked lists aren't working for you, you could try +=.
    > Or perhaps a preprocessor directive.


    Richard could you tell me in kandr2 where linked lists are explained
    because I can't see it.

    Bill
     
    Bill Cunningham, Jun 12, 2010
    #13
  14. Richard Heathfield wrote:

    > Take a guess.
    >
    > Linked lists are easy. K&R2 covers the subject in adequate detail.
    >
    > Of course, if linked lists aren't working for you, you could try +=.
    > Or perhaps a preprocessor directive.


    Oh I haven't even got to preprocessor directives. I'd be more confused
    than I am now.

    Bill
     
    Bill Cunningham, Jun 12, 2010
    #14
  15. "Bill Cunningham" <> writes:
    > Richard Heathfield wrote:
    >> Take a guess.
    >>
    >> Linked lists are easy. K&R2 covers the subject in adequate detail.
    >>
    >> Of course, if linked lists aren't working for you, you could try +=.
    >> Or perhaps a preprocessor directive.

    >
    > Oh I haven't even got to preprocessor directives. I'd be more confused
    > than I am now.


    Just in case you aren't already aware of it, Richard was commenting on
    your tendency to try random things when you run into problems.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 12, 2010
    #15
  16. Richard Heathfield wrote:

    > Section 6.6.
    >
    > With regards to your other reply, if you haven't yet covered easy easy
    > stuff like preprocessor directives, you probably want to cover those
    > first before getting onto complicated easy stuff like linked lists.


    I will study the table code at §6.6 and maybe post what it says. I read
    it and it didn't click right off.

    Bill
     
    Bill Cunningham, Jun 13, 2010
    #16
  17. Keith Thompson wrote:

    > Just in case you aren't already aware of it, Richard was commenting on
    > your tendency to try random things when you run into problems.


    Ok I'll try this that's not random. p 143 ¶ 3 §6.6.

    for(hashval=0;*s!='\0';s++)

    Why is *s not just s? This speaks of arrays. When I think of linked lists I
    don't think of arrays except arrays of structs. I will work with nlist,
    lookup, and install and try to figure them out. It might work. But do I
    understand what I'm doing? I guess I'll see.

    Bill
     
    Bill Cunningham, Jun 13, 2010
    #17
  18. #include <stdio.h>
    #include <string.h>

    struct nlist {
    struct nlist *next;
    char *name;
    char *def;
    };

    static struct nlist *hashtab[101]; /* What's this? A declared array

    to pointer? What's its purpose */

    unsigned hash(char *s)
    {
    unsigned hashval;
    for (hashval = 0; *s != '\0'; s++) /*Indent gives me an old style

    error here */

    hashval = *s + 31 * hasval;
    return hashval % 101;
    }

    Another attempt.

    Bill
     
    Bill Cunningham, Jun 13, 2010
    #18
  19. Bill Cunningham

    Chad Guest

    On Jun 12, 4:23 pm, pete <> wrote:
    > Bill Cunningham wrote:
    >
    > >     Is this a very simple example of a linked list
    > > or is the concept I am
    > > expressing in error again.

    >
    > I don't know.
    > Here's a fairly simple example of a linked list:
    >
    > /* BEGIN Lf_start.c */
    > /*
    > ** Demonstration of use of long double list functions.
    > ** Lf_start
    > ** Lf_append_0
    > */
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define NUMBERS         {15,14,13,7,20,9,8,12,11,6}
    >
    > #define NMEMB(A)        (sizeof (A) / sizeof *(A))
    >
    > struct Lf_node {
    >     struct Lf_node      *next;
    >     long double          data;
    >
    > };
    >
    > typedef struct Lf_node Lf_type;
    >
    > int Lf_fprintf(const Lf_type *node, FILE *stream);
    > Lf_type *Lf_start(long double data);
    > Lf_type *Lf_append_0(Lf_type *tail, long double data);
    > void Lf_free(Lf_type *node);
    >
    > int main(void)
    > {
    >     Lf_type *head;
    >     Lf_type *tail;
    >     long double numbers[] = NUMBERS;
    >     long double *ptr = numbers;
    >     long double *const after = numbers + NMEMB(numbers);
    >
    >     puts("/* BEGIN Lf_start.c output */");
    >     puts("\nOriginal order of list of long doubles:");
    >     head = tail = Lf_start(*ptr);
    >     if (head != NULL) {
    >         while (++ptr != after) {
    >             tail = Lf_append_0(tail, *ptr);
    >             if (tail == NULL) {
    >                 puts("malloc trouble!");
    >                 break;
    >             }
    >         }
    >     } else {
    >         puts("malloc trouble!");
    >     }
    >     Lf_fprintf(head, stdout);
    >     Lf_free(head);
    >     puts("\n/* END Lf_start.c output */");
    >     return 0;
    >
    > }
    >
    > int Lf_fprintf(const Lf_type *node, FILE *stream)
    > {
    >     int rc = 0;
    >
    >     while (node != NULL) {
    >         if (0 > (rc = fprintf(stream, "%Lf\n", node -> data))) {
    >             break;
    >         }
    >         node = node -> next;
    >     }
    >     return rc;
    >
    > }
    >
    > Lf_type *Lf_start(long double data)
    > {
    >     Lf_type *node;
    >
    >     node = malloc(sizeof *node);
    >     if (node != NULL) {
    >         node -> next = NULL;
    >         node -> data = data;
    >     }
    >     return node;
    >
    > }
    >
    > Lf_type *Lf_append_0(Lf_type *tail, long double data)
    > {
    >     Lf_type *node;
    >
    >     node = malloc(sizeof *node);
    >     if (node != NULL) {
    >         node -> next = NULL;
    >         node -> data = data;
    >         tail -> next = node;
    >     }
    >     return node;
    >
    > }
    >
    > void Lf_free(Lf_type *node)
    > {
    >     Lf_type *next_node;
    >
    >     while (node != NULL) {
    >         next_node = node -> next;
    >         free(node);
    >         node = next_node;
    >     }
    >
    > }
    >
    > /* END Lf_start.c */
    >


    Maybe I didn't look at your code close enough, but why, in


    Lf_type *Lf_start(long double data)
    {
    Lf_type *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
    node -> next = NULL;
    node -> data = data;
    }
    return node;

    }
     
    Chad, Jun 13, 2010
    #19
  20. Bill Cunningham

    Chad Guest

    > Maybe I didn't look at your code close enough, but why, in
    >
    > Lf_type *Lf_start(long double data)
    > {
    >     Lf_type *node;
    >
    >     node = malloc(sizeof *node);
    >     if (node != NULL) {
    >         node -> next = NULL;
    >         node -> data = data;
    >     }
    >     return node;
    >
    > }
    >
    >


    Disregard that partial reply. I started to ask the question, but then
    I decided to think about it, instead of just asking. And now, for
    whatever reasons, the post got posted instead of discarded.
     
    Chad, Jun 13, 2010
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    491
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    487
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    525
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    683
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    776
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page