creating linked list

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

  1. chirag

    chirag Guest

    hi i have following code. i m confused about where do i write p-> next
    =NULL once user enters
    -1 to exit. help is appriciated. because what i have right now it sets =
    NULL after the first integer.

    #include <iostream>
    #include <cassert>

    using std::cin;
    using std::cout;
    using std::endl;

    struct Node
    // Definition of Node type
    {
    int item;

    Node *next;
    };

    Node *enterNewLinkedList()
    // Reads a series of integers from the keyboard (ending with -1) and
    // returns a pointer to a linked list containing those integers, or
    // NULL if the list is empty. No error checking is done.
    {

    int a;

    cout<<"Enter a series of integers :"<<endl;
    cin>>a;

    if ( a!=-1)
    {
    Node *p;
    p = new Node;
    (*p).item = a;
    p->item = a;
    p->next= new Node;

    p->next=NULL;

    }

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

  2. chirag

    MatrixV Guest

    I don't quite understand you. This codes is really a mess.
    I've tried a lot to correct it, but I failed. Forgive me, your codes is
    totally wrong.


    "chirag" <> ????
    news:...
    > hi i have following code. i m confused about where do i write p-> next
    > =NULL once user enters
    > -1 to exit. help is appriciated. because what i have right now it sets =
    > NULL after the first integer.
    >
    > #include <iostream>
    > #include <cassert>
    >
    > using std::cin;
    > using std::cout;
    > using std::endl;
    >
    > struct Node
    > // Definition of Node type
    > {
    > int item;
    >
    > Node *next;
    > };
    >
    > Node *enterNewLinkedList()
    > // Reads a series of integers from the keyboard (ending with -1) and
    > // returns a pointer to a linked list containing those integers, or
    > // NULL if the list is empty. No error checking is done.
    > {
    >
    > int a;
    >
    > cout<<"Enter a series of integers :"<<endl;
    > cin>>a;
    >
    > if ( a!=-1)
    > {
    > Node *p;
    > p = new Node;
    > (*p).item = a;
    > p->item = a;
    > p->next= new Node;
    >
    > p->next=NULL;
    >
    > }
    >
    > }
    >
    >
    >
    >
    MatrixV, Feb 11, 2005
    #2
    1. Advertising

  3. chirag

    chirag Guest

    ok
    chirag, Feb 11, 2005
    #3
  4. chirag wrote:
    > hi i have following code. i m confused about where do i write p-> next
    > =NULL once user enters
    > -1 to exit. help is appriciated. because what i have right now it sets =
    > NULL after the first integer.
    >
    > #include <iostream>
    > #include <cassert>
    >
    > using std::cin;
    > using std::cout;
    > using std::endl;
    >
    > struct Node
    > // Definition of Node type
    > {
    > int item;
    >
    > Node *next;
    > };
    >
    > Node *enterNewLinkedList()
    > // Reads a series of integers from the keyboard (ending with -1) and
    > // returns a pointer to a linked list containing those integers, or
    > // NULL if the list is empty. No error checking is done.
    > {
    >
    > int a;
    >


    Node * list_start = 0;
    cout<<"Enter an integer, -1 to quit:";
    cout.flush();
    while ((cin >> a) && (a != -1))
    {
    > Node *p;
    > p = new Node;
    > (*p).item = a;

    or: p->item = a;

    p->next = list_start;
    list_start = p;
    cout << "Enter an integer, -1 to quit:";
    cout.flush();
    }
    return list_start;
    }

    --
    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
    #4
  5. chirag

    chirag Guest

    thanks let me see if that works
    chirag, Feb 11, 2005
    #5
  6. chirag

    chirag Guest

    i think the code u wrote does not set NULL if the list is empty.let me see
    what i can do.
    chirag, Feb 11, 2005
    #6
  7. chirag wrote:
    > i think the code u wrote does not set NULL if the list is empty.let me see
    > what i can do.
    >


    These two lines:
    Node * list_start = 0;
    and
    return list_start;

    Sets the list to null if the user does not enter anything.

    Try single-stepping through it with your debugger.

    --
    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
    #7
  8. chirag

    chirag Guest

    thanks
    chirag, Feb 11, 2005
    #8
    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:
    463
    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:
    453
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    734
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page