您的位置:首页 > 其它

【栈,队列】两栈实现队列

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
#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; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: