By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

creating linked list

P: n/a
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;

}

}


Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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" <ch*******@yahoo.com> ????
news:60******************************@localhost.ta lkaboutprogramming.com...
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;

}

}

Jul 23 '05 #2

P: n/a
ok

Jul 23 '05 #3

P: n/a
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

Jul 23 '05 #4

P: n/a
thanks let me see if that works

Jul 23 '05 #5

P: n/a
i think the code u wrote does not set NULL if the list is empty.let me see
what i can do.

Jul 23 '05 #6

P: n/a
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

Jul 23 '05 #7

P: n/a
thanks

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.