
- #include <iostream>
-
- using namespace std;
-
- #define MAX 5
-
- template <typename T>
- class Stack{
- public:
- //构造函数
- Stack();
- //析构函数
- ~Stack();
- //拷贝构造函数
- Stack(const Stack &other);
- //入栈
- int push(T e);
- //出栈
- int pop();
- //清空栈
- void clear();
- //判空
- bool empty();
- //判满
- bool full();
- //获取栈顶元素
- T topdata();
- //求栈的大小
- int size();
- private:
- T *data;
- int top;
- };
-
- template <typename T>
- //构造函数
- Stack<T>::Stack():data(new T[MAX]),top(-1){
- cout<<"构造函数"<<endl;
- }
- template <typename T>
- //析构函数
- Stack<T>::~Stack(){
- delete []data; //释放指针空间
- cout<<"析构函数"<<endl;
- }
- template <typename T>
- //拷贝构造函数
- Stack<T>::Stack(const Stack &other):data(new T[MAX]),top(other.top){
- std::copy(other.data,other.data+MAX,data);
- cout<<"拷贝函数"<<endl;
- }
- //入栈
- template <typename T>
- int Stack<T>::push(T e){
- if(Stack<T>::full()){
- cout<<"栈满,无法入栈;"<<endl;
- return 0;
- }
- data[++top]=e;
- cout<<data[top]<<"已入栈"<<endl;
- return 1;
- }
- //出栈
- template <typename T>
- int Stack<T>::pop(){
- if(Stack<T>::empty()){
- cout<<"栈空,无法出栈;"<<endl;
- return 0;
- }
- cout<<data[top--]<<"已出栈"<<endl;
- return 1;
- }
- //清空栈
- template <typename T>
- void Stack<T>::clear(){
- cout<<"****清空栈****"<<endl;
- while(!Stack::empty()){
- Stack::pop();
- }
- }
- //判空
- template <typename T>
- bool Stack<T>::empty(){
- return -1 == top;
- }
- //判满
- template <typename T>
- bool Stack<T>::full(){
- return MAX-1 == top;
- }
- //获取栈顶元素
- template <typename T>
- T Stack<T>::topdata(){
- cout<<"栈顶元素为:";
- return data[top];
- }
- //求栈的大小
- template <typename T>
- int Stack<T>::size(){
- cout<<this<<"栈的大小为:";
- return top+1;
- }
-
- int main()
- {
- Stack<string> s;
- s.push("ww");
- s.push("qq");
- s.push("ee");
- s.push("rr");
- s.push("tt");
- s.push("yy");
- cout<<s.size()<<endl;
- Stack<string> s2=s;
- cout<<s2.size()<<endl;
- Stack<string> s3=s2;
- cout<<s3.size()<<endl;
- s3.pop();
- s3.pop();
- s3.pop();
- cout<<s3.topdata()<<endl;
- s3.clear();
- s3.pop();
- cout<<s3.size()<<endl;
- cout<<s.size()<<endl;
- return 0;
- }
- #include <iostream>
- #define MAX 5
-
- using namespace std;
-
- template <typename T>
- class Queue{
- public:
- //构造函数
- Queue();
- //析构函数
- ~Queue();
- //拷贝构造函数
- Queue(const Queue &other);
- //入队
- int push(T e);
- //出队
- int pop();
- //清空队列
- void clear();
- //判空
- bool empty();
- //判满
- bool full();
- //求队列大小
- int size();
-
- private:
- int front;
- int tail;
- T *data;
- };
-
- //构造函数
- template <typename T>
- Queue<T>::Queue():front(0),tail(0),data(new T[MAX])
- {
- cout<<"构造函数"<<endl;
- }
- //析构函数
- template <typename T>
- Queue<T>::~Queue(){
- delete []data;
- cout<<"析构函数"<<endl;
- }
- //拷贝构造函数
- template <typename T>
- Queue<T>::Queue(const Queue &other):front(other.front),
- tail(other.tail),
- data(new T[MAX]){
- std::copy(other.data,other.data+MAX,data);
- cout<<"拷贝构造函数"<<endl;
- }
- //入队
- template <typename T>
- int Queue<T>::push(T e){
- if(Queue<T>::full()){
- cout<<"队满,无法入队;"<<endl;
- return 0;
- }
- data[tail]=e;
- cout<<e<<"已入队"<<endl;
- tail=(tail+1)%MAX;
-
- return 1;
- }
- //出队
- template <typename T>
- int Queue<T>::pop(){
- if(Queue<T>::empty()){
- cout<<"队空,无法出队;"<<endl;
- return 0;
- }
- cout<<data[front]<<"已出队"<<endl;
- front=(front+1)%MAX;
- return 1;
- }
- //清空队列
- template <typename T>
- void Queue<T>::clear(){
- cout<<"****清空队列****"<<endl;
- while(!Queue<T>::empty()){
- Queue<T>::pop();
- }
- }
- //判空
- template <typename T>
- bool Queue<T>::empty(){
- return front==tail;
- }
- //判满
- template <typename T>
- bool Queue<T>::full(){
- return (tail+1)%MAX==front;
- }
- //求队列大小
- template <typename T>
- int Queue<T>::size(){
- cout<<this<<"队列大小为:";
- return (tail-front+MAX)%MAX;
- }
-
-
- int main()
- {
- Queue<int> q;
- q.push(1);
- q.push(2);
- q.push(3);
- q.push(4);
- q.push(5);
- cout<<q.size()<<endl;
- Queue<int> q2=q;
- cout<<q2.size()<<endl;
- Queue<int> q3=q2;
- cout<<q2.size()<<endl;
- q2.clear();
- q2.pop();
- cout<<q2.size()<<endl;
- cout<<q.size()<<endl;
-
- return 0;
- }