Queues

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning the first element added is the first one to be removed. Think of it like a line of people waiting - the person who arrives first gets served first.
Properties
Section titled “Properties”- FIFO (First In, First Out) ordering: The first element added is the first one to be removed.
- Elements added at rear (enqueue): New elements are always added to the back of the queue.
- Elements removed from front (dequeue): Elements are always removed from the front of the queue.
- Can be implemented using arrays or linked lists: Flexible implementation options depending on needs.
Use Cases
Section titled “Use Cases”- Print job management: Handling print requests in the order they were submitted.
- Call center systems: Managing incoming calls in the order they arrive.
Operations
Section titled “Operations”- Enqueue: Add element to the rear (back) of the queue.
- Dequeue: Remove and return element from the front of the queue.
Advantages
Section titled “Advantages”- Fair processing (first come, first served): Ensures elements are processed in the order they arrive.
- Efficient for sequential processing: All basic operations have O(1) time complexity.
- Easy to implement: Simple structure with clear insertion and deletion points.
Disadvantages
Section titled “Disadvantages”- Limited access pattern: Can only access the front element directly.
Example
Section titled “Example”import java.util.Queue;import java.util.LinkedList;
Queue<String> students = new LinkedList<>();
// Enqueue elementsstudents.offer("Jalen");students.offer("Nawiesh");students.offer("Rafael");
// Get queue sizeSystem.out.println(students.size()); // Prints 3
// Accessing the front elementSystem.out.println(students.peek()); // Prints "Jalen"
// Dequeue elementsstudents.poll(); // Removes "Jalen", queue: [Nawiesh, Rafael]students.poll(); // Removes "Nawiesh", queue: [Rafael]System.out.println(students);