【栈,队列】两栈实现队列
2015-11-15 14:00
288 查看
一、用STL中的stack实现
MyQueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_h
#include<iostream>
#include<stack>
//MyQueue
template<typename T>
class MyQueue{
public:
typedef T value_type;
typedef T& reference;
MyQueue();//constructor
~MyQueue();//destructor
void push(const T value);//push
void pop();//pop
reference get_front();//front
reference get_back();//back
bool empty();//whether MyQueue is empty or not
int size();//get length
private:
std::stack<T> stack1;
std::stack<T> stack2;
};
//constructor
template<typename T>
MyQueue<T>::MyQueue(){}
//destructor
template<typename T>
MyQueue<T>::~MyQueue(){}
//push
template<typename T>
void MyQueue<T>::push(const T value){
stack1.push(value);
}
//pop
template<typename T>
void MyQueue<T>::pop(){
if (stack2.size() == 0){
while (stack1.size() > 0){
value_type data = stack1.top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
stack2.pop();
}
//front
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_front(){
if (stack2.size() <= 0){
while (stack1.size() > 0){
value_type data = stack1.top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack2.top();
}
//back
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_back(){
if (stack1.size() <= 0){
while (stack2.size() > 0){
value_type data = stack2.top();
stack1.push(data);
stack2.pop();
}
}
if (stack1.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack1.top();
}
//whether MyQueue is empty or not
template<typename T>
bool MyQueue<T>::empty(){
return size() == 0 ? true : false;
}
//get length
template<typename T>
int MyQueue<T>::size(){
return stack1.size() + stack2.size();
}
#endif main.cpp
LinkedStack.h
#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include<iostream>
//Stack node
template<typename T>
struct stack_node{
typedef stack_node<T>* pointer;
T data;
pointer next;
stack_node() :next(NULL){}
stack_node(const T value) :data(value), next(NULL){}
};
// LinkedStack
template<typename T>
class LinkedStack{
public:
typedef T value_type;
typedef T* pointer;
typedef value_type& reference;
typedef stack_node<T>* link_type;
LinkedStack();//constructor
~LinkedStack();//destructor
void push(const T value);//push
void pop();//pop
reference get_top();//top
bool empty();//whether LinkedStack is empty or not
int size();//get length
private:
void clear();//clear LinkedStack
private:
link_type top;
};
//constructor
template<typename T>
LinkedStack<T>::LinkedStack() :top(NULL){}
//destructor
template<typename T>
LinkedStack<T>::~LinkedStack(){
clear();
}
//push
template<typename T>
void LinkedStack<T>::push(const T value){
link_type p = new stack_node<T>(value);
p->next = top;
top = p;
}
//pop
template<typename T>
void LinkedStack<T>::pop(){
if (empty()){
std::cout << "there is no element in stack" << std::endl;
exit(1);
}
link_type p = top;
top = top->next;
delete p;
}
//top
template<typename T>
typename LinkedStack<T>::reference LinkedStack<T>::get_top(){
if (empty()){
std::cout << "there is no element in stack" << std::endl;
exit(1);
}
return top->data;
}
//whether stack is empty or not
template<typename T>
bool LinkedStack<T>::empty(){
return top == NULL ? true : false;
}
//get length
template<typename T>
int LinkedStack<T>::size(){
int count = 0;
link_type p = top;
while (p != NULL){
p = p->next;
count++;
}
return count;
}
//clear LinkedStack
template<typename T>
void LinkedStack<T>::clear(){
link_type p;
while (top != NULL){
p = top;
top = top->next;
delete p;
}
}
#endifMyQueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_h
#include"LinkedStack.h"
#include<iostream>
//MyQueue
template<typename T>
class MyQueue{
public:
typedef T value_type;
typedef T& reference;
MyQueue();//constructor
~MyQueue();//destructor
void push(const T value);//push
void pop();//pop
reference get_front();//front
reference get_back();//back
bool empty();//whether MyQueue is empty or not
int size();//get length
private:
LinkedStack<T> stack1;
LinkedStack<T> stack2;
};
//constructor
template<typename T>
MyQueue<T>::MyQueue(){}
//destructor
template<typename T>
MyQueue<T>::~MyQueue(){}
//push
template<typename T>
void MyQueue<T>::push(const T value){
stack1.push(value);
}
//pop
template<typename T>
void MyQueue<T>::pop(){
if (stack2.size() == 0){
while (stack1.size() > 0){
value_type data = stack1.get_top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
stack2.pop();
}
//front
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_front(){
if (stack2.size() == 0){
while (stack1.size() > 0){
value_type data = stack1.get_top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack2.get_top();
}
//back
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_back(){
if (stack1.size() == 0){
while (stack2.size() > 0){
value_type data = stack2.get_top();
stack1.push(data);
stack2.pop();
}
}
if (stack1.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack1.get_top();
}
//whether MyQueue is empty or not
template<typename T>
bool MyQueue<T>::empty(){
return size() == 0 ? true : false;
}
//get length
template<typename T>
int MyQueue<T>::size(){
return stack1.size() + stack2.size();
}
#endif
main.cpp
#include"MyQueue.h" using namespace std; int main(){ MyQueue<int> int_queue; cout << int_queue.size() << endl; //0 cout << boolalpha << int_queue.empty() << endl;//true for (int i = 1; i < 10; i++){ int_queue.push(i); } cout << int_queue.size() << endl; //9 cout << int_queue.get_front() << endl;//1 cout << int_queue.get_back() << endl;//9 cout << boolalpha << int_queue.empty() << endl;//false int_queue.pop(); int_queue.pop(); int_queue.pop(); cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//9 int_queue.push(10); int_queue.push(11); cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//11 int_queue.push(12); int_queue.push(13); int_queue.push(14); cout << int_queue.size() << endl; //11 cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//14 return 0; }
MyQueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_h
#include<iostream>
#include<stack>
//MyQueue
template<typename T>
class MyQueue{
public:
typedef T value_type;
typedef T& reference;
MyQueue();//constructor
~MyQueue();//destructor
void push(const T value);//push
void pop();//pop
reference get_front();//front
reference get_back();//back
bool empty();//whether MyQueue is empty or not
int size();//get length
private:
std::stack<T> stack1;
std::stack<T> stack2;
};
//constructor
template<typename T>
MyQueue<T>::MyQueue(){}
//destructor
template<typename T>
MyQueue<T>::~MyQueue(){}
//push
template<typename T>
void MyQueue<T>::push(const T value){
stack1.push(value);
}
//pop
template<typename T>
void MyQueue<T>::pop(){
if (stack2.size() == 0){
while (stack1.size() > 0){
value_type data = stack1.top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
stack2.pop();
}
//front
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_front(){
if (stack2.size() <= 0){
while (stack1.size() > 0){
value_type data = stack1.top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack2.top();
}
//back
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_back(){
if (stack1.size() <= 0){
while (stack2.size() > 0){
value_type data = stack2.top();
stack1.push(data);
stack2.pop();
}
}
if (stack1.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack1.top();
}
//whether MyQueue is empty or not
template<typename T>
bool MyQueue<T>::empty(){
return size() == 0 ? true : false;
}
//get length
template<typename T>
int MyQueue<T>::size(){
return stack1.size() + stack2.size();
}
#endif main.cpp
#include"MyQueue.h" using namespace std; int main(){ MyQueue<int> int_queue; cout << int_queue.size() << endl; //0 cout << boolalpha << int_queue.empty() << endl;//true for (int i = 1; i < 10; i++){ int_queue.push(i); } cout << int_queue.size() << endl; //9 cout << int_queue.get_front() << endl;//1 cout << int_queue.get_back() << endl;//9 cout << boolalpha << int_queue.empty() << endl;//false int_queue.pop(); int_queue.pop(); int_queue.pop(); cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//9 int_queue.push(10); int_queue.push(11); cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//11 int_queue.push(12); int_queue.push(13); int_queue.push(14); cout << int_queue.size() << endl; //11 cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//14 return 0; }二、用自己写的LinkedStack实现
LinkedStack.h
#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include<iostream>
//Stack node
template<typename T>
struct stack_node{
typedef stack_node<T>* pointer;
T data;
pointer next;
stack_node() :next(NULL){}
stack_node(const T value) :data(value), next(NULL){}
};
// LinkedStack
template<typename T>
class LinkedStack{
public:
typedef T value_type;
typedef T* pointer;
typedef value_type& reference;
typedef stack_node<T>* link_type;
LinkedStack();//constructor
~LinkedStack();//destructor
void push(const T value);//push
void pop();//pop
reference get_top();//top
bool empty();//whether LinkedStack is empty or not
int size();//get length
private:
void clear();//clear LinkedStack
private:
link_type top;
};
//constructor
template<typename T>
LinkedStack<T>::LinkedStack() :top(NULL){}
//destructor
template<typename T>
LinkedStack<T>::~LinkedStack(){
clear();
}
//push
template<typename T>
void LinkedStack<T>::push(const T value){
link_type p = new stack_node<T>(value);
p->next = top;
top = p;
}
//pop
template<typename T>
void LinkedStack<T>::pop(){
if (empty()){
std::cout << "there is no element in stack" << std::endl;
exit(1);
}
link_type p = top;
top = top->next;
delete p;
}
//top
template<typename T>
typename LinkedStack<T>::reference LinkedStack<T>::get_top(){
if (empty()){
std::cout << "there is no element in stack" << std::endl;
exit(1);
}
return top->data;
}
//whether stack is empty or not
template<typename T>
bool LinkedStack<T>::empty(){
return top == NULL ? true : false;
}
//get length
template<typename T>
int LinkedStack<T>::size(){
int count = 0;
link_type p = top;
while (p != NULL){
p = p->next;
count++;
}
return count;
}
//clear LinkedStack
template<typename T>
void LinkedStack<T>::clear(){
link_type p;
while (top != NULL){
p = top;
top = top->next;
delete p;
}
}
#endifMyQueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_h
#include"LinkedStack.h"
#include<iostream>
//MyQueue
template<typename T>
class MyQueue{
public:
typedef T value_type;
typedef T& reference;
MyQueue();//constructor
~MyQueue();//destructor
void push(const T value);//push
void pop();//pop
reference get_front();//front
reference get_back();//back
bool empty();//whether MyQueue is empty or not
int size();//get length
private:
LinkedStack<T> stack1;
LinkedStack<T> stack2;
};
//constructor
template<typename T>
MyQueue<T>::MyQueue(){}
//destructor
template<typename T>
MyQueue<T>::~MyQueue(){}
//push
template<typename T>
void MyQueue<T>::push(const T value){
stack1.push(value);
}
//pop
template<typename T>
void MyQueue<T>::pop(){
if (stack2.size() == 0){
while (stack1.size() > 0){
value_type data = stack1.get_top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
stack2.pop();
}
//front
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_front(){
if (stack2.size() == 0){
while (stack1.size() > 0){
value_type data = stack1.get_top();
stack2.push(data);
stack1.pop();
}
}
if (stack2.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack2.get_top();
}
//back
template<typename T>
typename MyQueue<T>::reference MyQueue<T>::get_back(){
if (stack1.size() == 0){
while (stack2.size() > 0){
value_type data = stack2.get_top();
stack1.push(data);
stack2.pop();
}
}
if (stack1.size() == 0){
std::cout << "there is no element in queue" << std::endl;
exit(1);
}
return stack1.get_top();
}
//whether MyQueue is empty or not
template<typename T>
bool MyQueue<T>::empty(){
return size() == 0 ? true : false;
}
//get length
template<typename T>
int MyQueue<T>::size(){
return stack1.size() + stack2.size();
}
#endif
main.cpp
#include"MyQueue.h" using namespace std; int main(){ MyQueue<int> int_queue; cout << int_queue.size() << endl; //0 cout << boolalpha << int_queue.empty() << endl;//true for (int i = 1; i < 10; i++){ int_queue.push(i); } cout << int_queue.size() << endl; //9 cout << int_queue.get_front() << endl;//1 cout << int_queue.get_back() << endl;//9 cout << boolalpha << int_queue.empty() << endl;//false int_queue.pop(); int_queue.pop(); int_queue.pop(); cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//9 int_queue.push(10); int_queue.push(11); cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//11 int_queue.push(12); int_queue.push(13); int_queue.push(14); cout << int_queue.size() << endl; //11 cout << int_queue.get_front() << endl;//4 cout << int_queue.get_back() << endl;//14 return 0; }
相关文章推荐
- VVDocumenter Xcode7 安装
- 【MySQL学习】Unknown column 'PASSWORD'|Access denied for user 'root'@'localhost'
- 操作系统9-进程和系统调度(II)
- 【最佳实践系列】基于序列化技术的XML数据源操作模式
- Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解
- 基于git hooks的前端代码质量控制解决方案
- 60. MySQLi 扩展拾遗
- postman、postman-rest client 安装
- 【NOIP2015】跳石头 (二分+贪心判定)
- React Flux架构简介
- float
- 【NOIP2015】跳石头 (二分+贪心判定)
- 函数调用和赋值运算
- 一个超级简单MiniBrowser
- jsp Request获取url信息的各种方法比较
- 看懂UML类图和时序图
- C++11新标准:decltype关键字
- JavaScript学习手记2
- 字符串的全排列
- ES规范解读之自增操作符