# Which Data Structure Is First in First Out?

//

Angela Bailey

When it comes to data structures, there are various types that serve different purposes. One common type is the First In First Out (FIFO) data structure.

As the name suggests, this data structure follows the principle of serving elements in the order they were added – the first element that was added will be the first one to be served. In this article, we will explore some of the popular data structures that follow the FIFO principle.

## Queue

A Queue is a fundamental data structure that follows the FIFO principle. It can be visualized as a line of people waiting for their turn. The person who arrives first gets served first, and as new people join the line, they stand at the end.

### Operations on Queue

A queue typically supports two main operations:

• Enqueue: This operation adds an element to the end of the queue.
• Dequeue: This operation removes and returns the element from the front of the queue.

The enqueue operation increases the size of the queue, while dequeue decreases it. Queues can be implemented using arrays or linked lists, depending on our requirements.

## Circular Queue

A Circular Queue is an extension of a regular queue that overcomes some of its limitations. In a regular queue implemented using an array, if we keep enqueueing elements continuously, eventually we will run out of space even if there are empty slots at the beginning due to dequeues. A circular queue solves this problem by reusing empty slots at both ends.

### Operations on Circular Queue

The operations supported by a circular queue are similar to those supported by a regular queue:

• Enqueue: This operation adds an element to the end of the circular queue.
• Dequeue: This operation removes and returns the element from the front of the circular queue.

In addition to these operations, a circular queue also supports:

• Front: This operation returns the element at the front of the circular queue without removing it.
• Rear: This operation returns the element at the end of the circular queue without removing it.

## Priority Queue

A Priority Queue is a type of data structure that serves elements based on their priority. The element with the highest priority gets served first, regardless of when it was added. In other words, elements are not served strictly in the order they were added.

### Operations on Priority Queue

A priority queue typically supports three main operations:

• Insertion: This operation adds an element to the priority queue based on its priority.
• Deletion: This operation removes and returns the highest-priority element from the priority queue.
• Peek: This operation returns (without removing) the highest-priority element from the priority queue.

A priority queue can be implemented using various data structures such as binary heaps, linked lists, or balanced search trees like AVL trees or Red-Black trees.

## In Conclusion

Data structures that follow the First In First Out (FIFO) principle are essential for many applications. Queues, circular queues, and priority queues are some popular examples that serve different purposes. Understanding these data structures and their operations can greatly enhance your ability to solve problems efficiently.

Remember, when it comes to data structures, choosing the right one depends on the specific requirements of your application.