我试图在LinkedList.ccp文件中测试我的删除方法。当我处于调试模式时,它会完全删除列表中的所有4个,但后来我收到了这条消息:
1 [main] trainlinkedlist 9904 exception::handle: Exception: STATUS_ACCESS_VIOLATION
614 [main] trainlinkedlist 9904 open_stackdumpfile: Dumping stack trace to trainlinkedlist.exe.stackdump
我需要做些什么才能解决这个问题?
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
#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
#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;
}
}
答案 0 :(得分:0)
您没有进行足够的检查,可能会取消引用NULL指针。
while(p->data==x)
首先检查p
是否为0。
if(p->next->data==x)
首先检查p->next
是否为0。