# What Is a Queue in Data Structure?

//

Scott Campbell

A queue is a commonly used data structure in computer science that follows the First-In-First-Out (FIFO) principle. It represents a collection of elements where the newest element is added at one end, known as the rear or tail, and the oldest element is removed from the other end, known as the front or head. Queues are widely used in various applications such as scheduling processes, handling requests, and implementing algorithms.

## Basic Operations on a Queue

A queue supports the following basic operations:

• Enqueue: This operation adds an element to the rear of the queue. It takes a constant amount of time, often referred to as O(1) time complexity.
• Dequeue: This operation removes and returns the element from the front of the queue.

Similar to enqueue, it also takes O(1) time complexity.

• Peek: This operation returns the element at the front of the queue without removing it. It allows you to access the oldest element without modifying the underlying data structure.
• IsEmpty: This operation checks whether the queue is empty or not. It returns a boolean value indicating if there are any elements present in the queue.

## Real-life Examples

To understand queues better, let’s consider a few real-life examples:

• Ticket Counter: Imagine standing in line at a ticket counter. The person who arrives first gets their ticket first, and so on. This scenario can be modeled using a queue data structure.
• Browsing History: When you visit websites in your browser, they are added to your browsing history.

You can navigate back by clicking the back button, which removes the most recent page you visited from the history. This behavior can be implemented using a queue.

• Printer Spooler: When multiple users send print requests to a printer, they are stored in a queue. The printer processes these requests one by one in the order they were received.

## Implementation

The implementation of a queue can be achieved using various data structures such as arrays or linked lists. Arrays provide a simple implementation where elements are stored sequentially, while linked lists allow for dynamic memory allocation and efficient enqueue and dequeue operations.

A common approach is to use an array-based implementation with two pointers, front and rear. The front pointer keeps track of the element at the front of the queue, while the rear pointer points to the next available position for enqueueing new elements.

### Array-based Implementation

To initialize an empty queue:

``````<pre><code><b>int MAX_SIZE = 100;
int queue[MAX_SIZE];
int front = 0;
int rear = -1;``````

The `MAX_SIZE` variable represents the maximum number of elements that can be stored in the queue. The `front` and `rear` variables are initialized accordingly.

To enqueue an element:

``````<pre><code>void enqueue(int element) {
// Check if queue is full
if (rear == MAX_SIZE - 1) {
printf("Queue Overflow");
// Handle overflow condition
return;
}

queue[++rear] = element;
}``````

The `enqueue` function adds the element to the rear of the queue. It first checks if the queue is full (`rear == MAX_SIZE – 1`) and handles the overflow condition if necessary.

To dequeue an element:

``````<pre><code>int dequeue() {
// Check if queue is empty
if (front > rear) {
printf("Queue Underflow");
// Handle underflow condition
return -1;
}

int element = queue[front++];
return element;
}``````

The `dequeue` function removes and returns the element from the front of the queue. It first checks if the queue is empty (`front > rear`) and handles the underflow condition if necessary.

## Conclusion

In conclusion, a queue is a fundamental data structure that follows the FIFO principle. It allows efficient insertion at one end and removal from the other end. Understanding queues and their operations is crucial for developing efficient algorithms and solving various real-life problems.

By incorporating queues into your programs, you can handle situations where elements need to be processed in a specific order or manage resources effectively. Remember to choose an appropriate implementation based on your requirements, whether it’s array-based or linked list-based.

Happy coding!