Data Structures: Queues, Trees, Graphs, and Searching Algorithms
Understanding Data Structures and Algorithms
8. Queues: FIFO Operations
A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. The element inserted first will be removed first, similar to people waiting in a line. It has two primary operations:
- enqueue(): Adds an element to the rear of the queue.
- dequeue(): Removes an element from the front of the queue.
Enqueue Operation Algorithm (Array-based):
- Check if the queue is full (rear == size - 1).
- If not full, increment rear.
- Insert the new element at
queue[rear].
Example:
if (rear == size - 1)
printf("Queue Overflow");
else {
rear++;
queue[rear] = value;
}
Dequeue Operation Algorithm:
- Check if the queue is empty (front > rear).
- If not empty, retrieve the element
with a size of 497.48 KB