In: Computer Science
Can anyone explain to me why I am getting an access violation when I try to add notes? It occurs when I try to set the "pLast" pointer to the previous node (for a doubly linked list). The code worked as singly linked list. When I added a "pLast" pointer and the needed code to make it a doubly linked list everything broke. #include <iostream> #include <stdlib.h> using namespace std; //build class that has a private function to inc count. class LinkedListCount { private: struct CountNode { int data; int countVar; //Make the variable "countVar" private to protect integrity. CountNode* pNext = NULL; CountNode* pLast = NULL; //Needed for bubbling back through list. }; //Keep track of head CountNode* head; CountNode* current; CountNode* temp; public: //Constructor Function (Set default values for head, current, and temp) LinkedListCount() { head = NULL; current = NULL; temp = NULL; } void AddNode(int dataIn) { //Addnode Function //Create and populate list. CountNode* newNode = new CountNode; newNode->pNext = NULL; newNode->pLast = NULL; newNode->data = dataIn; temp = head; newNode->countVar = 0; if (temp != NULL) { //We already have data entery. if (temp->pNext == NULL) { newNode = temp->pNext; newNode->pLast = head; //****THIS IS WHERE ACCESS VIOLATION OCCURES } //Set variables with the understanding that the head is the only data point. else { current = temp->pNext; //Set it equal to head. } while (current->pNext != NULL) {//This could be eliminated with keeping track of a tail. current = current->pNext; //Attach this to the end of the list. } current->pNext = newNode; //And newMode->pNext = to Null so next time I add data I'll get to the end of the list. newNode->pLast = current; } else if (head == NULL) { head = newNode; } } }; void addNodes(LinkedListCount &DataList) { //Populates list. for (int i = 0; i < 20; i++) { DataList.AddNode(i); } } int main(void) { addNodes(DataList); }
Solution:
Program modified by solving the access violation issue. The issue is with assigning the node to temp.
Highlighted code contains the modified code.
---------------------------------------------
Source Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
//build class that has a private function to inc count.
class LinkedListCount {
private:
struct CountNode {
int data;
int countVar; //Make the
variable "countVar" private to protect integrity.
CountNode* pNext =
NULL;
CountNode* pLast = NULL;
//Needed for bubbling back through list.
};
//Keep track of head
CountNode* head;
CountNode* current;
CountNode* temp;
public:
//Constructor Function (Set default values for
head, current, and temp)
LinkedListCount() {
head = NULL;
current = NULL;
temp = NULL;
}
void AddNode(int dataIn) { //Addnode
Function
//Create and populate
list.
CountNode* newNode = new
CountNode;
newNode->pNext =
NULL;
newNode->pLast =
NULL;
newNode->data =
dataIn;
temp = head;
newNode->countVar =
0;
if (temp != NULL) { //We
already have data entery.
if (temp->pNext == NULL) {
temp->pNext = newNode;
newNode->pNext = temp; //Modified....
return;
}
//Set variables with the understanding that the head is the only
data point.
else {
current = temp; //Set it equal to head.
}
while (current->pNext != head) {//This could be eliminated with
keeping track of a tail.
current = current->pNext; //Attach this
to the end of the list.
}
current->pNext = newNode; //And newMode->pNext = to Null so
next time I add data I'll get to the end of the list.
newNode->pNext = head;
}
else if (head == NULL)
{
head = newNode;
}
}
};
void addNodes(LinkedListCount &DataList) { //Populates list.
for (int i = 0; i < 20; i++) {
DataList.AddNode(i);
}
}
int main(void)
{
LinkedListCount
DataList;
addNodes(DataList);
}
---------------------------------------------------------------
Output:
------------------------------------------------
Kindly up-vote if you are satisfied with this solution.
Node data: 0 Node data: 1 Node data: 2 Node data: 3 Node data: 4 Node data: 5 Node data: 6 Node data: 7 Node data: 8 Node data: 9 Node data: 10 Node data: 11 Node data: 12 Node data: 13 Node data: 14 Node data: 15 Node data: 16 Node data: 17 Node data: 18 Node data: 19