Linear Data Structures
Queue

Queue

Overview

A queue is a data structure that organizes elements in a linear fashion, following the First-In-First-Out (FIFO) rule. In simpler terms, it operates like a waiting line where the first person to join the line is the first one to be served.

Imagine a queue at a supermarket checkout. The first person to enter the queue will be the first to reach the cashier when it's their turn to be served. As new customers join the queue, they line up behind the others, forming a sequence.

Queues have two primary operations:

Enqueue: This operation adds an element to the back of the queue, expanding the sequence.

Dequeue: This operation removes the front element from the queue, just like the first person in the line is served and leaves.

These operations allow us to manage data in a FIFO manner, making queues valuable for various practical applications. Besides enqueue and dequeue, other common operations on a queue include:

Front: This operation retrieves the element at the front of the queue without removing it, allowing you to check who's next in line (similar to stack's Top/Peak operation).

Is Empty: This operation is self-explanatory - it just checks if the queue is empty and returns a true or false value.

Size: Another simple operation - returns the current number of elements in the queue.

Queues are not as simple as array - instead, they can be implemented using arrays, linked lists, or other dynamic data structures, depending on specific requirements and constraints.

For instance, a queue can be utilized in simulating processes that occur in a specific order, managing tasks in a computer system, or processing elements in the order they arrive, like in a network packet processing system. Overall, queues play a fundamental role in algorithms, simulations, and systems where maintaining the order of elements is essential.

Code

For Java, we have used integrated language constructs like LinkedList and queue but by association with other code examples and the read above, queues can be implemented from scratch in those languages too.



class Queue:
    def __init__(self):
        self.queue = []
 
    def enqueue(self, item):
        self.queue.append(item)
 
    def dequeue(self):
        if not self.is_empty():
            return self.queue.pop(0)
 
    def is_empty(self):
        return len(self.queue) == 0
 
    def size(self):
        return len(self.queue)
 
# Example usage:
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
 
while not queue.is_empty():
    print(queue.dequeue())
 
 
class Queue {
    constructor() {
        this.queue = [];
    }
 
    enqueue(item) {
        this.queue.push(item);
    }
 
    dequeue() {
        if (!this.isEmpty()) {
            return this.queue.shift();
        }
    }
 
    isEmpty() {
        return this.queue.length === 0;
    }
 
    size() {
        return this.queue.length;
    }
}
 
// Example usage:
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
 
while (!queue.isEmpty()) {
    console.log(queue.dequeue());
}
 
 
import java.util.LinkedList;
import java.util.Queue;
 
public class QueueExample {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
 
        while (!queue.isEmpty()) {
            System.out.println(queue.remove());
        }
    }
}
 
 
#include <stdio.h>
#define MAX_SIZE 100
 
struct Queue {
    int items[MAX_SIZE];
    int front;
    int rear;
};
 
void enqueue(struct Queue* q, int item) {
    if (q->rear == MAX_SIZE - 1) {
        printf("Queue is full.\n");
    } else {
        q->rear++;
        q->items[q->rear] = item;
    }
}
 
int dequeue(struct Queue* q) {
    if (q->front > q->rear) {
        printf("Queue is empty.\n");
        return -1;
    } else {
        int item = q->items[q->front];
        q->front++;
        return item;
    }
}
 
// Example usage:
int main() {
    struct Queue q;
    q.front = 0;
    q.rear = -1;
 
    enqueue(&q, 1);
    enqueue(&q, 2);
    enqueue(&q, 3);
 
    while (q.front <= q.rear) {
        printf("%d\n", dequeue(&q));
    }
 
    return 0;
}
 
class Queue {
    private $queue = array();
 
    public function enqueue($item) {
        array_push($this->queue, $item);
    }
 
    public function dequeue() {
        if (!$this->isEmpty()) {
            return array_shift($this->queue);
        }
    }
 
    public function isEmpty() {
        return empty($this->queue);
    }
 
    public function size() {
        return count($this->queue);
    }
}
 
// Example usage:
$queue = new Queue();
$queue->enqueue(1);
$queue->enqueue(2);
$queue->enqueue(3);
 
while (!$queue->isEmpty()) {
    echo $queue->dequeue() . "\n";
}
 
We always try to improve this course so if you spot errors, inconsistencies or other issues, contact us.