⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genstack.h

📁 《C/C++程序设计导论(第二版)》一书的程序源文件
💻 H
字号:
// genstack.h Definition of the generic Stack class and generic
//   implementation functions.

#include <stdlib.h>
#include <iostream.h>

const char BOTTOM = 'b';
const char EXPREND = '#';
const char NUMBER = 'n';
const int MAX = 100;

template <class AnyType>
class Stack
{	public:	Stack ();					// constructor
				void Push (AnyType x);	// push x onto stack
				AnyType Pop ();			// pop and return top item
				AnyType Top ();			// return a copy of top item
	private:	AnyType cells[MAX];		// allow MAX entries
				int current_top;			// index of current top
				void Serror (char *);	// error message function
};

template <class AnyType>
Stack<AnyType>::Stack ()
{ current_top = 0;
}

template <class AnyType>
void Stack<AnyType>::Serror (char *errmes)
{	cerr << errmes << endl;
	exit(0);
}

template <class AnyType>
void Stack<AnyType>::Push (AnyType x)
{	if (current_top <= MAX)
		cells[current_top++] = x;
	else Serror("stack overflow");
}

template <class AnyType>
AnyType Stack<AnyType>::Pop ()
{	if (current_top == 0)
		Serror ("stack underflow");
	return (cells[--current_top]);
}

template <class AnyType>
AnyType Stack<AnyType>::Top ()
{	return (cells[current_top -1]);
}

⌨️ 快捷键说明

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