Queue using Linked List in C
Last Updated :
15 Jul, 2024
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 to implement a queue using a linked list in C, its basic operations along with their time and space complexity analysis, and the benefits of a linked list queue in C.
Linked List Implementation of Queue in C
A queue is generally implemented using an array, but the limitation of this kind of queue is that the memory occupied by the array is fixed no matter how many elements are in the queue. In the queue implemented using a linked list, the size occupied by the linked list will be equal to the number of elements in the queue. Moreover, its size is dynamic, meaning that the size will change automatically according to the elements present.
Queue in CRepresentation of Linked Queue in C
In C, the queue that is implemented using a linked list can be represented by pointers to both the front and rear nodes of the linked list. Each node in that linked list represents an element of the queue. The type of linked list here is a singly linked list in which each node consists of a data field and the next pointer.
struct Node {
int data;
struct Node* next;
};
Basic Operations of Linked List Queue in C
Following are the basic operations of the queue data structure that help us manipulate the data structure as needed:
Operation | Description | Time Complexity | Space Complexity |
---|
isEmpty | Returns true if the queue is empty, false otherwise. | O(1) | O(1) |
---|
Enqueue | This operation is used to add/insert data into the queue. | O(1) | O(1) |
---|
Dequeue | This operation is used to delete/remove data from the queue. | O(1) | O(1) |
---|
Peek | This operation returns the front element in the queue. | O(1) | O(1) |
---|
Let’s see how these operations are implemented in the queue.
Enqueue Function
The enqueue function will add a new element to the queue. To maintain the time and space complexity of O(1), we will insert the new element at the end of the linked list. The element at the front will be the element that was inserted first.
We need to check for queue overflow (when we try to enqueue into a full queue).
Algorithm for Enqueue Function
Following is the algorithm for the enqueue function:
- Create a new node with the given data.
- If the queue is empty, set the front and rear to the new node.
- Else, set the next of the rear to the new node and update the rear.
Dequeue Function
The dequeue function will remove the front element from the queue. The front element is the one that was inserted first, and it will be present at the front of the linked list.
We need to check for queue underflow (when we try to dequeue from an empty queue).
Algorithm for Dequeue Function
Following is the algorithm for the dequeue function:
- Check if the queue is empty.
- If not empty, store the front node in a temporary variable.
- Update the front pointer to the next node.
- Free the temporary node.
- If the queue becomes empty, update the rear to NULL.
Peek Function
The peek function will return the front element of the queue if the queue is not empty. The front element is the one at the front of the linked list.
Algorithm for Peek Function
The following is the algorithm for the peek function:
- Check if the queue is empty.
- If empty, return -1.
- Else, return the front->data.
IsEmpty Function
The isEmpty function will check if the queue is empty or not. This function returns true if the queue is empty; otherwise, it returns false.
Algorithm of isEmpty Function
The following is the algorithm for the isEmpty function:
- Check if the front pointer of the queue is NULL.
- If NULL, return true, indicating the queue is empty.
- Otherwise, return false, indicating the queue is not empty.
C Program to Implement a Queue Using Linked List
The below example demonstrates how to implement a queue using a linked list in C.
C
// C program to implement queue using linked list
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node of the linked list
typedef struct Node {
int data;
struct Node* next;
} node;
// Define the structure for the queue
typedef struct Queue {
node* front;
node* rear;
} queue;
// Function to create a new node
node* createNode(int data)
{
// Allocate memory for a new node
node* newNode = (node*)malloc(sizeof(node));
// Check if memory allocation was successful
if (newNode == NULL)
return NULL;
// Initialize the node's data and next pointer
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to create a new queue
queue* createQueue()
{
// Allocate memory for a new queue
queue* newQueue = (queue*)malloc(sizeof(queue));
// Initialize the front and rear pointers of the queue
newQueue->front = newQueue->rear = NULL;
return newQueue;
}
// Function to check if the queue is empty
int isEmpty(queue* q)
{
// Check if the front pointer is NULL
return q->front == NULL;
}
// Function to add an element to the queue
void enqueue(queue* q, int data)
{
// Create a new node with the given data
node* newNode = createNode(data);
// Check if memory allocation for the new node was
// successful
if (!newNode) {
printf("Queue Overflow!\n");
return;
}
// If the queue is empty, set the front and rear
// pointers to the new node
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
// Add the new node at the end of the queue and update
// the rear pointer
q->rear->next = newNode;
q->rear = newNode;
}
// Function to remove an element from the queue
int dequeue(queue* q)
{
// Check if the queue is empty
if (isEmpty(q)) {
printf("Queue Underflow\n");
return -1;
}
// Store the front node and update the front pointer
node* temp = q->front;
q->front = q->front->next;
// If the queue becomes empty, update the rear pointer
if (q->front == NULL)
q->rear = NULL;
// Store the data of the front node and free its memory
int data = temp->data;
free(temp);
return data;
}
// Function to return the front element of the queue
int peek(queue* q)
{
// Check if the queue is empty
if (isEmpty(q))
return -1;
// Return the data of the front node
return q->front->data;
}
// Function to print the queue
void printQueue(queue* q)
{
// Traverse the queue and print each element
node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
// Create a new queue
queue* q = createQueue();
// Enqueue elements into the queue
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
enqueue(q, 40);
enqueue(q, 50);
// Print the queue
printf("Queue: ");
printQueue(q);
// Dequeue elements from the queue
dequeue(q);
dequeue(q);
// Print the queue after deletion of elements
printf("Queue: ");
printQueue(q);
return 0;
}
OutputQueue: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Queue: 30 -> 40 -> 50 -> NULL
Benefits of Linked List Queue in C
The following are the major benefits of the linked list implementation over the array implementation:
- The dynamic memory management of the linked list provides a dynamic size to the queue that changes with the number of elements.
- Rarely reaches the condition of queue overflow.
Conclusion
The linked list implementation of the queue shows that even while providing such benefits, it can only be used when we are ready to bear the cost of implementing the linked list also in our C program. However, if we already have a linked list, we should prefer this implementation over the array one.
Related Articles
The following are some articles about the Queue data structure that can improve your understanding of it:
Suggested Quiz
5 Questions
What is the main advantage of implementing a queue using a linked list in C?
-
Dynamic memory management
-
Constant time complexity for all operations
-
Ability to store larger amounts of data
-
What is the time complexity of the enqueue operation in a linked list queue implementation in C?
Which of the following is not a basic operation of a queue implemented using a linked list?
What is the time complexity of the isEmpty function in a linked list queue implementation in C?
What is the benefit of using a linked list over an array to implement a queue?
-
Constant time complexity for all operations
-
Ability to store larger amounts of data
-
Dynamic memory management
-
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Similar Reads
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
C++ Program to Implement Queue using Linked List
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
5 min read
Linked List meaning in DSA
A linked list is a linear data structure used for storing a sequence of elements, where each element is stored in a node that contains both the element and a pointer to the next node in the sequence. Linked ListTypes of linked lists: Linked lists can be classified in the following categories Singly
4 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
Linked List in C++
In C++, a linked list is a linear data structure that allows the users to store data in non-contiguous memory locations. A linked list is defined as a collection of nodes where each node consists of two members which represents its value and a next/previous pointer which stores the address for the n
6 min read
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
Doubly Linked List in C
A doubly linked list is a type of linked list in which each node contains 3 parts, a data part and two addresses, one points to the previous node and one for the next node. It differs from the singly linked list as it has an extra pointer called previous that points to the previous node, allowing th
13 min read
Doubly Linked List in C++
A Doubly Linked List (DLL) is a two-way list in which each node has two pointers, the next and previous that have reference to both the next node and previous node respectively. Unlike a singly linked list where each node only points to the next node, a doubly linked list has an extra previous point
13 min read
IPC using Message Queues
A Message Queue is a linked list of messages stored within the kernel and identified by a message queue identifier. A new queue is created or an existing queue is opened by msgget(). New messages are added to the end of a queue by msgsnd(). Every message has a positive long integer type field, a non
5 min read
Circular Linked List in C++
A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead o
10 min read