# What Is Queue in Data Structure Explain With Example?

//

Angela Bailey

A queue is a fundamental data structure in computer science that follows the First-In-First-Out (FIFO) principle. It is an abstract data type that represents a collection of elements with two main operations: enqueue and dequeue. In this article, we will explore what a queue is and how it works, using examples to illustrate its functionality.

## What is a Queue?

A queue is similar to a real-life queue or line of people waiting for their turn. The first person who joins the queue is the first one to be served, while new people joining the line enter at the end. This concept can be applied to various scenarios in programming, such as processing requests, printing documents in order, or implementing breadth-first search algorithms.

## How Does a Queue Work?

In a queue, elements are added (enqueue) at one end called the rear and removed (dequeue) from the other end called the front. This arrangement ensures that the oldest element in the queue is always at the front and will be dequeued first.

### Enqueue Operation

The enqueue operation adds an element to the rear of the queue. When enqueuing an element, we increase the size of our queue by one and update its rear pointer accordingly.

``````<ul>
<li>Create an empty queue.</li>
<li>Check if the queue is full.</li>
<li>If not full, add element at rear position.</li>
<li>Update rear pointer.</li>
</ul>``````

### Dequeue Operation

The dequeue operation removes an element from the front of the queue. When dequeuing an element, we decrease the size of our queue by one and update its front pointer accordingly.

``````<ul>
<li>Check if the queue is empty.</li>
<li>If not empty, remove element from front position.</li>
<li>Update front pointer.</li>
</ul>``````

## Example

Let’s consider an example to understand how a queue works. We will create a queue to store integers.

``````<p><b><u>Example:</u></b></p>

<p><b><u>Queue Operations:</u></b></p>

<p><h3>Initially: Empty Queue</h3>>

<p style="margin-left: 40px;">Enqueue (Add elements to the rear):
Queue -rear: null, front: null
Enqueue 10: Queue -rear: 10, front: 10
Enqueue 20: Queue -rear: 20, front: 10
Enqueue 30: Queue -rear: 30, front: 10

<p style="margin-left: 40px;">Dequeue (Remove elements from the front):
Queue -rear: 30, front: 10
Dequeue: 10 (removed)
Queue -rear: 30, front: 20
Dequeue: 20 (removed)
Queue -rear: 30, front: 30
Dequeue: 30 (removed)

<p><h3>Finally: Empty Queue</h3>> ``````

In the example above, we start with an empty queue. We enqueue three elements: 10, 20, and 30.

Each insertion is done at the rear of the queue. After enqueuing these elements, we perform dequeue operations to remove elements from the front of the queue. As a result, we obtain an empty queue again.

## Conclusion

A queue is a versatile data structure that follows the FIFO principle. It is widely used in programming and can be implemented using various programming languages. Understanding queues and their operations is essential for developing efficient algorithms and solving problems effectively.

I hope this article has provided you with a clear understanding of queues in data structures!

Note: The examples provided in this article are simplified for clarity and may not represent a complete implementation of a queue.

Privacy Policy