0

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

1 に答える 1

0

十分なチェックを行っておらず、NULL ポインターを逆参照している可能性があります。

while(p->data==x)pが 0かどうかを最初にチェックしません。

if(p->next->data==x)p->nextが 0かどうかを最初にチェックしません。

于 2013-02-28T20:59:51.033 に答える