473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

creating linked list

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
7 2641
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
ok

Jul 23 '05 #3
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
thanks let me see if that works

Jul 23 '05 #5
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
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
thanks

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
2
by: Ross Clement (Email address invalid - do not use) | last post by:
Hi. I would like to make a typedef for a structure which can be joined up into a linked list. I include my code below. My code works, but I have a vague suspicion that I could have declared my...
7
by: alternativa | last post by:
Hello, I'm a beginner in C programming and I have a problem that probably will seem trivial to most of you, however I can't find a solution... So, I have to write a data base - program should ask...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
6
by: Suyash Upadhyay | last post by:
Hi All, As a beginner in Computer Programming, I decided to create Linked List using recursion, but I am not getting right answer. I think it is fundamentally correct, but I am stuck. ...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.