creating linked list

C

chirag

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;

}

}
 
M

MatrixV

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.
 
T

Thomas Matthews

chirag said:
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
 
C

chirag

i think the code u wrote does not set NULL if the list is empty.let me see
what i can do.
 
T

Thomas Matthews

chirag said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top