【问题标题】:Run Time Error with a Doubly Linked List双向链表的运行时错误
【发布时间】:2018-03-09 16:55:54
【问题描述】:

所以,我尝试在 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;
}

【问题讨论】:

  • 无关:编译器可能会发出警告:if((cur-&gt;next) = tail){ 不要忽略警告。它们是编译器,告诉你可能有一个简单的逻辑错误。
  • 当您创建一个新的DList 时,headNULL 开头。然后,当您调用addFront 时,您在此处取消引用该空指针:DNode* cur = head;temp-&gt;next = cur-&gt;next;
  • 这应该足以让你在那里得到答案,@0x5453。
  • 一些有用的阅读:How to debug small programs
  • @0x5453 哎呀,我明白了。刚刚修好了。谢谢!

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


【解决方案1】:

让我的评论成为答案。

我至少看到一个问题:当您创建一个新的DList 时,headNULL 开头。然后当您调用addFront 时,您在此处取消引用该空指针:

DNode* cur = head; 

temp->next = cur->next;

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

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多