[Leetcode] 設計循環雙端隊列

設計實現雙端隊列。
你的實現需要支持以下操作:

  • MyCircularDeque(k):構造函數,雙端隊列的大小爲k。
  • insertFront():將一個元素添加到雙端隊列頭部。 如果操作成功返回 true。
  • insertLast():將一個元素添加到雙端隊列尾部。如果操作成功返回 true。
  • deleteFront():從雙端隊列頭部刪除一個元素。 如果操作成功返回 true。
  • deleteLast():從雙端隊列尾部刪除一個元素。如果操作成功返回 true。
  • getFront():從雙端隊列頭部獲得一個元素。如果雙端隊列爲空,返回 -1。
  • getRear():獲得雙端隊列的最後一個元素。 如果雙端隊列爲空,返回 -1。
  • isEmpty():檢查雙端隊列是否爲空。
  • isFull():檢查雙端隊列是否滿了。
public class MyCircularDeque {
    
    Node first, last;
    int capacity;
    int size;

    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
        this.capacity = k;    
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public boolean insertFront(int value) {
        if (size == capacity)
            return false;
        linkFirst(value);
        return true;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public boolean insertLast(int value) {
        if (size == capacity)
            return false;
        linkLast(value);
        return true;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public boolean deleteFront() {
        Node f = first;
        return f == null ? false : unlinkFirst(f);
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public boolean deleteLast() {
        Node l = last;
        return l == null ? false : unlinkLast(l);
    }
    
    /** Get the front item from the deque. */
    public int getFront() {
        Node f = first;
        return f == null ? -1 : f.val;
    }
    
    /** Get the last item from the deque. */
    public int getRear() {
        Node l = last;
        return l == null ? -1 : l.val;
    }
    
    /** Checks whether the circular deque is empty or not. */
    public boolean isEmpty() {
        return size == 0;
    }
    
    /** Checks whether the circular deque is full or not. */
    public boolean isFull() {
        return size == capacity;
    }
    
    void linkFirst(int val) {
        Node f = first;
        Node newNode = new Node(null, val, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else 
            f.prev = newNode;
        size++;
    }
    
    void linkLast(int val) {
        Node l = last;
        Node newNode = new Node(l, val, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
    }
    
    boolean unlinkFirst(Node f) {
        Node next = f.next;
        f.next = null;
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        return true;
    }
    
    boolean unlinkLast(Node l) {
        Node prev = l.prev;
        l.prev = null;
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        return true;
    }
    
    static class Node {
        int val;
        Node prev, next;
        
        Node (Node prev, int val, Node next) { 
            this.val = val;
            this.prev = prev;
            this.next = next;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章