数据结构链式栈的输入输出(c++)
#include
template
class link
{
public:
T date;
link *next;
link(const T info, link *nextvalue=NULL) {
date=info;
next=nextvalue;
}
link(link *nextvalue)
{
next=nextvalue;
}
};
template
class inkstack
{
private:
link *top;
int size;
public:
inkstack();
~inkstack();
void clear();
bool push(const T item);
bool pop(T & item);
bool toop(T & item);
};
template
inkstack::inkstack()
{
top=NULL;
size=0;
}
template
inkstack::~inkstack()
{
clear();
}
template
void inkstack::clear()
{
while(top!=NULL)
{
link *tmp=top;
top=top->next;
delete top;
}
size=0;
}
template
bool inkstack::push(const T item)
{
link *tmp=new link(item,top);
top=tmp;
size++;
return true;
}
template
bool inkstack::pop(T & item)
{
link *tmp;
if(size==0)
{
cout
}
item=top->date;
tmp=top->next;
delete top;
top=tmp;
size--;
return true;
}
template
bool inkstack::toop(T & item)
{
if(size==0)
{
cout
}
item=top->date;
return true;
}
void main()
{
inkstack b; int i,a[10],c,d; for(i=0;i
cin>>a[i]; b.push(a[i]); }
for(i=0;i
b.pop(c);
cout
b.toop(c); }