- //Stack
- template<class datatype>
- class stack{
- public:
- static const int MAXSIZE = 100;
- stack() : top(0) {
- }
- bool empty() const {
- return top == 0;
- }
- void push(const datatype & x) {
- if(top == MAXSIZE)
- throw "StackOverflow";
- data[top++] = x;
- }
- void pop() {
- if(empty())
- throw "StackEmpty";
- top--;
- }
- void get_top() const{
- if(empty())
- throw "StackEmpty";
- return data[top-1];
- }
- private:
- datatype data[MAXSIZE];
- int top;
- };
- ///
- //****************************************************************
- /
- //queue
- template<class datatype>
- class queue {
- public:
- static const int MAXSIZE = 100;
- queue() {
- front = rear = 0;
- }
- bool is_empty() const{
- return front == rear;
- }
- bool is_full() const {
- return rear == MAXSIZE;
- }
- void insert(datatype x) {
- if(is_full())
- throw "Queue Overflow";
- data[rear++] = x;
- }
- void delete() {
- if(is_empty())
- throw "Queue Empty";
- front++;
- }
- datatype get_front() const{
- if(is_empty())
- throw "Queue Empty";
- return data[front];
- }
- private:
- datatype data[MAXSIZE];
- int front;
- int rear;
- };
- //^_^
simple version of stack and queue 's ADT
最新推荐文章于 2022-09-05 15:27:44 发布