由两个栈组成的队列(C++实现)
2021-04-09 07:25
标签:row c++ img mes dex std string class pac 代码: 由两个栈组成的队列(C++实现) 标签:row c++ img mes dex std string class pac 原文地址:https://www.cnblogs.com/shiheyuanfang/p/13376302.html
#include
# include
#include
{
public:
My_Queue(){};
~My_Queue(){};
int add(int var);
int poll();
int peek();
bool empty();
bool s_push_empty();
int stackPush_pop();
private:
stack
stack
};
int My_Queue::add(int var)
{
stackPush.push(var);
return 0;
}
{
throw runtime_error("Empty Queue !");
}
else{
while(!stackPush.empty()){
int top_val = stackPush.top();
stackPush.pop();
stackPop.push(top_val);
}
}
int top_value = stackPop.top();//返回stackPop栈顶元素的引用
stackPop.pop();//弹出stackPop栈顶元素
return top_value;
}
{
if(stackPush.empty()&&stackPop.empty()){
throw runtime_error("Empty Queue !");
}
else{
while(!stackPush.empty()){
int top_val = stackPush.top();
stackPush.pop();
stackPop.push(top_val);
}
}
int top_value = stackPop.top();
return top_value;
}
bool My_Queue::empty()
{
if(stackPush.empty()&&stackPop.empty())
return true;
else
return false;
}
int My_Queue::stackPush_pop()//stackPush弹出
{
int _top_var = stackPush.top();
}
}
bool My_Queue::s_push_empty()//判断stackPush为空?
{
if(stackPush.empty())
return true;
else
return false;
}
int main()
{
My_Queue myqueue;
int a[] = {1,3,2,2,1,5};
for(int i=0;i {
myqueue.add(a[i]);
}
std::cout while (!myqueue.s_push_empty()) {
}
myqueue.add(a[i]);
}
std::cout std::cout std::cout while(!myqueue.empty())
{
return 0;
}
上一篇:线程 等待/通知机制
下一篇:java中的序列化和反序列化