剑指offer09 : 用两个栈实现队列

https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/submissions/

考察点: 栈的使用、队列的概念理解、一种数据结构的实现

package BasicDataStruct;

import java.util.Stack;

public class CQueue {
    private Stack in = new Stack<Integer>();
    private Stack out = new Stack<Integer>();

    public CQueue() {
    }

    public void appendTail(int value) {
        in.push(value);
    }

    public int deleteHead() throws Exception {
        if (!out.empty()) {
            return (int) out.pop();
        }
        while (!in.empty()) {
            out.push(in.pop());
        }
        if (!out.empty()) {
            return (int) out.pop();
        }
        throw new Exception();
    }


}

import BasicDataStruct.CQueue;

public class Offer09CQueue {

    public static void main(String[] args) throws Exception {
        CQueue cQueue = new CQueue();
        cQueue.appendTail(111);
        cQueue.appendTail(123);
        int deletedValue = cQueue.deleteHead();
        System.out.println(deletedValue);
    }
}

上一篇:剑指 Offer 09. 用两个栈实现队列


下一篇:推荐免费云服务器