Data Structures Questions
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It works by maintaining a collection of elements in a specific order, where the element that is added first is the one that is removed first.
In a queue, elements are added at one end called the rear or tail, and removed from the other end called the front or head. This ensures that the oldest element in the queue is always at the front, and the newest element is always at the rear.
The operations performed on a queue include:
1. Enqueue: Adding an element to the rear of the queue.
2. Dequeue: Removing the element from the front of the queue.
3. Peek: Viewing the element at the front of the queue without removing it.
4. IsEmpty: Checking if the queue is empty.
5. Size: Determining the number of elements in the queue.
Queues are commonly used in various applications such as scheduling processes, handling requests, and implementing breadth-first search algorithms.