-
June 26th, 2009, 08:50 AM
#1
Adding an element in Linked List
Hello to all..
I'm having a little problem with my program about linked list...when adding an element at the end of the list for a second time, a window that says that the visual c++ 6.0 had an error and needs to be close..i already thought lots of other ways to solve the problem but it just keeps happening again and again..
i'll be posting the code here:
Code:
void FunctionAddData(int newData)
{
node *currentPointer = NULL;
node *last = NULL;
currentPointer = root;
node *tempNode = new node;
cout<<"Enter a number: ";
cin>>tempNode -> data;
tempNode -> next = NULL;
if(tempNode == NULL)
return;
else
{
tempNode -> next = NULL;
if(root == NULL)
{
root = tempNode;
last = tempNode;
}
else
{
last -> next = tempNode;
last = tempNode;
}
}
main();
return;
}
-
June 26th, 2009, 08:54 AM
#2
Re: Adding an element in Linked List
It's generally helpful to tell us what the error is and what line it occurred on.
I see you're calling main directly which is something you should never do. I'm not ever sure why you'd want to in a function that adds a node to a list.
-
June 26th, 2009, 09:05 AM
#3
Re: Adding an element in Linked List
Originally Posted by gamer1127
Code:
node *last = NULL;
// ...
if(root == NULL)
{
root = tempNode;
last = tempNode;
}
else
{
last -> next = tempNode;
last = tempNode;
}
As last is NULL (you are never setting it to anything else), accessing last->next will crash your program.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
June 26th, 2009, 09:07 AM
#4
Re: Adding an element in Linked List
Code:
void FunctionAddData(int newData) // 'newData' is not used.
{
node *currentPointer = NULL;
node *last = NULL;
currentPointer = root;
node *tempNode = new node;
cout<<"Enter a number: ";
cin>>tempNode -> data;
tempNode -> next = NULL;
if(tempNode == NULL) // If this was true then the previous two lines would have been 'undefined behaviour' (or crash)
return;
else
{
tempNode -> next = NULL; // You did that a few lines ago!
if(root == NULL)
{
root = tempNode; // I assume 'root' is some global
last = tempNode; // This is pointless as 'last' has only function scope and will 'disappear' on return.
}
else
{
last -> next = tempNode; // 'last' has no value set here.
last = tempNode;
}
}
main(); // 'main' should not be called.
return;
}
What you should be doing is looping through the 'next' pointers starting at 'root' trying to find the last in the list and then adding your new node to this.
Assuming C++, if this function were part of a link list class then the 'root' and 'last' nodes could be stored as private member variables.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|