3. Queues- Tarun

What is a Queue?

  • A collection designed for holding elements prior to processing.
  • Follows the First-In-First-Out (FIFO) principle: the first element added is the first one to be removed.
  • Useful in scenarios like scheduling( QUEUE SYSTEM IN TOOLKIT FOR EXAMPLE ), task processing, or handling events.

Key Operations

  • add(E e): Adds an element to the queue. If the queue is full, it throws an exception.
  • remove(): Removes and returns the head (first element) of the queue. Throws an exception if the queue is empty.
  • peek(): Returns the head of the queue without removing it. Returns null if the queue is empty.
  • isEmpty(): Returns true if the queue contains no elements.
  • size(): Returns the number of elements in the queue.
  • clear(): Removes all elements from the queue.

Example: Basic Queue Operations

Queue<String> queue = new LinkedList<>();

queue.add("Apple");
queue.add("Banana");
queue.add("Orange");
System.out.println("Queue after adding elements: " + queue);

String removedElement = queue.remove();
System.out.println("Removed element: " + removedElement);
System.out.println("Queue after removing an element: " + queue);

String firstElement = queue.peek();
System.out.println("First element (peek): " + firstElement);

System.out.println("Size of Queue: " + queue.size());

System.out.println("Is Queue empty: " + queue.isEmpty());

queue.clear();
System.out.println("Queue after clearing: " + queue);

System.out.println("Is Queue empty after clear: " + queue.isEmpty());
Queue after adding elements: [Apple, Banana, Orange]
Removed element: Apple
Queue after removing an element: [Banana, Orange]
First element (peek): Banana
Size of Queue: 2
Is Queue empty: false
Queue after clearing: []
Is Queue empty after clear: true

4. Deques- Tarun

What is a Deque?

  • A double-ended queue that allows insertion and removal of elements from both ends
  • Supports both FIFO (queue) and LIFO (stack) operations
  • More flexible than a standard queue
  • Common in sliding window problems, undo features, and scheduling tasks

Key Operations

  • addFirst(E e): Inserts element at the front
  • addLast(E e): Inserts element at the back
  • removeFirst(): Removes and returns the front element
  • removeLast(): Removes and returns the back element
  • peekFirst(): Returns (but does not remove) the front element
  • peekLast(): Returns (but does not remove) the back element
  • isEmpty(): Returns true if the deque is empty
  • size(): Returns the number of elements
  • clear(): Removes all elements from the deque

Key Takeaways

  • Two-End Access: Add or remove from both front and back
  • Stack + Queue Behavior: Use it like either a stack or a queue
  • Flexible & Efficient: Ideal for a variety of algorithmic patterns
  • Common Implementations: ArrayDeque, LinkedList
  • O(1) Operations: Most operations are constant time with ArrayDeque

Example: Basic Deque Operations

import java.util.Deque;
import java.util.ArrayDeque;


Deque<String> deque = new ArrayDeque<>();

deque.addFirst("B");
deque.addLast("C");
deque.addFirst("A");
deque.addLast("D");

System.out.println(deque);

System.out.println(deque.removeFirst());
System.out.println(deque.removeLast());

System.out.println(deque.peekFirst());
System.out.println(deque.peekLast());

System.out.println(deque.isEmpty());
System.out.println(deque.size());

deque.clear();
System.out.println(deque.isEmpty());

[A, B, C, D]
A
D
B
C
false
2
true

Example: Basic Queue Operations

Queue<String> queue = new LinkedList<>();

queue.add("Apple");
queue.add("Banana");
queue.add("Orange");
System.out.println("Queue after adding elements: " + queue);

String removedElement = queue.remove();
System.out.println("Removed element: " + removedElement);
System.out.println("Queue after removing an element: " + queue);

String firstElement = queue.peek();
System.out.println("First element (peek): " + firstElement);

System.out.println("Size of Queue: " + queue.size());

System.out.println("Is Queue empty: " + queue.isEmpty());

queue.clear();
System.out.println("Queue after clearing: " + queue);

System.out.println("Is Queue empty after clear: " + queue.isEmpty());
Queue after adding elements: [Apple, Banana, Orange]
Removed element: Apple
Queue after removing an element: [Banana, Orange]
First element (peek): Banana
Size of Queue: 2
Is Queue empty: false
Queue after clearing: []
Is Queue empty after clear: true