双向链接列表的运行时错误

时间:2018-03-09 16:55:54

标签: c++ oop pointers linked-list doubly-linked-list

所以,我试图用C ++实现一个双向链表。 它编译得很好,但它运行的那一刻,它会崩溃并退出。 它只是说"流程返回-1073741819"

(我使用Code :: Blocks和GNU GCC编译器顺便说一下)

我觉得问题在于构造函数,但我并不确定。我试过试验它,但无济于事。

任何想法可能会产生运行时错误?任何可能触发问题的指针(无双关语)都将不胜感激!

DNode.h

#ifndef DNODE_H_INCLUDED
#define DNODE_H_INCLUDED
#include <stdio.h>
#include <ostream>

class DNode{

private:
    int elem;
    DNode* next;
    DNode* prev;
    friend class DList;
public:
    DNode(int value=0){
        elem= value;
        next= NULL;
        prev = NULL;
    }
};


#endif // DNODE_H_INCLUDED

DList.h

#ifndef DLIST_H_INCLUDED
#define DLIST_H_INCLUDED
#include "DNode.h"

using namespace std;

class DList{

private:
    DNode* head;
    DNode* tail;

public:
    DList(){
        head = NULL;
        tail = NULL;
    }
    ~DList();
    void display();
    bool isEmpty() const;
    const int& getFront() const;
    const int& getRear() const;
    void addFront(const int& e);
    void addRear(const int& e);
    void removeFront();
    void removeRear();
    void addMiddleBefore(const int&e, const int& pos);
    void addMiddleAfter(const int&e, const int& pos);
    bool isIn(const int& e);
    void removeNode(const int& e);

};




#endif // DLIST_H_INCLUDED

DList.cpp

#include "DList.h"
#include "DNode.h"
#include <iostream>

using namespace std;

void DList::display(){
DNode* cur = head;
while((cur->next) != tail){
    cout<<cur->elem<<"->";
    cur = cur -> next;
}
cout<<endl;
}


bool DList::isEmpty()const {
DNode* cur = head;
if((cur->next) = tail){
    cout<<"Yep, the list is empty!"<<endl;
    return true;
}
else{
    cout<<"Nope,not empty"<<endl;
    return false;
}
}

const int& DList::getFront() const{
cout<<"The first element is " <<endl;
return head->elem;
}

const int& DList::getRear() const{
cout<<"The last element is "<<endl;
return tail->elem;
}

void DList::addFront(const int& e){
DNode* temp = new DNode(e);
DNode* cur = head;

temp->next = cur->next;
temp->prev = cur;
cur->next->prev = temp;
cur->next = temp;
cout<<"Added element to the front"<<endl;
}


void DList::addRear(const int& e){
DNode* temp = new DNode(e);
DNode* cur = tail;

temp->next = cur;
temp->prev = cur->prev;
cur->prev->next = temp;
cur->prev = temp;
cout<<"Added element to the rear"<<endl;
}

void DList::addMiddleBefore(const int&e, const int& pos){
DNode*temp = new DNode(e);
DNode *cur = head;

while((cur->elem) != pos ){
    cur = cur -> next;
}
temp->next = cur;
temp->prev = cur->prev;
cur->prev->next = temp;
cur->prev = temp;
cout<<"Added node"<<endl;
}

void DList::addMiddleAfter(const int&e, const int& pos){
DNode* temp = new DNode(e);
DNode* cur = head;

while((cur->elem) != pos){
    cur = cur -> next;
}

temp ->next = cur->next;
temp->prev = cur;
cur->next->prev = temp;
cur->next = temp;
cout<<"Added node"<<endl;

}

void DList::removeFront(){
DNode* temp = head;
temp->next->prev = head;
head = head -> next;
delete temp;
cout<<"Deleted Front"<<endl;
}


void DList::removeRear(){
DNode* temp = tail;
temp->prev->next = tail;
tail = tail -> prev;
delete temp;
cout<<"Deleted Rear"<<endl;
}

void DList::removeNode(const int & e){
DNode* cur = head;
while((cur->elem) != e){
    cur = cur -> next;
}
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
delete cur;
cout<<"Deleted node"<<endl;
}

bool DList::isIn(const int &e){
DNode* cur = head;
while((cur->next) != tail){
    if (cur->elem == e){
        cout<<"Found"<<endl;
        return true;
    }
cur = cur->next;
}
cout<<"Not found"<<endl;
return false;
}

DList::~DList(){
while((!isEmpty())) removeFront();
}

DNode.cpp

#include "DNode.h"

的main.cpp

#include "DList.h"
#include "DNode.h"
#include <iostream>

using namespace std;

DList newList;
int main()
{
    newList.addFront(50);
    newList.addRear(4);
    newList.display();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

让我的评论成为答案。

我发现至少有一个问题:当您创建新的DList时,head将以NULL开头。然后当你调用addFront时,你在这里取消引用空指针:

DNode* cur = head; 

temp->next = cur->next;

请注意,如果您通过调试器运行代码,则在大多数情况下应该告诉您崩溃发生的确切行,并且可以让您在崩溃时检查所有变量的值。绝对是一个有用的学习工具。