Sha256: f3090ad26afa992031e7b362ba0fed099d3dd343a61846c773a5ccaddf0ce0a2

Contents?: true

Size: 1.24 KB

Versions: 94

Compression:

Stored size: 1.24 KB

Contents

public final class Deque<T> {
    private Element<T> head;

    public void push(T value) {
        if (head == null) {
            head = new Element<>(value, null, null);
            head.next = head;
            head.prev = head;
            return;
        }

        Element<T> oldTail = head.prev;
        Element<T> tail = new Element<>(value, oldTail, head);
        oldTail.next = tail;
        head.prev = tail;
    }

    public T pop() {
        head = head.prev;
        return shift();
    }

    public void unshift(T value) {
        push(value);
        head = head.prev;
    }

    public T shift() {
        T value = head.value;

        Element<T> newHead = head.next;
        Element<T> newTail = head.prev;

        if (newHead == head) {
            head = null;
        }
        else {
            newHead.prev = newTail;
            newTail.next = newHead;
            head = newHead;
        }

        return value;
    }

    private static final class Element<T> {
        private final T value;
        private Element<T> prev;
        private Element<T> next;

        public Element(T value, Element<T> prev, Element<T> next) {
            this.value = value;
            this.prev = prev;
            this.next = next;
        }
    }
}

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
trackler-2.0.6.38 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.37 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.36 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.35 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.34 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.33 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.32 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.31 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.30 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.29 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.28 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.27 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.26 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.25 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.24 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.23 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.22 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.21 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.20 tracks/java/exercises/linked-list/src/example/java/Deque.java
trackler-2.0.6.19 tracks/java/exercises/linked-list/src/example/java/Deque.java