首页 > 试题广场 >

用两个栈模拟队列先进先出,模拟其add和romve功能,给出

[问答题]
用两个栈模拟队列先进先出,模拟其add和romve功能,给出思路和代码。
推荐
  1. 有A、B两个栈,进队列push到A,出队列从B中pop
  2. 在进队列前把B中的元素全部pop出来并push到A中
  3. 在出队列前把A中的元素全部pop出来并push到B中
编辑于 2015-02-10 10:51:31 回复(0)
两个栈stackIn 和 stackOut,
add:push stackIn
remove:若stackOut不为空,pop stackOut;若stackOut为空则把stackIn倒入stackOut,再pop stackOut。

public void add(String in){
    stackIn.push(in);
}

public String remove(){
    if(stackOut.isEmpty()){
        while(stackIn.isNotEmpty()){
            stackOut.push(stackIn.pop());  }
    }
    stackOut.pop();
}

发表于 2015-05-21 16:58:20 回复(0)
template<class T>
class Queue{   
public:
    Queue();
    ~Queue();
    void append(const T& node);
    T Delete();

private :
    stack<T>   stack1;
    stack<T>   stack2;
};
template<class T>
void Queue<T>::append(const T& elem){
    stack1.push(elem);
}
template<class T>
T Queue<T>::Delete(){
    if (stack2.size() <= 0){
        while (stack1.size()>0)
        {
            T&data = stack1.top();
            stack1.pop();
            stack2.push(data);
        }
    }
    if (stack2.size()==0)
            return -1;
    T head = stack2.top();
    stack2.pop();
    return head;
}
发表于 2016-06-03 11:47:05 回复(0)
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);
    }

}

发表于 2021-03-13 15:25:19 回复(0)
我的代码:
import java.util.Stack;

public class MyQueue {
private Stack<Integer>stack1=new Stack<>();
private Stack<Integer>stack2=new Stack<>();
public void Add(Integer e) {
stack1.push(e);
}
public int remove() {
if(stack1.size()==0)
return Integer.MAX_VALUE;
if(stack1.size()!=0)
{
if(stack1.size()==1)
{
int res=stack1.peek();
stack1.pop();
return res;
}
while (stack1.size()!=1) {
stack2.add(stack1.pop());
}
int res=stack1.peek();
stack1.pop();
while (stack2.size()>0)
stack1.add(stack2.pop());
return res;
}
return Integer.MAX_VALUE;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyQueue myQueue=new MyQueue();
myQueue.Add(1);
myQueue.Add(2);
myQueue.Add(3);
myQueue.Add(4);
myQueue.Add(5);
System.out.println(myQueue.remove());
System.out.println(myQueue.remove());
System.out.println(myQueue.remove());
System.out.println(myQueue.remove());
System.out.println(myQueue.remove());
}

}

发表于 2015-09-11 17:16:40 回复(0)
<div> 思路:栈A只负责进队列,栈B只负责出队列,但栈A不空时,栈A的元素出栈,再进入栈B;当栈B 不空时,栈B里的元素出栈。 </div> <div> int fifo(&nbsp;&nbsp;) </div> <div> {int stack1[MAXSIZE], stack2[MAXSIZE]; </div> <div> int top1=top2=-1; </div> <div> for(int i=0;i&lt;n;i++) </div> <div> stack[top1++]=A[i]; </div> <div> if(top1!=-1) </div> <div> stack[top2++]=staclk[top1--]; </div> <div> if(top2!=-1) </div> <div> cout&lt;&lt;ack[top2--]&lt;&lt;endl; </div> <div> } </div>
发表于 2015-09-11 11:29:08 回复(0)
<div> 堆栈的特征:FILO 先进后出 </div> <div> 队列的特征:FIFO 先进先出 </div> <div> 所以,用两个栈s1和s2模拟一个队列时,s1作输入栈,逐个元素压栈,以此模拟队列元素的入队。 </div> <div> 当需要出队时,将栈s1退栈并逐个压入栈s2中,s1中最先入栈的元素,在s2中处于栈顶。 </div> <div> s2退栈,相当于队列的出队,实现了先进先出。 </div> <div> 显然,只有栈s2为空且s1也为空,才算是队列空。 </div> <div> <div> class Solution </div> <div> { </div> <div> public: </div> <div> &nbsp; void push(int node) { </div> <div> &nbsp; &nbsp; &nbsp; stack1.push(node); </div> <div> &nbsp;} </div> <div> void remove() { </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; if(stack2.empty()){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(!stack1.empty()){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack2.push(stack1.top()); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack1.pop(); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; } </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; int ret=stack2.top(); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; stack2.pop(); </div> <div> &nbsp;} </div> <div> private: </div> <div> &nbsp;stack&lt;int&gt; stack1; </div> <div> &nbsp;stack&lt;int&gt; stack2; </div> <div> }; </div> </div>
发表于 2015-09-10 14:59:03 回复(0)
<pre class="prettyprint lang-java">//只从s1进栈 public void add(int v){ s.push(v); } //只从s2出栈 public int remove(){ if(!s2.isEmpty()){ return s2.pop(); }else{ if(!s1.isEmpty()){ while(!s1.isEmpty()){ s2.push(s1.pop()); } return s2.pop(); }else{ return -1; } } }</pre> <br />
发表于 2015-09-08 12:14:10 回复(0)
public class TwoStackToQueue {

public static void main(String[] args) {

MyQueue queue = new MyQueue();

System.out.println("入队的内容: " + queue.in("123"));

System.out.println("出队的内容: " + queue.out());

}
}

class MyQueue {
Stack<String> stackOne;
Stack<String> stackTwo;

/**
* 入队
* @param str
* @return
*/
public String in(String str) {

if (stackOne == null) {
stackOne = new Stack<String>();
}
return stackOne.push(str);

}

/**
* 出队
* @param str
* @return
*/
public String out() {

if (stackTwo == null) {
stackTwo = new Stack<String>();

}

while (stackOne != null && !stackOne.isEmpty()) {
stackTwo.push(stackOne.pop());
}

return stackTwo.pop();
}

}

发表于 2015-07-30 16:54:01 回复(0)
两个栈,1和2
入栈
:直接入栈1,
出栈:将栈1都出栈,入栈2,完毕后,然后栈2的栈头就是要出栈的那个,将其出栈后,再将栈2都出栈,入栈1
发表于 2015-04-27 00:35:49 回复(0)
1、有A、B两个栈,进队列push到A中,
2、出队列时,若B非空从B中pop;若B空则将A中所有元素依次弹出并压人B中
3、A、B多为空时,不能弹出
发表于 2015-04-14 11:47:54 回复(0)