tempzhan1.h

来自「这是书上的代码」· C头文件 代码 · 共 73 行

H
73
字号
#ifndef STACK_CLASS
#define STACK_CLASS
#include <iostream.h>
#include <stdlib.h>
const int MaxStackSize=50;
template <class T>
class Stack
{
	private:
		 T stacklist[MaxStackSize];
		 int top;
	public:
		Stack(void);
		void push(const T& item);
		T pop(void);
		void clearstack(void);
		T peek(void)const;
		int stackempty(void)const;
		int stackfull(void)const;
};
template <class T>
Stack<T>::Stack(void)
{top=-1; }
template <class T>
void Stack<T>::push(const T& item)
{
	if(top==MaxStackSize-1)
	{ //cerr<<"stack over flow!"<<endl;
	 exit(1);
	}
	top++;
	stacklist[top]=item;

}
template <class T>
T Stack<T>::pop(void)
{
	T temp;
	if(top==-1)
	{ //cerr<<"attempt to pop an empty stack!"<< endl;
	 exit(1);
	}
	temp=stacklist[top];
	top--;
	return temp;
}

template <class T>
T Stack<T>::peek(void)const
{
if(top==-1)
{
	// cerr<<"attempt to peek at an empty stack!"<< endl;
	 exit(1);
}
return stacklist[top];
}

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

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

template <class T>
int Stack<T>::stackfull(void)const
{retrun top==MaxStackSize-1;}

#endif  //STACK_CLASS

⌨️ 快捷键说明

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