📄 stack.h
字号:
///////////////////////////
// //
// 堆栈数据结构 stack.h
// //
//////////////////////////
#include<iostream.h>
template<class Type>class Stack; //定义堆栈的结点类
template<class Type>
class StackNode
{
friend class Stack<Type>;
public:
Type data;
StackNode<Type> *link;
StackNode(Type D=0,StackNode<Type> *L=NULL):link(L),data(D){}
};
template<class Type> //定义堆栈类
class Stack
{
public:
Stack():top(NULL),NumItem(0){} //构造函数
void Push(Type item); //入堆栈
Type Pop(); //出堆栈
Type GetTopValue(); //得到堆栈顶的元素的值
void MakeEmpty(); //清除堆栈
bool IsEmpty(); //看堆栈是否为空
int GetNum(); //得到堆中元素的个数
Type *GetTop(){return top;}; //得到堆栈顶的元素
private:
int NumItem; //记录堆栈元素的个数
StackNode<Type> *top; //堆栈的顶
};
template<class Type>
void Stack<Type>::Push(Type item)
{
top=new StackNode<Type>(item,top);
NumItem++;
}
template<class Type>
Type Stack<Type>::Pop()
{
StackNode<Type> *p;
Type temp;
temp=top->data;
p=top;
top=top->link;
delete p;
NumItem--;
return temp;
}
template<class Type>
Type Stack<Type>::GetTopValue()
{
return top->data;
}
template<class Type>
bool Stack<Type>::IsEmpty()
{
return top==NULL;
}
template<class Type>
void Stack<Type>::MakeEmpty()
{
delete top;
}
template<class Type>
int Stack<Type>::GetNum()
{
return NumItem;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -