Implement Queue using Two Stacks

本文介绍了一种使用两个栈来实现队列的方法,并详细解释了offer、poll、peek等操作的具体实现过程。通过这种方式,可以在平均情况下达到O(1)的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/*
how to use two stacks to implement the queue: offer, poll, peek,size, isEmpty
offer(3) offer(2) poll() offer(1) peek() offer(6) poll() poll()
                    3               2              2       1
in   (3 2)  (1  6)
out  (2)  (3)    6  (1)

stack1(in): is the only stack to store new elements when adding a new element into the queue
stack2(out): is the only stack to pop old element out of the queue.
when stack2 is empty, we move all data from stack1(in) to stack2(out) if any
* */
public class QueueUseStack {
    private Deque<Integer> in ;
    private Deque<Integer> out;
    public QueueUseStack(){
        in = new LinkedList<>();
        out = new LinkedList<>();
    }
    //offer: always goes to the in: o(1)
    public void offer(int val){
        in.push(val);
    }
    /*poll:
    * if the out has items, then pop;
    * else shuffle the in to out, then pop
    * corner cases: make the return type Integer, so the caller could do some checks
    * before pop, if the out still empty, return null
    * */
    public Integer poll(){
        if (out.isEmpty()){
            //shuffle
            shuffle();
        }
        if (out.isEmpty()) return null ;
        return out.pop();
    }
    //the only chance we need to shuffle is when out is empty and the caller is invoking poll or peek
    private void shuffle(){
        while (!in.isEmpty()){
            out.push(in.pop());
        }
    }

    //peek
    public Integer peek(){
        if (out.isEmpty()){
            //shuffle
            shuffle();
        }
        if (out.isEmpty()) return null ;
        return out.peek();
    }

    //size: o(1)
    public int size(){
        return in.size() + out.size();
    }

    //isEmpty: o(1)
    public boolean isEmpty(){
        return in.isEmpty() && out.isEmpty();
    }
}
class TestClass{
    public static void main(String[] args){
        QueueUseStack queueUseStack = new QueueUseStack();
        //offer(3) offer(2) poll() offer(1) peek() offer(6) poll() poll()
        queueUseStack.offer(3);
        queueUseStack.offer(2);
        System.out.println(queueUseStack.poll()); //3
        queueUseStack.offer(1);
        System.out.println(queueUseStack.peek()); //2
        queueUseStack.offer(6);
        System.out.println(queueUseStack.poll()); //2
        System.out.println(queueUseStack.poll());  //1
    }
}

time complexity: 

offer(), size(), isEmpty(): o(1)

for poll and peek:

worst case: offer n times, peek/poll only once: o(n)

average case: offer n times, peek/poll n times

2n: offer n times + peek/poll n times

n: shuffle n times: totally there are n items to shuffle

/n: average time complexity for each peek/poll

amortized time complexity: o((2n+n)/n) = o(1)

转载于:https://www.cnblogs.com/davidnyc/p/8456026.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值