双链接列表的运行时错误 所以,我尝试在C++中实现一个双链表。 它编译得很好,但一旦运行,就会崩溃并退出。 它只是说“返回的进程-1073741819”
(我正在使用Code::Blocks和GNU GCC编译器btw) 我有一种感觉,问题在于构造器,但我不是很确定。我试过用它做实验,但没有用 你知道是什么导致了运行时错误吗?任何可能引发问题的提示(并非双关语)都将不胜感激 DNode.h双链接列表的运行时错误 所以,我尝试在C++中实现一个双链表。 它编译得很好,但一旦运行,就会崩溃并退出。 它只是说“返回的进程-1073741819”,c++,oop,pointers,linked-list,doubly-linked-list,C++,Oop,Pointers,Linked List,Doubly Linked List,(我正在使用Code::Blocks和GNU GCC编译器btw) 我有一种感觉,问题在于构造器,但我不是很确定。我试过用它做实验,但没有用 你知道是什么导致了运行时错误吗?任何可能引发问题的提示(并非双关语)都将不胜感激 DNode.h #ifndef DNODE_H_INCLUDED #define DNODE_H_INCLUDED #include <stdio.h> #include <ostream> class DNode{ private: in
#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.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();
}
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;
}
#包括“DList.h”
#包括“DNode.h”
#包括
使用名称空间std;
dlistnewlist;
int main()
{
新列表。addFront(50);
新增列表。添加后(4);
newList.display();
返回0;
}
回答我的评论
我至少看到一个问题:当您创建一个新的
DList
时,head
以NULL
开始。然后,当调用addFront
时,在此处取消引用该空指针:
DNode* cur = head;
temp->next = cur->next;
请注意,如果通过调试器运行代码,在大多数情况下,调试器应该告诉您崩溃发生的确切行,并允许您在崩溃时检查所有变量的值。绝对是一个有用的学习工具。无关:编译器可能会在以下方面发出警告:
if((cur->next)=tail){
不要忽略警告。它们是编译器告诉你可能有一个简单的逻辑错误。当你创建一个新的DList
时,head
以NULL
开始。然后当你调用addFront
时,你在这里取消引用空指针:DNode*cur=head;
temp->next=cur->next;
答案是@0x5453。一些有用的阅读:@0x5453-Yikes,我明白了。刚刚修好。谢谢!
#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;
}
DNode* cur = head;
temp->next = cur->next;