linked list

This is a discussion on linked list within the C Programming forums, part of the General Programming Boards category; I'm trying to set up my first linked list. I think I set it up right but when I try ...

  1. #1
    ecd
    ecd is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    linked list

    I'm trying to set up my first linked list. I think I set it up right but when I try to print the contents to stderr I don't see what's expected. Can someone look at this code snipet and let me know if I'm reading the list correctly or if the problem is in how I write to the list?

    typedef struct Nameval Nameval;
    struct Nameval {
    char *name;
    int *age;
    Nameval *next;
    };


    in main.....
    {

    char dbname[20];
    int dbage;

    Nameval *namelist, *listptr;

    read from database......

    if(dbage == something)
    namelist = addname(namelist, newnameval(dbname));

    for(; namelist->next != NULL; namelist = namelist->next)
    fprintf(stderr,"name = %.20s\n",namelist->name);

    }


    Nameval *newname(char *name)
    {
    Nameval *newnm;

    newnm = (Nameval *) malloc(sizeof(Nameval));
    newnm ->name = name;
    newnm->age = 20;
    newnm->next = NULL;
    return newnm;
    }

    Nameval *addname(Nameval *listptr, Nameval *newname)
    {
    newname->next = listptr;
    return newname;
    }


    When I try to print to stderr, the same name constantly prints out and it's not one that I would expect to see. Thx.






    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It works okay when I test it. Can you give us a complete (short!) program that exhibits the problem?
    My best code is written with the delete key.

  3. #3
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    error
    Code:
    typedef struct Nameval Nameval;
    struct Nameval {
    char *name;
    int *age;
    Nameval *next;
    };
    good one
    Code:
    typedef struct {
    struct Nameval {
    char *name;
    int *age;
    Nameval *next;
    }Nameval;
    it's o.k
    precise your question /*sorry about my english */

  4. #4
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    or use this
    struct Nameval {
    char *name;
    int *age;
    Nameval *next;
    };
    typedef struct {Nameval name[maxname];} Nameval;

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You're wrong. All of your code is wrong.
    Code:
    typedef struct foo Tfoo;
    struct foo {
        Tfoo *next;
    };
    You cannot typedef something to a name that already exists. Do you ever even try compiling what you write? I suggest before you post code from now on, due to the sheer volume of errors you're handing out, and saying that it's right, that you actually try and compile your examples before posting them.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21