队列实现栈,栈实现队列

//队列实现栈
class MyStack {

    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    private int size;
    /** Initialize your data structure here. */
    public MyStack() {
        this.queue1 = new LinkedList<Integer>();
        this.queue2 = new LinkedList<Integer>();
        this.size = 0;
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        if(queue1.peek() != null){
            queue1.offer(x);
        }else if(queue2.peek() != null){
            queue2.offer(x);
        }else{
            //都为空,则用queue1开始
            queue1.offer(x);
        }
        size++;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(empty()){
            throw new RuntimeException("无数据");
        }
        int e = 0;
        if(queue2.peek() != null){
            for(int i = 0; i < size - 1; i++){
                queue1.offer(queue2.poll());
            }
            e = queue2.poll();
        }else{
            for(int i = 0; i < size - 1; i++){
                queue2.offer(queue1.poll());
            }
            e = queue1.poll();
        }
        this.size--;
        return e;
    }
    
    /** Get the top element. */
    public int top() {
        if(empty()){
            throw new RuntimeException("无数据");
        }
        int e = 0;
        if(queue2.peek() != null){
            for(int i = 0; i < size; i++){
                e = queue2.poll();
                queue1.offer(e);
            }
        }else{
            for(int i = 0; i < size; i++){
                e = queue1.poll();
                queue2.offer(e);
            }
        }
        return e;   
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
       return size == 0;
    }
    
    public int size(){
        return this.size;
    }
}



//栈实现队列
class MyQueue {

    Stack <Integer> stack = new Stack<Integer>();
	Stack <Integer> stackTemp = new Stack<Integer>();
	/** Initialize your data structure here. */
	public MyQueue() {
		
	}

	/** Push element x to the back of queue. */
	public void push(int x) {
		stack.push(x);
		
	}

	/** Removes the element from in front of queue and returns that element. */
	public int pop() {		
        if(stackTemp.empty()) {
           while(!stack.empty()){
			    stackTemp.push(stack.pop());
		    } 
        }
        int temp = 0;
        if(!stackTemp.empty()) {
            temp = stackTemp.pop();
        }
		/*
		while(!stackTemp.isEmpty()){
			stack.push(stackTemp.pop());
		}*/	
		return temp;
	}

	/** Get the front element. */
	public int peek() {
		while(!stack.empty()){
			stackTemp.push(stack.pop());
		}
		int temp = stackTemp.peek();
		while(!stackTemp.empty()){
			stack.push(stackTemp.pop());
		}		
		return temp;
	}

	/** Returns whether the queue is empty. */
	public boolean empty() {
		return stackTemp.empty() && stack.empty();
	}
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值