简体   繁体   English

我收到STATUS_ACCESS_VIOLATION(C ++)

[英]I'm getting a STATUS_ACCESS_VIOLATION (C++)

I'm trying to test out my delete method in the LinkedList.ccp file. 我正在尝试在LinkedList.ccp文件中测试我的删除方法。 When I'm in debug mode it completly deletes all of the 4s from the the list but then I get this message: 当我处于调试模式时,它会从列表中完全删除所有的4,但是我得到以下消息:

  1 [main] trainlinkedlist 9904 exception::handle: Exception: STATUS_ACCESS_VIOLATION
614 [main] trainlinkedlist 9904 open_stackdumpfile: Dumping stack trace to trainlinkedlist.exe.stackdump

Any ideas what I need to do to fix this? 有什么想法需要解决吗?

main.ccp main.ccp

#include "LinkedList.h"
#include <iostream>     //ADDED BECAUSE IT IS NEED FOR PROGRAM TO RUN
#include <cstdlib>
using namespace std;    //ADDED BECAUSE IT IS NEED FOR PROGRAM TO RUN

/*
 * 
 */

int main() {
    LinkedList Train;
    srand(time(0));
        Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
        Train.InsertAtEnd(4);
    Train.InsertAtEnd(5);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    for(int i=0;i<5;i++){
        Train.InsertAtEnd((rand()%20));
    }
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
        Train.InsertAtEnd(35);
    Train.InsertAtEnd(2);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
        Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(5);
    Train.InsertAtEnd(4);
        Train.InsertAtEnd(4);
    Train.InsertAtEnd(6);
    Train.InsertAtEnd(4);
    Train.InsertAtEnd(4);
    Train.Delete(4);
    Train.Display();
    return 0;
}

LinkedList.h LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

typedef int ElementType;
struct Node {
    ElementType data;
    Node * next;
};

class LinkedList
{
 public: 
   LinkedList();//create an empty list
   bool empty(); // return true if the list is empty, otherwise return false
   void InsertAtEnd(ElementType x);//insert a value x at the end of list
   void Delete(ElementType x); //if value x is in the list, remove x
   void Display();// Display the data values in the list
  private:
Node * first;   //The first node in a Linked list
};

#endif  /* LINKEDLIST_H */

LinkedList.ccp LinkedList.ccp

#include "LinkedList.h"
#include <iostream>
using namespace std;

/**
 * LinkedList() sets first as a Dummy Node
 */
LinkedList::LinkedList() {
    first = NULL;
}

bool LinkedList::empty() {
    if (first == NULL) {  //when this is true the list is empty
        return true;
    } else {
        return false;   //when this is false the list is NOT empty
    }
}

void LinkedList::InsertAtEnd(ElementType x) {
    Node *p;
    p = first;
    if(empty()){
        p = new Node;
        p->data = x;
        p->next = NULL;
        first=p;
        return;
    }
    while (p != NULL) { //this while loop looks for the end of the list
        if (p->next == NULL) {  //this is a check for if node p is the last node in the list
            p->next = new Node; //a new node is created at the end of the list with data value of x and the next is NULL
            p->next->data = x;
            p->next->next = NULL;
            break;      //breaks the while loop when a new node is added
        }
        p = p->next;
    }
}

void LinkedList::Delete(ElementType x) {
    Node *p;    //p is used to the find Node that will be deleted
    Node *q;    //q keeps track of the node before the one that is deleted
    p = first;
    q = NULL;
    while(p->data==x){
        first=first->next;
        p->data =0;
        p->next=NULL;
        delete p;
        p=first;
    }
    while(p!=NULL){     //this while loop goes through the list
        if(p->next->data==x){
            q=p->next;
            p->next=p->next->next;
            q->data=0;
            q->next=NULL;
            delete q;
            q=NULL;
        }else{
            p=p->next;
        }
//        if(p->data==x){ //check for the node that needs to be deleted
//            q->next=p->next;    //connects q's next to the p's next
//            delete p;   //deletes the node p points to
//            p->next= NULL;
//        }
//        q=p;
//        p=p->next;
    }
}

void LinkedList::Display() {
    Node *p;
    p = first;
    while (p != NULL) {
        if(p->data==-1){        //check used to skip the dummy node
            p=p->next;
            continue;
        }
        cout << p->data << " "; //prints out all the values in the list
        p = p->next;
    }
}

You aren't making enough checks and likely dereferencing NULL pointers. 您没有进行足够的检查,并且可能会取消引用NULL指针。

while(p->data==x) does not first check whether or not p is 0. while(p->data==x)不会首先检查p是否为0。

if(p->next->data==x) does not first check whether or not p->next is 0. if(p->next->data==x)不首先检查p->next是否为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM