genstack.h
来自「《C/C++程序设计导论(第二版)》一书的程序源文件」· C头文件 代码 · 共 53 行
H
53 行
// 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 + =
减小字号Ctrl + -
显示快捷键?