用你熟悉的语言编写程序用两个栈(Stack)模拟队列(Queue)的先进先出操作,仅实现add、remove方法即可。
1)请先描述思路; 2)编写完整代码实现,编程语言不限。
import java.util.Stack; public class StackToQueue { static Stack<Integer> stack1 = new Stack<Integer>(); static Stack<Integer> stack2 = new Stack<Integer>(); public void add(int node) { stack1.push(node); } public int remove() { if(stack2.isEmpty()){//pop时如果stack2为空则将stack1内元素倒置入stack2,取栈顶元素; while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } } return stack2.pop(); } }望采纳!
template<class T> class MyQueue //自定义的MyQueue类 { public: void Add(T data); //自定义队列的增加功能 void Remove(T data); //自定义队列的删除功能 private: T front(); T end(); private: stack<T> stackIn; //入栈 stack<T> stackOut; //出栈 }; template<class T> void MyQueue<T>::Remove(T data) { if (stackOut.empty()) { while (!stackIn.empty()) { stackOut.push(stackIn.top()); stackIn.pop; } } if (!stackOut.empty()) { stackOut.pop(); } } template<class T> void MyQueue<T>::Add(T data) { stackIn.push(data); }
}
package StackAndQueue; import java.util.Stack; public class Main { private Stack<Integer> stackIn = null; private Stack<Integer> stackOut = null; public Main() { stackIn = new Stack<Integer>(); stackOut = new Stack<Integer>(); } public static void main(String[] args) { Main main = new Main(); main.add(1); main.add(2); main.add(3); main.add(4); main.add(5); main.add(6); main.add(7); main.add(8); main.remove(); main.remove(); main.remove(); main.remove(); main.remove(); main.remove(); main.remove(); main.remove(); } public void add(int i) { Integer push = (Integer) this.stackIn.push(new Integer(i)); System.out.println("元素 " + push + " 已进入队列"); System.out.println(stackIn); } public void remove() { if (stackIn.isEmpty()) { System.out.println("队列已空"); return; } while (!stackIn.isEmpty()) { stackOut.push(stackIn.pop()); } Integer pop = (Integer) stackOut.pop(); System.out.println("元素 " + pop + " 已退出队列"); while (!stackOut.isEmpty()) { stackIn.push(stackOut.pop()); } System.out.println(stackIn); } }
#include<iostream>
#include<string>
#include<stack>
#include<algoritnm>
using namespace std;
template<class T>
class Queue
{
public:
void add(T elem);
T remove();
private:
stack<T> stack1;
stack<T> stack2;
}
template<class T>
void Queue::add(elem){
stack1.push(elem);
}
template<class T>
T Queue::remove(){
if(stack2.empty())
{
if(stack1.empty()){
exit(-1);
}else
{
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
}
}
return stack2.pop();
}
int main(){
Queue<int> q;
for(int i=1;i<=9;i++)
q.add(i*10);
for(int i=1;i<=9;i++){
cout<<q.remove()<<" ";
}
cout<<endl;
return 0;
}
<分析>:
入队:将元素进栈A
出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;
void N_queue::N_push(int a) { st1.push(a); } int N_queue::N_pop() { int a; if(st2.empty()) { while (!st1.empty()) { a=st1.top(); st2.push(a); st1.pop(); } } a=st2.top(); st2.pop(); return a; } bool N_queue::N_empty() { return st1.empty()&&st2.empty(); }
用两个队列实现一个栈的功能?要求给出算法和思路!
<分析>:
入栈:将元素进队列A
出栈:判断队列A中元素的个数是否为1,如果等于1,则出队列,否则将队列A中的元素 以此出队列并放入队列B,直到队列A中的元素留下一个,然后队列A出队列,再把 队列B中的元素出队列以此放入队列A中。
void N_stack::N_push(int a) { qu1.push(a); } int N_stack::N_pop() { int a,b; while(qu1.size()>1) { a=qu1.front(); qu2.push(a); qu1.pop(); } b=qu1.front(); qu1.pop(); while(!qu2.empty()) { a=qu2.front(); qu1.push(a); qu2.pop(); } return b; } bool N_stack::N_empty() { return qu1.empty(); }