Chào mọi người, hiện tại em đang học lập trình C++. Tới bài linklist,viết code chạy thử thì ra lỗi stop working
Như vậy thì liệu code em có viết đúng không ạ? Làm sao để khắc phục tình trạng này?
Em đang dùng codeblock.
sourse code của em:
node.h
#ifndef NODE_H
#define NODE_H
class node
{
public:
node(void);
void setValue(int val);
void setNext(node*const next);
void setPrev(node*const prev);
int getValue()const;
node*getNext()const;
node*getPrev()const;
bool hasNext()const;
bool hasPrev()const;
virtual ~node(void);
protected:
private:
int value_;
node*next_;
node*prev_;
};
#endif // NODE_H
node.cpp
#include "node.h"
#include <iostream>
node::node(void)
{
this->value_=0;
this->next_=0;
this->prev_=0;
}
void node::setValue(int val)
{
this->value_=val;
}
void node::setNext(node*const next)
{
std::cout<<value_<<"->"<<next_->value_<<std::endl;
this->next_=next;
}
void node::setPrev(node*const prev)
{
std::cout<<prev_->value_<<"<-"<<value_<<std::endl;
this->prev_=prev;
}
int node::getValue()const
{
return this->value_;
}
node*node::getNext()const
{
if(this->hasNext())
{
return this->next_;
}
else
return 0;
}
node*node::getPrev()const
{
if(this->hasPrev())
{
return this->prev_;
}
else
return 0;
}
bool node::hasNext()const
{
return this->next_!=0;
}
bool node::hasPrev()const
{
return this->prev_!=0;
}
node::~node()
{
//dtor
}
main.cpp
#include <iostream>
#include "node.h"
using namespace std;
int main()
{
node*first=new node;
first->setValue(0);
node*currentnode=first;
for(int i=1;i<4;i++)
{
node*newnode=new node;
newnode->setValue(i);
currentnode->setNext(newnode);
newnode->setPrev(currentnode);
currentnode=newnode;
}
return 0;
}
Em cảm ơn ạ.