how to add at the end of the linked list

C

chirag

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;

}
}
 
R

Rolf Magnus

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

Thomas Matthews

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

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top