两个队列实现一个栈——栈和队列面试题(3)
2016-09-18 19:13
211 查看
题目要求:用两个队列实现一个栈,实现栈的内部函数
“test.cpp”
<strong><span style="font-size:18px;">#include<iostream>
using namespace std;
#include<queue>
template<class T>
class TwoQueueFromStack
{
public:
TwoQueueFromStack(){}
void Push(const T& data)
{
if(FirstQueue.empty())
{
ScondQueue.push(data);
}
else
{
FirstQueue.push(data);
}
}
void Pop()
{
if(FirstQueue.empty())
{
while(ScondQueue.size() != 1)
{
FirstQueue.push(ScondQueue.front());
ScondQueue.pop();
}
ScondQueue.pop();
}
else
{
while(FirstQueue.size() != 1)
{
ScondQueue.push(FirstQueue.front());
FirstQueue.pop();
}
FirstQueue.pop();
}
}
T& Top()
{
if(FirstQueue.empty())
{
return ScondQueue.back();
}
else
{
return FirstQueue.back();
}
}
size_t Size()
{
if(FirstQueue.empty())
{
return ScondQueue.size();
}
else
{
return FirstQueue.size();
}
}
bool Empty()
{
return FirstQueue.empty() && ScondQueue.empty();
}
private:
queue<T> FirstQueue;
queue<T> ScondQueue;
};
void test()
{
TwoQueueFromStack<int> s;
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
s.Push(6);
cout<<"top = "<<s.Top()<<endl;
cout<<"size = "<<s.Size()<<endl;
cout<<"empty = "<<s.Empty()<<endl;
s.Pop();
s.Pop();
s.Pop();
cout<<"top = "<<s.Top()<<endl;
cout<<"size = "<<s.Size()<<endl;
cout<<"empty = "<<s.Empty()<<endl;
s.Pop();
s.Pop();
s.Pop();
cout<<"empty = "<<s.Empty()<<endl;
}
int main()
{
test();
return 0;
}</span></strong>
“test.cpp”
<strong><span style="font-size:18px;">#include<iostream>
using namespace std;
#include<queue>
template<class T>
class TwoQueueFromStack
{
public:
TwoQueueFromStack(){}
void Push(const T& data)
{
if(FirstQueue.empty())
{
ScondQueue.push(data);
}
else
{
FirstQueue.push(data);
}
}
void Pop()
{
if(FirstQueue.empty())
{
while(ScondQueue.size() != 1)
{
FirstQueue.push(ScondQueue.front());
ScondQueue.pop();
}
ScondQueue.pop();
}
else
{
while(FirstQueue.size() != 1)
{
ScondQueue.push(FirstQueue.front());
FirstQueue.pop();
}
FirstQueue.pop();
}
}
T& Top()
{
if(FirstQueue.empty())
{
return ScondQueue.back();
}
else
{
return FirstQueue.back();
}
}
size_t Size()
{
if(FirstQueue.empty())
{
return ScondQueue.size();
}
else
{
return FirstQueue.size();
}
}
bool Empty()
{
return FirstQueue.empty() && ScondQueue.empty();
}
private:
queue<T> FirstQueue;
queue<T> ScondQueue;
};
void test()
{
TwoQueueFromStack<int> s;
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
s.Push(6);
cout<<"top = "<<s.Top()<<endl;
cout<<"size = "<<s.Size()<<endl;
cout<<"empty = "<<s.Empty()<<endl;
s.Pop();
s.Pop();
s.Pop();
cout<<"top = "<<s.Top()<<endl;
cout<<"size = "<<s.Size()<<endl;
cout<<"empty = "<<s.Empty()<<endl;
s.Pop();
s.Pop();
s.Pop();
cout<<"empty = "<<s.Empty()<<endl;
}
int main()
{
test();
return 0;
}</span></strong>
相关文章推荐
- 经典面试题一:用两个栈实现一个队列
- 面试题之两个栈实现一个队列,乘机数组问题
- 58面试题-----用两个栈实现一个队列
- 栈&队列面试题之之两个栈实现一个队列
- 数据结构面试题:两个队列实现一个堆栈
- 面试题之用两个栈实现一个队列
- 两个栈实现一个队列——栈和队列面试题(2)
- 【数据结构】(面试题)使用两个栈实现一个队列(详细介绍)
- 【面试题】剑指Offer-7-两个栈实现一个队列
- 剑指offer编程题Java实现——面试题7相关题用两个队列实现一个栈
- 【数据结构】栈面试题--两个队列实现一个栈
- 面试题7:用两个栈实现一个队列
- 栈&队列面试题之两个队列实现一个栈
- 面试题(两个栈实现一个队列和两个队列实现一个栈)
- 一个数组实现两个栈——栈和队列面试题(5)
- 数据结构面试题(1)--用两个队列实现一栈和用两个栈实现一个队列
- 面试题-----两个栈实现一个队列
- 面试题之两个队列实现一个栈
- 剑指offer《面试题7:用两个栈实现队列》及用两个队列实现一个栈
- 面试题--用两个栈实现一个队列