how to add at the end of the linked list

Discussion in 'C++' started by chirag, Feb 11, 2005.

  1. chirag

    chirag Guest

    hi i am writing the following function . but does not seem to put the code
    in sequence. it does not compile. its kind of messed up. please help me
    improve it. thanks.

    void addToEndOfLinkedList(Node * &head)
    // Reads a series of integers from the keyboard (ending with -1) and
    // appends them to the end of the linked list pointed to by head. No
    // error checking is done. Correctly handles the case when the
    // original list starts out empty.

    {

    int a;
    int prev;

    cout<<"Enter an integer, -1 to quit:";
    cout.flush();

    while ((cin >> a) && (a != -1))


    if (prev==NULL)

    {
    Node *newPtr;
    Node *newvalue;

    newPtr = new Node;
    newPtr->item= newvalue;
    newPtr->next = NULL;
    NULL= newPtr;

    }
    }
     
    chirag, Feb 11, 2005
    #1
    1. Advertising

  2. chirag

    Rolf Magnus Guest

    chirag wrote:

    > hi i am writing the following function . but does not seem to put the code
    > in sequence. it does not compile.


    And no error message form the compiler?

    > its kind of messed up. please help me improve it. thanks.
    >
    > void addToEndOfLinkedList(Node * &head)


    Why do you pass the pointer by reference? You are not writing to it.
    Actually, you aren't using the pointer at all in the function.

    > // Reads a series of integers from the keyboard (ending with -1) and
    > // appends them to the end of the linked list pointed to by head. No
    > // error checking is done. Correctly handles the case when the
    > // original list starts out empty.
    >
    > {
    >
    > int a;
    > int prev;
    >
    > cout<<"Enter an integer, -1 to quit:";
    > cout.flush();


    No need to flush here. cin and cout are tied together, which means that
    reading from cin will automatically flush cout.

    > while ((cin >> a) && (a != -1))


    You're reading your integers here, but you are never using them for anything
    except ending the loop.

    > if (prev==NULL)


    prev is uninitialized. Don't ever use variables that haven't been
    initialized and haven't been written to before. The value is undefined.
    Another thing: Never use NULL in an integer context. It is meant for
    pointers only. Actually, it's a good idea to not use NULL at all. Just use
    0. Anyway, I'm not sure that prev actually is supposed to be an integer.
    What is the purpose of prev?

    > {
    > Node *newPtr;
    > Node *newvalue;
    >
    > newPtr = new Node;
    > newPtr->item= newvalue;


    Again, newvalue is uninitialized. Also, is newPtr->item really a pointer to
    Node? Looks to me as if you actually should assign the variable a to it.

    > newPtr->next = NULL;
    > NULL= newPtr;


    That last assignment makes no sense. You cannot assign anything to NULL.
    What was the intention of that?
    Where are you actually appending your new node to the list?

    > }
    > }
     
    Rolf Magnus, Feb 11, 2005
    #2
    1. Advertising

  3. chirag wrote:
    > hi i am writing the following function . but does not seem to put the code
    > in sequence. it does not compile. its kind of messed up. please help me
    > improve it. thanks.
    >
    > void addToEndOfLinkedList(Node * &head)

    Just pass the pointer, no need to pass it by reference.
    Is this argument a new node or a pointer to the start of the list?

    > // Reads a series of integers from the keyboard (ending with -1) and
    > // appends them to the end of the linked list pointed to by head. No
    > // error checking is done. Correctly handles the case when the
    > // original list starts out empty.
    >
    > {
    >
    > int a;
    > int prev;
    >
    > cout<<"Enter an integer, -1 to quit:";
    > cout.flush();
    >
    > while ((cin >> a) && (a != -1))
    >
    >
    > if (prev==NULL)
    >
    > {
    > Node *newPtr;
    > Node *newvalue;
    >
    > newPtr = new Node;
    > newPtr->item= newvalue;
    > newPtr->next = NULL;
    > NULL= newPtr;
    >
    > }
    > }
    >

    Rather than correct your code, here is the generic
    code for appending a node to a list:
    struct Node
    {
    Node * next;
    };

    struct Integer_Node
    : public Node
    {
    int value;
    };

    void Append_To_List(Node * &list_start,
    Node * new_node)
    {
    /* Make sure that the pointer to the new node is
    * not NULL. Bad things happen when dereferencing
    * a NULL pointer.
    */
    assert(new_node != NULL);

    /* As a precaution, set the "next" field of the
    * new node to NULL. This may not be necessary,
    * but better safe than sorry.
    */
    new_node->next = NULL;

    if (list_start == NULL)
    {
    // This is the reason for passing the pointer
    // by reference.
    list_start = new_node;
    }
    else
    {
    Node * prev = list_start;
    Node * n = list_start;

    /* Search for the end of the list, and maintain
    * a pointer to the previous node.
    */
    while (n != NULL)
    {
    prev = n;
    n = n->next;
    }

    /* At this point, "prev" points to the last node
    * in the list. Link the new node to the "prev"
    * node.
    */
    prev->next = n;
    }
    return;
    }

    The above function only appends a node to the list.
    The creating of a node has been extracted to make
    the functionality more generic so it can be used for
    programs that create nodes from files, databases, etc,
    not just from cin.

    Also note that the above function only deals with the
    link field and not the value field. That is why I have
    separated the two fields into separate classes.

    int main(void)
    {
    Integer_Node * list_head;
    Integer_Node * i_node;

    cout<<"Enter an integer, -1 to quit:";
    cout.flush();

    int a;
    while ((cin >> a) && (a != -1))
    {
    i_node = new Integer_Node;
    i_node->value = a;
    Append_To_List(list_head, i_node);

    cout<<"Enter an integer, -1 to quit:";
    cout.flush();
    }
    return 0;
    }

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Feb 11, 2005
    #3
    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:
    501
    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:
    493
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    530
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    693
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    787
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page