stack.h
来自「自己在以前学数据结构时用C++模板写的一些常用数据,都测试过」· C头文件 代码 · 共 77 行
H
77 行
///////////////////////////
// //
// 堆栈数据结构 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 + =
减小字号Ctrl + -
显示快捷键?