一、用STL的queue实现
MyStack.h
#ifndef MYSTACK_H #define MYSTACK_H #include<iostream> #include<queue> //MyStack template<typename T> class MyStack{ public: typedef T value_type; typedef T& reference; MyStack();//constructor ~MyStack();//destructor void push(const T value);//push void pop();//pop reference get_top();//top bool empty();//whether MyStack is empty or not int size();//get length private: std::queue<T> que1; std::queue<T> que2; }; //constructor template<typename T> MyStack<T>::MyStack(){} //destructor template<typename T> MyStack<T>::~MyStack(){} //push template<typename T> void MyStack<T>::push(const T value){ if (empty()) que1.push(value); else if (que1.empty()) que2.push(value); else if (que2.empty()) que1.push(value); } //pop template<typename T> void MyStack<T>::pop(){ if (empty()){ std::cout << "there is no element in stack" << std::endl; exit(1); }else if (que1.empty()){ while (que2.size() > 1){ value_type data = que2.front(); que1.push(data); que2.pop(); } que2.pop(); }else if (que2.empty()){ while (que1.size()>1){ value_type data = que1.front(); que2.push(data); que1.pop(); } que1.pop(); } } //top template<typename T> typename MyStack<T>::reference MyStack<T>::get_top(){ if (empty()){ std::cout << "there is no element in stack" << std::endl; exit(1); } else if (que1.empty()){ return que2.back(); } else if (que2.empty()){ return que1.back(); } } //whether MyStack is empty or not template<typename T> bool MyStack<T>::empty(){ return size() == 0 ? true : false; } //get length template<typename T> int MyStack<T>::size(){ return que1.size() + que2.size(); } #endifmain.cpp
#include"MyStack.h" using namespace std; int main(){ MyStack<int> int_stack; cout << int_stack.size() << endl; //0 cout << boolalpha << int_stack.empty() << endl;//true int_stack.push(1); cout << int_stack.size() << endl; //1 cout << int_stack.get_top() << endl;//1 int_stack.pop(); for (int i = 0; i < 20; i++){ int_stack.push(i); } cout << int_stack.size() << endl; //20 cout << int_stack.get_top() << endl;//19 cout << boolalpha << int_stack.empty() << endl;//false int_stack.push(21); cout << int_stack.get_top() << endl;//21 int_stack.pop(); cout << int_stack.get_top() << endl;//19 int_stack.pop(); int_stack.push(22); cout << int_stack.size() << endl; //20 cout << int_stack.get_top() << endl;//22 int_stack.pop(); cout << int_stack.get_top() << endl;//18 return 0; }二、用自己写的LinkedQueue实现
LinkedQueue.h
#ifndef LINKEDQUEUE_H #define LINKEDQUEUE_h #include<iostream> //queue node template<typename T> struct queue_node{ typedef queue_node<T>* pointer; T data; pointer next; queue_node() :next(NULL){} queue_node(const T value) :data(value), next(NULL){} }; //LinkedQueue template<typename T> class LinkedQueue{ public: typedef T value_type; typedef T* pointer; typedef T& reference; typedef queue_node<T>* linked_type; LinkedQueue();//constructor ~LinkedQueue();//destructor void push(const T value);//push void pop();//pop reference get_front();//front reference get_back();//back bool empty();//whether LinkedQueue is empty or not int size();//get length private: void clear();//clear LinkedQueue private: linked_type front; linked_type rear; }; //constructor template<typename T> LinkedQueue<T>::LinkedQueue() :front(new queue_node<T>()), rear(front){} //destructor template<typename T> LinkedQueue<T>::~LinkedQueue(){ clear(); delete front; front = NULL; rear = NULL; } //push template<typename T> void LinkedQueue<T>::push(const T value){ linked_type p = new queue_node<T>(value); rear->next = p; rear = p; } //pop template<typename T> void LinkedQueue<T>::pop(){ if (empty()){ std::cout << "there is no element in queue" << std::endl; exit(1); } linked_type p = front->next; front->next = p->next; if (rear == p) rear = front; delete p; } //front template<typename T> typename LinkedQueue<T>::reference LinkedQueue<T>::get_front(){ if (empty()){ std::cout << "there is no element in queue" << std::endl; exit(1); } return front->next->data; } //back template<typename T> typename LinkedQueue<T>::reference LinkedQueue<T>::get_back(){ if (empty()){ std::cout << "there is no element in queue" << std::endl; exit(1); } return rear->data; } //whether LinkedQueue is empty or not template<typename T> bool LinkedQueue<T>::empty(){ return front == rear ? true : false; } //get length template<typename T> int LinkedQueue<T>::size(){ int count = 0; linked_type p = front->next; while (p != NULL){ p = p->next; count++; } return count; } //clear LinkedQueue template<typename T> void LinkedQueue<T>::clear(){ rear = front; linked_type p = front->next; linked_type q; front->next = NULL; while (p != NULL){ q = p; p = p->next; delete q; } } #endifMyStack.h
#ifndef MYSTACK_H #define MYSTACK_H #include"LinkedQueue.h" #include<iostream> //MyStack template<typename T> class MyStack{ public: typedef T value_type; typedef T& reference; MyStack();//constructor ~MyStack();//destructor void push(const T value);//push void pop();//pop reference get_top();//top bool empty();//whether MyStack is empty or not int size();//get length private: LinkedQueue<T> que1; LinkedQueue<T> que2; }; //constructor template<typename T> MyStack<T>::MyStack(){} //destructor template<typename T> MyStack<T>::~MyStack(){} //push template<typename T> void MyStack<T>::push(const T value){ if (empty()) que1.push(value); else if (que1.empty()) que2.push(value); else if (que2.empty()) que1.push(value); } //pop template<typename T> void MyStack<T>::pop(){ if (empty()){ std::cout << "there is no element in stack" << std::endl; exit(1); } else if (que1.empty()){ while (que2.size() > 1){ value_type data = que2.get_front(); que1.push(data); que2.pop(); } que2.pop(); } else if (que2.empty()){ while (que1.size()>1){ value_type data = que1.get_front(); que2.push(data); que1.pop(); } que1.pop(); } } //top template<typename T> typename MyStack<T>::reference MyStack<T>::get_top(){ if (empty()){ std::cout << "there is no element in stack" << std::endl; exit(1); } else if (que1.empty()){ return que2.get_back(); } else if (que2.empty()){ return que1.get_back(); } } //whether MyStack is empty or not template<typename T> bool MyStack<T>::empty(){ return size() == 0 ? true : false; } //get length template<typename T> int MyStack<T>::size(){ return que1.size() + que2.size(); } #endifmain.cpp
#include"MyStack.h" using namespace std; int main(){ MyStack<int> int_stack; cout << int_stack.size() << endl; //0 cout << boolalpha << int_stack.empty() << endl;//true int_stack.push(1); cout << int_stack.size() << endl; //1 cout << int_stack.get_top() << endl;//1 int_stack.pop(); for (int i = 0; i < 20; i++){ int_stack.push(i); } cout << int_stack.size() << endl; //20 cout << int_stack.get_top() << endl;//19 cout << boolalpha << int_stack.empty() << endl;//false int_stack.push(21); cout << int_stack.get_top() << endl;//21 int_stack.pop(); cout << int_stack.get_top() << endl;//19 int_stack.pop(); int_stack.push(22); cout << int_stack.size() << endl; //20 cout << int_stack.get_top() << endl;//22 int_stack.pop(); cout << int_stack.get_top() << endl;//18 return 0; }