|
| 1 | +package com.thealgorithms.queues; |
| 2 | + |
| 3 | +import java.util.NoSuchElementException; |
| 4 | + |
| 5 | +/** |
| 6 | + * Implementation of a Double-Ended Queue (Deque) using a doubly linked list. |
| 7 | + * <p> |
| 8 | + * A deque allows insertion and deletion from both the front and the rear. |
| 9 | + * It follows the FIFO principle at both ends. |
| 10 | + * </p> |
| 11 | + * |
| 12 | + * Example: |
| 13 | + * DequeUsingArrayOrLinkedList<Integer> deque = new DequeUsingArrayOrLinkedList<>(); |
| 14 | + * deque.addFront(1); |
| 15 | + * deque.addRear(2); |
| 16 | + * deque.removeFront(); // returns 1 |
| 17 | + * deque.removeRear(); // returns 2 |
| 18 | + * |
| 19 | + * @param <T> the type of elements stored in the deque |
| 20 | + */ |
| 21 | +public final class DequeUsingArrayOrLinkedList<T> { |
| 22 | + |
| 23 | + private Node<T> front; |
| 24 | + private Node<T> rear; |
| 25 | + private int size; |
| 26 | + |
| 27 | + /** Node class for doubly linked list */ |
| 28 | + private static class Node<T> { |
| 29 | + T data; |
| 30 | + Node<T> prev; |
| 31 | + Node<T> next; |
| 32 | + |
| 33 | + Node(T data) { |
| 34 | + this.data = data; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** Constructor to initialize an empty deque */ |
| 39 | + public DequeUsingArrayOrLinkedList() { |
| 40 | + front = null; |
| 41 | + rear = null; |
| 42 | + size = 0; |
| 43 | + } |
| 44 | + |
| 45 | + /** Add an element at the front of the deque */ |
| 46 | + public void addFront(T value) { |
| 47 | + Node<T> newNode = new Node<>(value); |
| 48 | + if (isEmpty()) { |
| 49 | + front = rear = newNode; |
| 50 | + } else { |
| 51 | + newNode.next = front; |
| 52 | + front.prev = newNode; |
| 53 | + front = newNode; |
| 54 | + } |
| 55 | + size++; |
| 56 | + } |
| 57 | + |
| 58 | + /** Add an element at the rear of the deque */ |
| 59 | + public void addRear(T value) { |
| 60 | + Node<T> newNode = new Node<>(value); |
| 61 | + if (isEmpty()) { |
| 62 | + front = rear = newNode; |
| 63 | + } else { |
| 64 | + rear.next = newNode; |
| 65 | + newNode.prev = rear; |
| 66 | + rear = newNode; |
| 67 | + } |
| 68 | + size++; |
| 69 | + } |
| 70 | + |
| 71 | + /** Remove and return the element from the front of the deque */ |
| 72 | + public T removeFront() { |
| 73 | + if (isEmpty()) { |
| 74 | + throw new NoSuchElementException("Deque is empty"); |
| 75 | + } |
| 76 | + T value = front.data; |
| 77 | + front = front.next; |
| 78 | + if (front != null) { |
| 79 | + front.prev = null; |
| 80 | + } else { |
| 81 | + rear = null; // deque became empty |
| 82 | + } |
| 83 | + size--; |
| 84 | + return value; |
| 85 | + } |
| 86 | + |
| 87 | + /** Remove and return the element from the rear of the deque */ |
| 88 | + public T removeRear() { |
| 89 | + if (isEmpty()) { |
| 90 | + throw new NoSuchElementException("Deque is empty"); |
| 91 | + } |
| 92 | + T value = rear.data; |
| 93 | + rear = rear.prev; |
| 94 | + if (rear != null) { |
| 95 | + rear.next = null; |
| 96 | + } else { |
| 97 | + front = null; // deque became empty |
| 98 | + } |
| 99 | + size--; |
| 100 | + return value; |
| 101 | + } |
| 102 | + |
| 103 | + /** Peek the front element without removing it */ |
| 104 | + public T peekFront() { |
| 105 | + if (isEmpty()) { |
| 106 | + throw new NoSuchElementException("Deque is empty"); |
| 107 | + } |
| 108 | + return front.data; |
| 109 | + } |
| 110 | + |
| 111 | + /** Peek the rear element without removing it */ |
| 112 | + public T peekRear() { |
| 113 | + if (isEmpty()) { |
| 114 | + throw new NoSuchElementException("Deque is empty"); |
| 115 | + } |
| 116 | + return rear.data; |
| 117 | + } |
| 118 | + |
| 119 | + /** Check if the deque is empty */ |
| 120 | + public boolean isEmpty() { |
| 121 | + return size == 0; |
| 122 | + } |
| 123 | + |
| 124 | + /** Return the number of elements in the deque */ |
| 125 | + public int size() { |
| 126 | + return size; |
| 127 | + } |
| 128 | +} |
0 commit comments