📄 stack.h
字号:
//****************************************************************
//This software may not be distributed further without permission from
// Sun Xueshi's Group
//
//This software is distributed WITHOUT ANY WARRANTY. No claims are made
// as to its functionality or purpose.
//
//Authors: 孙学诗,孙海洋
// Date: 12/4/03
//
//****************************************************************
#include<iostream>
#include<cassert>
using namespace std;
template <class Type>class Stack;
template <class Type>class StackNode{
friend class Stack<Type>;
public:
StackNode(Type d = 0,StackNode<Type> *l = NULL)
: data(d), link(l) {}
private:
Type data;
StackNode<Type> *link;
};
template <class Type>class Stack {
public:
Stack() : top(NULL){}
~Stack();
int Push(const Type &item);
Type Pop();
Type GetTop();
void MakeEmpty() {top = NULL;}
int IsEmpty() const { return top == NULL; } //if stack is empty, return ture
private:
StackNode<Type> *top;
};
//Destroy stack
template<class Type>
Stack<Type>::~Stack(){
StackNode<Type> *p;
while( top != NULL ){
p = top;
top = top->link;
delete p;
}
}
//Push a value into stack
template<class Type>
int Stack<Type>::Push(const Type &item){
top = new StackNode<Type>(item,top);
return 1;
}
//get a value from stack and delete it from stack
template<class Type>
Type Stack<Type>::Pop(){
Type value;
if( !IsEmpty() ) {
StackNode<Type> *p = top;
value = p ->data;
top = top->link;
delete p;
}
else value = 0; //if stack is empty return 0
return value;
}
//Get a value from top of stack
template<class Type>
Type Stack<Type>::GetTop(){
Type Value;
if( !IsEmpty() )
Value = top->data;
else Value = 0; //if stack is empty return 0
return Value;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -