# What Is Queue Definition in Data Structure?

//

Scott Campbell

A queue is a fundamental data structure in computer science. It follows the First-In-First-Out (FIFO) principle, meaning that the item that is inserted first will be the first one to be removed. In simple words, a queue works just like a real-life queue or line.

## Definition of Queue:

A queue can be defined as an ordered collection of elements in which an element is inserted at one end, known as the rear, and removed from the other end, known as the front. The rear is always positioned at the last element, while the front represents the first element in the queue.

Queues are widely used in various computer algorithms and applications. They provide an efficient way to manage and process data in a sequential manner.

## Basic Operations on Queue:

A queue typically supports three primary operations:

• Enqueue: This operation inserts an element at the rear of the queue.
• Dequeue: This operation removes and returns the element from the front of the queue.
• Peek: This operation returns (but does not remove) the element from the front of the queue.

The enqueue operation adds an element to the rear of the queue, while dequeue removes an element from its front. Peek allows you to examine (or access) the element at the front without removing it.

## Real-Life Examples:

To better understand queues, let’s consider some real-life examples where queues are commonly used:

• Ticket Counter: When people stand in line to buy tickets, they form a queue. The person who arrives first gets their ticket first.
• Print Spooler: When multiple print jobs are sent to a printer, they are queued and processed in the order they were received.
• Waiting List: In many scenarios, such as restaurant reservations or hospital appointments, people are placed on a waiting list. The first person on the list gets the next available spot.

## Implementation of Queue:

A queue can be implemented using various data structures, such as arrays and linked lists. Arrays offer a simple implementation where elements are stored in a fixed-size container. Linked lists provide a dynamic implementation, allowing easy insertion and removal of elements at both ends.

### Array Implementation:

In an array implementation, two pointers called ‘front’ and ‘rear’ keep track of the elements. The front pointer points to the first element of the queue, while the rear pointer points to the last element.

• Simple to implement
• Memory-efficient for small-sized queues

• Fixed size – can cause overflow if more elements are added than the array capacity
• Inefficient for large-sized queues due to shifting elements during dequeue operation

In a linked list implementation, each element (node) contains both the data and a reference to the next node in line. The front pointer points to the first node (element), while the rear pointer points to the last node.

• No fixed size limitation – can dynamically grow as needed
• Efficient enqueue and dequeue operations