C++两个栈实现一个队列
来源:互联网 发布:nginx配置域名绑定ip 编辑:程序博客网 时间:2024/05/22 10:23
#include <iostream>
using namespace std;
#include <stack>
class Queue{
public:
stack<int> myStack1;
stack<int> myStack2;
void push(int t){
myStack1.push(t);
}
int pop(){
if (empty())
{
return NULL;
}
else if (myStack2.empty())
{
int size=myStack1.size();
for (int i=0;i<size;i++)
{
myStack2.push(myStack1.top());
myStack1.pop();
}
}
int temp=myStack2.top();
myStack2.pop();
return temp;
}
bool empty(){
bool b=false;
if (myStack2.empty()&&myStack1.empty())
{
b=true;
}
return b;
}
int size(){
int temp=myStack1.size();
temp+=myStack2.size();
return temp;
}
//取队首元素
int front(){
int result=NULL;
if (empty())
{
return result;
}
else if (myStack2.empty())
{
int size=myStack1.size();
for (int i=0;i<size;i++)
{
myStack2.push(myStack1.top());
myStack1.pop();
}
}
result=myStack2.top();
return result;
}
//取队尾元素
int back(){
int result=NULL;
if (empty())
{
return result;
}
else if (!myStack1.empty())
{
result=myStack1.top();
}else{
int size=myStack2.size();
for (int i=0;i<size;i++)
{
myStack1.push(myStack2.top());
myStack2.pop();
}
result=myStack1.top();
}
return result;
}
};
void main()
{
Queue *queue=new Queue();
for (int i=0;i<10;i++)
{
queue->push(i);
}
for (int i=0;i<5;i++)
{
cout<<queue->pop()<<endl;
}
cout<<"front:"<<queue->front()<<endl;
cout<<"back:"<<queue->back()<<endl;
for (int i=10;i<20;i++)
{
queue->push(i);
}
cout<<"pop5-19"<<endl;
cout<<"queuesize"<<queue->size()<<endl;
for (int i=0;true;i++)
{
if (queue->empty())
{
break;
}
cout<<queue->pop()<<endl;
}
}
using namespace std;
#include <stack>
class Queue{
public:
stack<int> myStack1;
stack<int> myStack2;
void push(int t){
myStack1.push(t);
}
int pop(){
if (empty())
{
return NULL;
}
else if (myStack2.empty())
{
int size=myStack1.size();
for (int i=0;i<size;i++)
{
myStack2.push(myStack1.top());
myStack1.pop();
}
}
int temp=myStack2.top();
myStack2.pop();
return temp;
}
bool empty(){
bool b=false;
if (myStack2.empty()&&myStack1.empty())
{
b=true;
}
return b;
}
int size(){
int temp=myStack1.size();
temp+=myStack2.size();
return temp;
}
//取队首元素
int front(){
int result=NULL;
if (empty())
{
return result;
}
else if (myStack2.empty())
{
int size=myStack1.size();
for (int i=0;i<size;i++)
{
myStack2.push(myStack1.top());
myStack1.pop();
}
}
result=myStack2.top();
return result;
}
//取队尾元素
int back(){
int result=NULL;
if (empty())
{
return result;
}
else if (!myStack1.empty())
{
result=myStack1.top();
}else{
int size=myStack2.size();
for (int i=0;i<size;i++)
{
myStack1.push(myStack2.top());
myStack2.pop();
}
result=myStack1.top();
}
return result;
}
};
void main()
{
Queue *queue=new Queue();
for (int i=0;i<10;i++)
{
queue->push(i);
}
for (int i=0;i<5;i++)
{
cout<<queue->pop()<<endl;
}
cout<<"front:"<<queue->front()<<endl;
cout<<"back:"<<queue->back()<<endl;
for (int i=10;i<20;i++)
{
queue->push(i);
}
cout<<"pop5-19"<<endl;
cout<<"queuesize"<<queue->size()<<endl;
for (int i=0;true;i++)
{
if (queue->empty())
{
break;
}
cout<<queue->pop()<<endl;
}
}
0 0
- 两个栈实现一个队列(C/C++)
- 【C++】两个队列实现一个栈
- 【C++】两个栈实现一个队列
- 两个队列实现一个栈&两个栈实现一个队列
- 两个栈实现一个队列-----两个队列实现一个栈
- 两个栈实现一个队列&&两个队列实现一个栈
- 两个栈实现一个队列 && 两个队列实现一个栈
- 两个栈实现一个队列,两个队列实现一个栈
- 两个栈实现一个队列/两个队列实现一个栈
- 两个栈实现一个队列&两个队列实现一个栈
- 两个队列实现一个栈&两个栈实现一个队列
- 两个队列实现一个栈 / 两个栈实现一个队列
- 两个栈实现一个队列,两个队列实现一个栈
- 两个栈实现一个队列&两个队列实现一个栈
- 两个栈实现一个队列&&两个队列实现一个栈
- 两个栈实现一个队列&两个队列实现一个栈
- 两个栈实现一个队列/两个队列实现一个栈
- 剑指offer07题两个栈实现一个队列(c语言)
- Linux上Oracle 11g安装步骤图解
- 关于ARM地址映射的理解
- 在 linux 下使用 CMake 构建应用程序
- vmware与ubuntu共享文件夹
- Thinkpphp3.3.2 IP管理查询近7天到期IP
- C++两个栈实现一个队列
- 《我不是潘金莲》--中国式的荒唐
- Dijkstra's 算法
- 【Web前端】$(document).ready(function(){}) 和 $(window).on("load",function(){}) 的异同
- 文章标题
- 贪心+dp--hdu5501(经典题)
- arduino简易流水灯(新手之路)
- Oracle的UNDO表空间管理总结
- 电信天邑TEWA-300EA光猫破解, 解绑iTV口, 使用iTV口上网