//根据程序输出结果完成类Stack的Pop()成员函数的类外定义
#include
#include
template class Stack;
template class StackNode
{
friend class Stack;
private:
Type data;
StackNode *next;
public:
StackNode(Type d=0,StackNode *L=NULL):data(d),next(L)
{}
};
template class Stack
{
private:
StackNode *top;
public:
Stack():top(NULL){}
~Stack();
void Push(const Type & item);
Type Pop();
Type GetTop();
int IsEmpty() const{return top==NULL;}
void MakeEmpty();
};
template Stack::~Stack(){
//逐次删去链式栈中的元素直至栈顶指针为空
StackNode *p;
while(top!=NULL){p=top;top=top->next;delete p;}
}
template void Stack::Push(const Type & item){
//将元素值item插入到链式栈的栈顶
top=new StackNode(item,top);
}
//请完成如下Pop()函数的定义
template Type Stack::Pop()
{
//删除栈顶结点,返回被删栈顶元素的值
}
template Type Stack::GetTop(){
//返回栈顶元素的值
if(IsEmpty()) return NULL;
return top->data;
}
void main()
{
Stack s1;
cout<
for(int i=0;i<10;i++)
s1.Push(i+1);
while(!s1.IsEmpty())
cout< cout<}
/*程序输出结果为:
0
10 9 8 7 6 5 4 3 2 1
*/
--------------------next---------------------
阅读(973) | 评论(0) | 转发(0) |