2013-02-28 44 views
0

我想在LinkedList.ccp文件中測試我的刪除方法。當我在調試模式下是完全地它刪除所有從列表中4S的但後來我得到這個消息:我得到一個STATUS_ACCESS_VIOLATION(C++)

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

優先使用的名稱,如'is_empty',而不是按照標準庫的命名混亂實踐。 'empty'是一個動詞。 – 2013-02-28 20:51:27

+0

爲另一個類似的問題我張貼短[鏈表教程(http://stackoverflow.com/questions/14993882/linked-list-and-reading-from-text-file/14994011#14994011) – 2013-02-28 20:57:36

+0

'使用命名空間std ;'不是「需要程序運行」。相信我。 – 2013-02-28 23:08:46

回答

0

你沒有取得足夠的檢查,並可能取消引用NULL指針。

while(p->data==x)不首先檢查p是否爲0。

if(p->next->data==x)不首先檢查p->next是否爲0。

+0

提問者的回覆:這可能是問題,但我還沒有弄清楚如何解決這個問題。有任何想法嗎? – user2121217 2013-02-28 21:25:08

+0

如果你不明白指針或鏈接列表,你需要先了解它們,或許,研究它們並對它們進行可視化。瞭解他們是你的線索。 – 2013-02-28 21:33:59

+0

我對兩者都有基本的瞭解。我一直在使用C++一個月,我試圖解決這個問題,因爲它是在8小時內完成的任務。 – user2121217 2013-02-28 21:45:54

相關問題