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