- 原文地址:http://blog.youkuaiyun.com/moxiaomomo/article/details/6393258 感谢作者分享
- #ifndef STACK_H
- #define STACK_H
- #include<stdio.h>
- #include<stdlib.h>
- #include <iostream>
- using namespace std;
- template<class T>
- class Stack
- {
- public:
- int top;
- int maxtop;
- T *data;
- Stack(int size); //构造函数
- ~Stack();
- bool isEmpty(); //判断为空
- bool isFull(); //判断为满
- void push(T ch/*,Stack s*/); //压栈
- T pop(); //出栈
- };
- template <class T>
- Stack<T>::Stack(int size)
- {
- maxtop=size;
- top=-1;
- data=new T[size];
- }
- template <class T>
- Stack<T>::~Stack()
- {
- delete []data;
- }
- template <class T>
- bool Stack<T>::isEmpty()
- {
- return top<0;
- }
- template <class T>
- bool Stack<T>::isFull()
- {
- return top==maxtop;
- }
- template <class T>
- void Stack<T>::push(T ch/*,Stack s*/)
- {
- if (isFull())
- {
- cout<<"The stack is full~"<<endl;
- return;
- }
- data[++top]=ch;
- }
- template <class T>
- T Stack<T>::pop()
- {
- if (isEmpty())
- {
- cout<<"The stack is empty~"<<endl;
- return -1;
- }
- T s=data[top];
- top--;
- return s;
- }
- #endif
栈操作的C++实现
最新推荐文章于 2024-09-30 18:37:43 发布