Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to add at the end of the linked list

Reply
Thread Tools

how to add at the end of the linked list

 
 
chirag
Guest
Posts: n/a
 
      02-11-2005
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;

}
}

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-11-2005
chirag wrote:

> 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?

> }
> }


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      02-11-2005
chirag wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments