# How Queue Can Be Implemented Using Linked List Data Structure?

//

Heather Bennett

Queues are an essential data structure in computer science that follow the First-In-First-Out (FIFO) principle. They are widely used in various applications such as scheduling processes, handling requests, and managing resources. One of the most common ways to implement a queue is using a linked list.

## What is a Linked List?

A linked list is a linear data structure consisting of nodes, where each node contains a value and a reference (or pointer) to the next node in the list. Unlike arrays, linked lists do not require contiguous memory allocation, which makes them more flexible for dynamic data structures like queues.

## Implementing a Queue using Linked List

To implement a queue using a linked list, we can use two pointers: front and rear. The front pointer points to the first element in the queue, while the rear pointer points to the last element in the queue. Initially, both pointers are set to NULL since there are no elements in the queue.

Enqueue Operation:

To enqueue (insert) an element into the queue, we create a new node containing the value and set its next reference to NULL. Then we update the rear pointer to point to this new node. If it is the first element being enqueued, we also update the front pointer to point to this node.

Dequeue Operation:

To dequeue (remove) an element from the queue, we check if the front pointer is NULL. If it is, it means that there are no elements in the queue.

Otherwise, we store the value of the front node and update the front pointer to point to its next node. If the front pointer becomes NULL after this operation, it means that the queue is now empty, and we should update the rear pointer to NULL as well.

## Code Implementation

Let’s take a look at a simple implementation of a queue using a linked list in C++:

``````
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

class Queue {
private:
Node* front;
Node* rear;

public:
Queue() {
front = rear = NULL;
}

void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;

if (rear == NULL) {
front = rear = newNode;
}
else {
rear->next = newNode;
rear = newNode;
}
}

void dequeue() {
if (front == NULL) {
cout << "Queue is empty." << endl;
return;
}

Node* temp = front;
front = front->next;

if (front == NULL) {
rear = NULL; // Update rear when queue becomes empty
}

delete temp; // Free memory
}
};
```
```

## Conclusion

In this tutorial, we have learned how to implement a queue using a linked list data structure. Linked lists provide an efficient way to maintain the order of elements in the queue while allowing for dynamic memory allocation. Understanding how queues work and their implementation using linked lists is crucial for building efficient algorithms and solving various real-world problems.

Now that you have grasped the concept of implementing a queue using a linked list, you can explore further by implementing additional functionalities such as checking the size of the queue or peeking at the front element. Happy coding!