stack.cpp

来自「自己做课程设计的时候做的小型编译器」· C++ 代码 · 共 55 行

CPP
55
字号
#include "stdafx.h"
using namespace std;


template <class T>
Stack<T>::Stack (void):top(-1),StackList(InitStackSize) {}

template <class T>
void Stack<T>::Push (const T& item)
{
	if (top==StackList.ListSize()-1)
		StackList.ReSize(StackList.ListSize()*2);
	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 an empty Stack!"<<endl;
		exit(1);
	}
	return StackList[top];
}


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


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

⌨️ 快捷键说明

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