C++ Program to Implement Queue using Linked List
Last Updated :
27 May, 2024
Queue is the fundamental data structure that follows the First In, First Out (FIFO) principle where the elements are added at the one end, called the rear and removed from other end called the front. In this article, we will learn how to implement queue in C++ using a linked list.
Queue Using Linked List in C++
The queue can implemented using the linked list which consists of the nodes where the each node contains the two parts:
- Data: The value or the data can be stored in the node.
- Next: The pointer to the next node in queue.
The queue itself maintains the two pointers:
- Front: It can be used the points to the first node of the linked list.
- Rear: It can be used the points to the last node of the linked list.
When the queues is empty, both the front and rear pointers are the nullptr. When the new element is enqueued, it is added at the rear and when the element is dequeued, it is removed from the front.
Basic Operations on Queue in C++
The following are the basic operation on the queue in C++ that is implemented using linked list:
Operation
| Description
| Time Complexity
| Space Complexity
|
---|
Enqueue
| It adds the new element to the end of the queue.
| O(1)
| O(1)
|
---|
Dequeue
| It removes the element from the front of the queue.
| O(1)
| O(1)
|
---|
Peek
| It retrieves but does not remove then the front element of the queue.
| O(1)
| O(1)
|
---|
IsEmpty
| Returns true is the queue is empty otherwise returns false.
| O(1)
| O(1)
|
---|
Implementation of Basic Operations
The above basic operations are implemented using the following algorithms:
Algorithm of Enqueue
- Create the new node with the given data.
- Check if the queue is empty.
- If it is empty then set the front and rear pointers to this new node.
- If it is not empty then link the current rear nodes next pointer to this new node.
- Update the rear pointer to the point to the new node.
Algorithm of Dequeue
- Check if the queue is empty.
- If queue is empty then return from the function or throw an error.
- Save the current front node in the temporary variable.
- Move the front pointer to next node in the queue.
- Delete the old front node.
- If the queue becomes the empty then update the rear pointer to nullptr as well.
Algorithm of Peek
- Check if queue is empty.
- If queue is empty then throw the error or return the sentinel value indicating the queue is empty.
- Return the data from the front node.
Algorithm of isEmpty
- Return the true if the front pointer is nullptr otherwise return false.
C++ Program to Implement Queue using Linked List
C++
#include <iostream>
using namespace std;
// Define Node structure
struct Node {
// The data held by the node
int data;
// Pointer to the next node in the list
Node* next;
};
// Define Queue class
class Queue {
// Pointer to the front node of the queue
Node* front;
// Pointer to the rear node of the queue
Node* rear;
public:
// Constructor initializes an empty queue
Queue()
: front(nullptr)
, rear(nullptr)
{
}
// Enqueue adds an element at the end of the queue
void enqueue(int x)
{
// Create a new node with given data
Node* newNode = new Node{ x, nullptr };
// If the queue is empty
if (rear == nullptr) {
// Both front and rear point to the new node
front = rear = newNode;
}
else {
// Link the new node at the end of the queue
rear->next = newNode;
// Update rear to the new node
rear = newNode;
}
}
// Dequeue removes the element at the front of the queue
void dequeue()
{
// If the queue is empty, do nothing
if (front == nullptr)
return;
// Temporary pointer to the front node
Node* temp = front;
// Move front to the next node
front = front->next;
// If the queue is now empty
if (front == nullptr)
// Set rear to nullptr
rear = nullptr;
// Delete the old front node
delete temp;
}
// Peek returns the front element of the queue
int peek()
{
if (!isEmpty())
return front->data;
else
throw runtime_error("Queue is empty");
}
// isEmpty checks if the queue is empty
bool isEmpty()
{
// Return true if front is nullptr
return front == nullptr;
}
};
// Main function
int main()
{
// Create a queue
Queue q;
// Enqueue elements into the queue
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
// Output the front element
cout << "Front element is: " << q.peek() << endl;
// Dequeue the front element
q.dequeue();
// Output the new front element
cout << "Front element is: " << q.peek() << endl;
// Dequeue the remaining elements
q.dequeue();
q.dequeue();
// Check if the queue is empty and output the result
cout << "Queue is empty: " << q.isEmpty() << endl;
return 0;
}
OutputFront element is: 10
Front element is: 20
Queue is empty: 1
Time and Space Complexity
- The time complexity of the implementation of queue using linked list is O(1).
- The space complexity of the implementation of queue using linked list is O(n), where n is number of the nodes in the linked list.
Application of the Linked List Queue
- It can applies in operating system uses the queues for job scheduling.
- It can be used in web servers for the handling incoming requests in the order.
- It can be used for the buffering data streams into the media players.
- It can applies on the inter-process communication for the facilitate asynchronous data exchange between the processes.
Conclusion
Implementing the queue using Linked list in the C++ offers the advantages of the dynamic memory usage. It can allowing the queue to the expand based on the needs of the application. This approach can particularly useful in the scenarios where the maximum size of the queue is not known in advance.
Similar Reads
C Program to Implement Singly Linked List A linked list is a linear data structure used to store elements of the same data type but not in contiguous memory locations. It is a collection of nodes where each node contains a data field and a next pointer indicating the address of the next node. So only the current node in the list knows where
9 min read
C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read
C Program to Implement Priority Queue Priority queue is an abstract data type(ADT) in the computer science which is designed to the operate much like the regular queue except that each element has the certain priority. The priority can determines the order in which elements are dequeued - elements with the higher priority are removed fr
6 min read
Queue using Linked List in C Queue is a linear data structure that follows the First-In-First-Out (FIFO) order of operations. This means the first element added to the queue will be the first one to be removed. There are different ways using which we can implement a queue data structure in C.In this article, we will learn how t
7 min read
C Program to Implement Circular Linked List A linked list is a dynamic data structure where elements are not stored in contiguous memory locations but linked using pointers. In a circular linked list, the last node points back to the first node instead of pointing to NULL, forming a circle.In this article, we will learn how to implement the c
8 min read
Queue Implementation Using Linked List in Java Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the cons
4 min read
C Program to Implement Circular Queue A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the last position back to the first, forming a circle. In this article, we will learn how to implement circular queue in C programming language. What is a Circular Queue in C?In a circular
5 min read
C++ Program to Implement Circular Queue In C++, Queues are a fundamental data structure in computer science which works on the principle of FIFO (First In, First Out). They can be implemented using both array and linked list. A circular queue is a type of queue in which the last element is connected to the first element, forming a circula
7 min read
Stack Using Linked List in C Stack is a linear data structure that follows the Last-In-First-Out (LIFO) order of operations. This means the last element added to the stack will be the first one to be removed. There are different ways using which we can implement stack data structure in C. In this article, we will learn how to i
7 min read
Priority Queue of Lists in C++ with Examples Priority Queue Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Functio
5 min read