首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于数据结构栈跟队列c++程序中的析构函数

2013-10-24 
关于数据结构栈和队列c++程序中的析构函数下面程序把析构函数的内容注释之后,程序运行没问题,可是一旦加上

关于数据结构栈和队列c++程序中的析构函数
下面程序把析构函数的内容注释之后,程序运行没问题,可是一旦加上去,就出现内存错误,可是由于本人刚学习不久,还搞不清楚内部是什么问题,求大虾解释:
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
};

class lstack
{
private:
node *top;
public:
lstack(){top=NULL;}
~lstack();
int isempty();
void display();
void push(int x);
void pop();

};

 lstack::~lstack()
{
/*node *p,*q;
//p=new node;
p=top;
//q=new node;
q=top->next;
while(p!=NULL)
{
delete p;
p=q;
q=q->next;

}
top=NULL;*/
}

int lstack::isempty()
{
if(top==NULL)return 1;
else return 0;
}

void lstack::display()
{
node *p;
p=top;
while(p!=NULL)
{
cout<<"\t"<<p->data<<endl;
p=p->next;
}
cout<<"输出结束"<<endl;
}

void lstack::push(int x)
{
node *p;
p=new node;
p->data=x;
p->next=top;
top=p;

}
void lstack::pop()
{
int x;
if(top!=NULL)
{
x=top->data;
top=top->next;
cout<<"出栈数据x="<<x<<endl;
}
else
{
cout<<"栈为空"<<endl;

}
}

void main()
{
lstack h;

h.isempty();
int count;
cout<<"输入进栈次数"<<endl;
cin>>count;
while(count)
{
int x;
cout<<"输入进栈数据"<<endl;
cin>>x;
h.push(x);
count--;
}


h.pop();

h.display();/**/

}

热点排行