stack.h

来自「某大学的unix模拟实验试题要求」· C头文件 代码 · 共 84 行

H
84
字号
#include<iostream>
#include<stdlib.h>
const int MaxStackSize=150;
template<class T>
class Stack
{
private:
	T Stacklist[MaxStackSize];
	int top;
public:
	Stack(void);
	void push(T item);
	T pop(void);
	void ClearStack(void);
	T Peek(void);
	int StackEmpty(void);
	int StackFull(void);
	int recount();
};
template<class T>
Stack<T> ::Stack(void):top(-1){}

template<class T>
void Stack<T> ::push(T item)
{
	if(top==MaxStackSize-1)
	{
		cout<<"Stack overflow!"<<endl;
		exit(1);
	}
	top++;
	Stacklist[top]=item;
	//return Stacklist[top];
}

template<class T>
T Stack<T> ::pop(void)
{
	T temp;
	if(top==-1)
	{
		cout<<"Attempt to pop an empty Stack!"<<endl;
		exit(1);
	}
	temp=Stacklist[top];
	top--;
	return temp;
}

template<class T>
void Stack<T> ::ClearStack(void)
{
	top=-1;
}

template<class T>
T Stack<T> ::Peek(void)
{
	if(top==-1)
	{
		cout<<"Attempt topeek an empty Stack"<<endl;
		exit(1);
	}
	return Stacklist[top];
}

template<class T>
int Stack<T> ::StackEmpty(void)
{
	return top==-1;
}

template<class T>
int Stack<T> ::StackFull(void)
{
	return top==MaxStackSize-1;
}
template<class T>
int Stack<T> ::recount(void)
{
	int temp;temp=top+1;
	return temp;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?