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

原创 2015年11月21日 15:17:25

一、用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();
}

#endif
main.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;
	}
}

#endif 
MyStack.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();
}

#endif
main.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;
}

相关文章推荐

栈和队列的实现

  • 2013年12月17日 14:37
  • 269KB
  • 下载

栈和队列的实现

  • 2015年09月16日 20:14
  • 6KB
  • 下载

用两个栈实现一个队列功能

两个栈 设为StackIn和StackOut 1.StackIn实现队列入队功能 压栈进入A push(A) 2.StackOut实现队列出队功能 如果B栈为空,A弹栈,压入B中,B弹栈  ...

《剑指offer》面试题七 用两个栈实现队列

原题地址 http://ac.jobdu.com/problem.php?pid=1512 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int...

链栈和链队列的实现

  • 2011年11月19日 12:12
  • 3KB
  • 下载

C++算法之 两个队列实现一个栈

题目:用两个队列实现一个栈 算法思路: 现有两个队列q1与q2,入栈:如果q1与q2都为空,那么我们选择q1入栈也就是入队列,比如q1入栈 1 2 3 4 ;现在要出栈,后进先出那么4要出栈。但是...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【栈,队列】两队列实现栈
举报原因:
原因补充:

(最多只允许输入30个字)