在链表派生类中的退栈函数 引起程序卡住 请问原因
/*****结点类 node*****/
#pragma once
#include <iostream>
using namespace std;
class node
{
friend class linklist;
friend class stack;
public:
node();
node(node &n);
node(int i,char c= '0 ');
node(int i,char c,node *p,node *n);
~node();
private:
int idata;
char cdata;
node *prior;
node *next;
};
node::node()
{
cout < < "结点类构造函数I运行中... " < <endl;
idata=0;
cdata= '0 ';
prior=NULL;
next=NULL;
}
node::node(int i,char c)
{
cout < < "结点类构造函数II运行中... " < <endl;
idata=i;
cdata=c;
prior=NULL;
next=NULL;
}
node::node(int i, char c, node *p, node *n)
{
cout < < "结点构造类函数III运行中... " < <endl;
idata=i;
cdata=c;
prior=p;
next=n;
}
node::node(node &n)
{
idata=n.idata;
cdata=n.cdata;
prior=n.prior;
next=n.next;
}
node::~node()
{
cout < < "结点类析构函数运行中... " < <endl;
}
/******链表类 linklist*******/
#pragma once
#include "node.h "
class linklist
{
public:
linklist(int i,char c);
linklist(linklist &l);
~linklist();
bool locate(int i);
bool locate(char c);
bool insert(int i,char c);
bool Delete();
void show();
void Destroy();
protected:
node head;
node *pcurrent;
};
linklist::linklist(int i,char c):head(i,c)
{
cout < < "链表构造函数运行中... " < <endl;
pcurrent=&head;
}
linklist::linklist(linklist &l):head(l.head)
{
cout < < "链表深拷贝函数运行中... " < <endl;
pcurrent=&head;
node *temp=l.head.next;
while(temp!=NULL)
{
node *temp1=new node(temp-> idata,temp-> cdata,pcurrent,NULL);
pcurrent-> next=temp1;
pcurrent=pcurrent-> next;
temp=temp-> next;
}
}
linklist::~linklist()
{
cout < < "链表类析构函数运行中... " < <endl;
Destroy();
}
bool linklist::locate(int i)
{
node *temp=&head;
while(temp!=NULL)
{
if(temp-> idata==i)
{
return true;
}
temp=temp-> next;
}
return false;
}
bool linklist::locate(char c)
{
node *temp=&head;
while(temp!=NULL)
{
if(temp-> cdata==c)
{
return true;
}
}
return false;
}
bool linklist::insert(int i, char c)
{
if(pcurrent!=NULL)
{
node *temp=new node(i,c,pcurrent,pcurrent-> next);
if(pcurrent-> next!=NULL)
pcurrent-> next-> prior=temp;
pcurrent-> next=temp;
return true;
}
return false;
}
bool linklist::Delete()
{
if(pcurrent!=NULL && pcurrent!=&head)
{
node *temp=pcurrent;
if(temp-> next!=NULL)
{
temp-> next-> prior=pcurrent-> prior;
}
temp-> prior-> next=pcurrent-> prior;
pcurrent=temp-> prior;
delete temp;
return true;
}
else
{
return false;
}
}
void linklist::show()
{
node *temp=&head;
while(temp!=NULL)
{
cout < <temp-> idata < < '\t ' < <temp-> cdata < <endl;
temp=temp-> next;
}
}
void linklist::Destroy()
{
node *temp=head.next;
node *temp1=NULL;
while(temp!=NULL)
{
temp1=temp;
temp=temp-> next;
delete temp1;
}
head.next=NULL;
}
/****以私有继承链表类的栈类 stack ****/
#pragma once
#include "linklist.h "
class stack :
private linklist
{
public:
/*stack(void)
{
cout < < "不带参数构造函数运行中... " < <endl;
}*/
stack(int i,char c):linklist(i,c)
{
cout < < "栈类带参数构造函数运行中... " < <endl;
}
bool push(int i,char c)
{
while(pcurrent-> next!=NULL)
pcurrent=pcurrent-> next;
return insert(i,c);
}
bool pop(int &i,char &c)
{
while(pcurrent-> next!=NULL)
pcurrent=pcurrent-> next;
i=pcurrent-> idata;
c=pcurrent-> cdata;
return Delete();
}
void show()
{
linklist::show();
}
~stack(void)
{
cout < < "栈类析构函数运行中... " < <endl;
}
};
/******main.CPP******/
// 私有的实现继承.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h "
#include "stack.h "
int _tmain(int argc, _TCHAR* argv[])
{
stack ss(0, '0 ');
int i,j;char c;
for(j=0;j <1;++j)
{
cout < < "请输入一个数字和一个字母: ";
cin> > i> > c;
if(ss.push(i,c))
{
cout < < "压栈成功! " < <endl;
}
}
ss.show();
while(ss.pop(i,c))
{
cout < < "退栈数据为i= " < <i < < '\t ' < < "c= " < <c < <endl;
}
getchar();
return 0;
}
为什么 运行程序 程序输出了 第一个退出栈结点的 并输出idata与cdata便不继续运行了?
[解决办法]
Delete()函数错误。