|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
access violation w/ linked list pointers
I am writing a method that will append a node onto the end of a linked list.
I am getting an "unhandled exception: access violation" from the following line of code: // temp pointer's next pointer needs to now point to new node temp->next = p; I attached a file with my method in it. I had to name it .java so it would upload correctly. If anyone has any suggestions, I'd appreciate it. ----------------------------------------- // insert user data at the end of the list bool LinkedList::append(const UserData& d){ // if head pointer points to null, then it is an empty list // node can be immediately added if (head == NULL){ cout << "This is an empty list."; // first step, create a new node to put information in // use 2 arg constructor so that your new node will have the same information as the argument's data/link // create the node with the d object as your data & point next to NULL indicating end of list Node* n = new Node(d, NULL); // point head pointer to new node head = n; // point tail pointer to new node since it is the end of the list tail = n; // increment node count by one count++; return true; // else head pointer does not point to null (list is not empty) } else if (head != NULL){ cout << "Trying to add a node to a list that is not empty" << endl; // traverse to last node (NOT all the way to NULL pointer) for (Node* temp = head; temp != NULL; temp = temp->next){ cout << "Is it even getting here?"; } // create a new node Node* p = new Node(d, NULL); // temp pointer's next pointer needs to now point to new node temp->next = p; // do not delete the temp pointer because it is still pointing to your new node & it would delete that also // increment node count by one count++; // point tail pointer to n tail = p; return true; } return false; } Last edited by lfindle : February 7th, 2003 at 04:47 PM. |
|
#2
|
||||
|
||||
|
I think your bug is here:
Code:
// traverse to last node (NOT all the way to NULL pointer)
for (Node* temp = head; temp != NULL; temp = temp->next){
cout << "Is it even getting here?";
}
Irrespective of what your comment says, you are traversing your way to the NULL pointer, not to the last node! IMHO it should say: Code:
// traverse to last node (NOT all the way to NULL pointer)
for (Node* temp = head; temp->next != NULL; temp = temp->next){
cout << "Is it even getting here?";
}
See the difference? ![]() Also, while you're at it, there's a scoping problem in your code, which may lead to non-portable code for other C++ compilers. I'm referring to your declaring a variable in a for loop and referring to it later outside the loop. In the above code you're declaring Node *temp in the for loop and then using it again a few lines below like this: // temp pointer's next pointer needs to now point to new node temp->next = p; Older C++ compilers would allow this, because the old C++ standard allowed it. However, the latest C++ standards dictate that the scope of the variable declared within the for loop does not exist outside the for loop. This means that a newer C++ compiler will refuse to compile the line temp->next = p;, saying that temp is an undeclared variable. See http://www.mozilla.org/hacking/port...ariables_in_for for more. Hope this helps! ![]() |
|
#3
|
|||
|
|||
|
From your code you appear to have pointer to the last node, so use this instead of searching for the last node.
Something like http://pastecode.net/?action=viewpost&tag=8. |
|
#4
|
|||
|
|||
|
Thank you both for the responses. That solved the problem.
I appreciate it. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > access violation w/ linked list pointers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|