Question

In: Computer Science

Can anyone explain to me why I am getting an access violation when I try to...

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);   
}

Solutions

Expert Solution

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


Related Solutions

Hello Everyone, Can anyone tell me why my program will not run? I am trying to...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to work on abstract base classes... not sure what is going on. import math from abc import ABC from abc import abstractmethod #TODO: convert this to an ABC class Shape(ABC): def __init__(self): self.name = "" def display(self): print("{} - {:.2f}".format(self.name, self.get_area())) #TODO: Add an abstractmethod here called get_area @abstractmethod def get_area(self): if self.name == "Circle": get_area()= 3.14 * radius * radius else: get_area() = self.length...
Can someone please tell me why I am getting errors. I declared the map and it's...
Can someone please tell me why I am getting errors. I declared the map and it's values like instructed but it's telling me I'm wrong. #include <iostream> #include <stdio.h> #include <time.h> #include <chrono> #include <string> #include <cctype> #include <set> #include <map> #include "d_state.h" using namespace std; int main() { string name; map<string,string> s; map<string,string>::iterator it; s[“Maryland”] = "Salisbury"; s[“Arizona”] = "Phoenix"; s[“Florida”] = "Orlando"; s[“Califonia”] = "Sacramento"; s[“Virginia”] = "Richmond"; cout << "Enter a state:" << endl; cin >> name;...
Can anyone explain to me how I solve this, I need to prepare a journal entry...
Can anyone explain to me how I solve this, I need to prepare a journal entry for the given problem: on jan 1 a corporation issued $325,000, 5.5%, 9 year bonds when the market rate was 6%. Interest is to be paid annually on each Jan 1
Hello All, anyone can explain these to me? I. What’s the effect of increasing the capacitance?...
Hello All, anyone can explain these to me? I. What’s the effect of increasing the capacitance? in term of saturation mode II. What’s the effect of decreasing the capacitance? in term of saturation mode III. Does dose saturation mode mean a short circuit? IV. What will happen to the capacitor under saturation mode? V. What's the relationship between the temp and the capacitance? curious to know the answers for these questions
Can anyone please explain this table for me please. I need it very badly. thanks The...
Can anyone please explain this table for me please. I need it very badly. thanks The 95% confidence intervals for the student using cannabis in the last 12 months is as follows: Point Estimate Upper Limit Lower Limit Male 0.27 0.05 Female 0.35 0.09 Total 0.28 0.10
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
can anyone tell me the reason why if the number of basis is equal to the...
can anyone tell me the reason why if the number of basis is equal to the dimension of a vector space V then it is the basis of the vector space V. and also what the theorem is? since, I think if V=span{(1,0,0), (0,1,0)} and dim(V)=2 and basis={(1,0,0), (0,0,1)} which the number is also 2. but it is not the basis of V. So, can you tell where is the mistake. THANK YOU!
Exam prep question I am struggling with. Could anyone give me a solution with steps? Thank...
Exam prep question I am struggling with. Could anyone give me a solution with steps? Thank you! You are an executive at WX Resorts and you have executive stock options as part of your compensation. You hold 1,000 options that expire six months from today at a strike price of 50 but that can be exercised early. WX is trading at $55 and just announced a $1 dividend per share that will be paid four months from today to stockholders...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
Can someone explain in detail how to solve the below indefinite integral? I am getting really...
Can someone explain in detail how to solve the below indefinite integral? I am getting really confused when it comes to the U-substitution part and do not understand how 9 ends up in the denominator. I know the correct answer is [-2(ln3x+1)-3x]/9 + C, but I do not know how to get there. ∫(2x)/(3x+1) dx
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT