siteName > > datastructures.queues.Deque
Threads
java ( 7978 ) - java ( 7979 ) stack: com.thealgorithms.datastructures.queues.Deque.main(Deque.java:176)
package com.thealgorithms.datastructures.queues;

import java.util.NoSuchElementException;

/**
 * A [deque](https://en.wikipedia.org/wiki/Double-ended_queue) is short for a
 * double ended queue pronounced "deck" and sometimes referred to as a head-tail
 * linked list. A deque is a data structure based on a doubly linked list, but
 * only supports adding and removal of nodes from the beginning and the end of
 * the list.
 *
 * @author [Ian Cowan](https://github.com/iccowan)
 */
public class Deque<T> {

    /**
     * Node for the deque
     */
    private static class DequeNode<S> {
        S val;
        DequeNode<S> next = null;
        DequeNode<S> prev = null;

        DequeNode(S val) {
            this.val = val;
        }
    }

    private DequeNode<T> head = null;
    private DequeNode<T> tail = null;
    private int size = 0;

    /**
     * Adds the specified value to the head of the deque
     *
     * @param val Value to add to the deque
     */
    public void addFirst(T val) {
        DequeNode<T> newNode = new DequeNode<>(val);

        if (isEmpty()) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }

        size++;
    }

    /**
     * Adds the specified value to the tail of the deque
     *
     * @param val Value to add to the deque
     */
    public void addLast(T val) {
        DequeNode<T> newNode = new DequeNode<>(val);
        if (tail == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.prev = tail;
            tail.next = newNode;
            tail = newNode;
        }
        size++;
    }

    /**
     * Removes and returns the first (head) value in the deque
     *
     * @return the value of the head of the deque
     * @throws NoSuchElementException if the deque is empty
     */
    public T pollFirst() {
        if (head == null) {
            throw new NoSuchElementException("Deque is empty");
        }

        T oldHeadVal = head.val;
        if (head == tail) {
            head = null;
            tail = null;
        } else {
            head = head.next;
            head.prev = null;
        }
        size--;
        return oldHeadVal;
    }

    /**
     * Removes and returns the last (tail) value in the deque
     *
     * @return the value of the tail of the deque
     * @throws NoSuchElementException if the deque is empty
     */
    public T pollLast() {
        if (tail == null) {
            throw new NoSuchElementException("Deque is empty");
        }

        T oldTailVal = tail.val;
        if (head == tail) {
            head = null;
            tail = null;
        } else {
            tail = tail.prev;
            tail.next = null;
        }
        size--;
        return oldTailVal;
    }

    /**
     * Returns the first (head) value of the deque WITHOUT removing
     *
     * @return the value of the head of the deque, or null if empty
     */
    public T peekFirst() {
        return head != null ? head.val : null;
    }

    /**
     * Returns the last (tail) value of the deque WITHOUT removing
     *
     * @return the value of the tail of the deque, or null if empty
     */
    public T peekLast() {
        return tail != null ? tail.val : null;
    }

    /**
     * Returns the size of the deque
     *
     * @return the size of the deque
     */
    public int size() {
        return size;
    }

    /**
     * Returns whether or not the deque is empty
     *
     * @return whether or not the deque is empty
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * Returns a stringified deque in a pretty form:
     *
     * <p>
     * Head -> 1 <-> 2 <-> 3 <- Tail
     *
     * @return the stringified deque
     */
    @Override
    public String toString() {
        StringBuilder dequeString = new StringBuilder("Head -> ");
        DequeNode<T> currNode = head;
        while (currNode != null) {
            dequeString.append(currNode.val);
            if (currNode.next != null) {
                dequeString.append(" <-> ");
            }
            currNode = currNode.next;
        }
        dequeString.append(" <- Tail");
        return dequeString.toString();
    }

    public static void main(String[] args) {
        Deque<Integer> myDeque = new Deque<>();
        for (int i = 0; i < 42; i++) {
            if (i / 42.0 < 0.5) {
                myDeque.addFirst(i);
            } else {
                myDeque.addLast(i);
            }
        }

        System.out.println(myDeque);
        System.out.println("Size: " + myDeque.size());
        System.out.println();

        myDeque.pollFirst();
        myDeque.pollFirst();
        myDeque.pollLast();
        System.out.println(myDeque);
        System.out.println("Size: " + myDeque.size());
        System.out.println();

        int dequeSize = myDeque.size();
        for (int i = 0; i < dequeSize; i++) {
            int removing = -1;
            if (i / 39.0 < 0.5) {
                removing = myDeque.pollFirst();
            } else {
                removing = myDeque.pollLast();
            }

            System.out.println("Removing: " + removing);
        }

        System.out.println(myDeque);
        System.out.println(myDeque.size());
    }
}
Variables All
No.FromNameValue
1176args[Ljava.lang.String;@7852e922
END 0 00
Output All Filter Merge
Process FilterThread Filter
7978 java 7979 java
No.PNPIDTIDTNMessage
1java79787979javaHead -> 20 <-> 19 <-> 18 <-> 17 <-> 16 <-> 15 <-> 14 <-> 13 <-> 12 <-> 11 <-> 10 <-> 9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1 <-> 0 <-> 21 <-> 22 <-> 23 <-> 24 <-> 25 <-> 26 <-> 27 <-> 28 <-> 29 <-> 30 <-> 31 <-> 32 <-> 33 <-> 34 <-> 35 <-> 36 <-> 37 <-> 38 <-> 39 <-> 40 <-> 41 <- Tail
2java79787979javaSize: 42
3java79787979javaHead -> 18 <-> 17 <-> 16 <-> 15 <-> 14 <-> 13 <-> 12 <-> 11 <-> 10 <-> 9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1 <-> 0 <-> 21 <-> 22 <-> 23 <-> 24 <-> 25 <-> 26 <-> 27 <-> 28 <-> 29 <-> 30 <-> 31 <-> 32 <-> 33 <-> 34 <-> 35 <-> 36 <-> 37 <-> 38 <-> 39 <-> 40 <- Tail
4java79787979javaSize: 39
5java79787979javaRemoving: 18
6java79787979javaRemoving: 17
7java79787979javaRemoving: 16
8java79787979javaRemoving: 15
9java79787979javaRemoving: 14
10java79787979javaRemoving: 13
11java79787979javaRemoving: 12
12java79787979javaRemoving: 11
13java79787979javaRemoving: 10
14java79787979javaRemoving: 9
15java79787979javaRemoving: 8
16java79787979javaRemoving: 7
17java79787979javaRemoving: 6
18java79787979javaRemoving: 5
19java79787979javaRemoving: 4
20java79787979javaRemoving: 3
21java79787979javaRemoving: 2
22java79787979javaRemoving: 1
23java79787979javaRemoving: 0
24java79787979javaRemoving: 21
25java79787979javaRemoving: 40
26java79787979javaRemoving: 39
27java79787979javaRemoving: 38
28java79787979javaRemoving: 37
29java79787979javaRemoving: 36
30java79787979javaRemoving: 35
31java79787979javaRemoving: 34
32java79787979javaRemoving: 33
33java79787979javaRemoving: 32
34java79787979javaRemoving: 31
35java79787979javaRemoving: 30
36java79787979javaRemoving: 29
37java79787979javaRemoving: 28
38java79787979javaRemoving: 27
39java79787979javaRemoving: 26
40java79787979javaRemoving: 25
41java79787979javaRemoving: 24
42java79787979javaRemoving: 23
43java79787979javaRemoving: 22
44java79787979javaHead -> <- Tail
END 0 0 0 00
Project:Alg-Java
Update:20240824
Commit:a7cd97d7
Source Code:datastructures.queues.Deque
BuildTool:Java17
Compiler:Java17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine