For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
While developing efficient data structures in C, one challenge developers often face is choosing the right implementation strategy. The Implementation of Queue Using Linked List offers a flexible and dynamic approach, especially useful when dealing with unknown or varying input sizes. This method allows seamless memory management without worrying about overflow as long as the system has memory available.
In many real-world scenarios, queues operate under unpredictable demands. A linked list-based queue ensures that enqueue and dequeue operations remain efficient even as the queue grows or shrinks. This article will walk you through each step of the Implementation of Queue Using Linked List, from understanding the basics to writing a complete program, covering algorithms, examples, outputs, and explanations. Also visit our Software Engineering Courses to get hands-on experience!
A queue is a linear data structure that follows the FIFO (First In First Out) principle. It means the element inserted first will be removed first. Queues are useful in task scheduling, handling requests, and many other applications. They allow elements to be added at the rear and removed from the front, ensuring orderly processing. This structure is widely used in operating systems, printers, and customer service systems.
If you’re ready to grow your AI and Data Science skills, check out these programs.
A linked list is a dynamic data structure consisting of nodes. Each node contains data and a pointer to the next node. Linked lists allow flexible memory allocation and easy insertion or deletion without shifting elements. You can explore linked list implementation in C for a deeper understanding.
Using a linked list for implementing a queue removes the fixed size limitation of arrays. It enables dynamic memory allocation, avoiding overflow unless the system runs out of memory. Also, insertions and deletions at the front or rear become efficient since no shifting of elements is required.
A queue using a linked list in C uses nodes connected by pointers. Each node holds a data value and a pointer to the next node. The queue maintains two pointers: front (points to the first node) and rear (points to the last node). New nodes are added at the rear and removed from the front.
Check out this detailed tutorial on queue implementation.
struct Node {
int data;
struct Node* next;
};
Each node stores an integer data and a pointer to the next node. You can modify the data type to store other data as required.
Initially, both front and rear pointers are set to NULL, indicating an empty queue:
struct Node* front = NULL;
struct Node* rear = NULL;
These pointers help in performing enqueue and dequeue operations efficiently.
The enqueue operation inserts an element at the rear of the queue. A new node is dynamically allocated and linked to the existing rear.
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
Output:
enqueue(10);
enqueue(20);
enqueue(30);
Explanation: After enqueueing 10, 20, and 30, the queue contains 10 at front and 30 at rear. The nodes are linked in the order: 10 -> 20 -> 30.
Refer to this tutorial on stack implementation using linked lists.
The dequeue operation removes an element from the front of the queue. It frees the memory of the removed node and updates the front pointer.
void dequeue() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
struct Node* temp = front;
printf("Dequeued: %d\n", temp->data);
front = front->next;
if (front == NULL) rear = NULL;
free(temp);
}
Output:
dequeue(); // Dequeued: 10
dequeue(); // Dequeued: 20
Explanation: First call removes 10, second removes 20. After dequeuing twice, the front points to 30, and rear remains at 30.
Peek returns the front element without removing it. isEmpty checks if the queue is empty.
int peek() {
if (front == NULL) {
printf("Queue is empty\n");
return -1;
}
return front->data;
}
int isEmpty() {
return front == NULL;
}
Output:
printf("Front element: %d\n", peek()); // Front element: 30
printf("Is queue empty? %s\n", isEmpty() ? "Yes" : "No"); // Is queue empty? No
Explanation: Peek returns 30, which is at the front. isEmpty returns No since queue still has elements.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
void dequeue() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
struct Node* temp = front;
printf("Dequeued: %d\n", temp->data);
front = front->next;
if (front == NULL) rear = NULL;
free(temp);
}
int peek() {
if (front == NULL) {
printf("Queue is empty\n");
return -1;
}
return front->data;
}
int isEmpty() {
return front == NULL;
}
int main() {
enqueue(50); // Ram arrives
enqueue(60); // Shyam arrives
enqueue(70); // Aniket arrives
printf("Front: %d\n", peek());
dequeue();
dequeue();
printf("Front: %d\n", peek());
dequeue();
dequeue();
return 0;
}
Output
Front: 50
Dequeued: 50
Dequeued: 60
Front: 70
Dequeued: 70
Queue is empty
Explanation: Ram (50), Shyam (60), Aniket (70) enter the queue. First peek returns 50 (front). After dequeuing Ram and Shyam, peek shows 70. Final dequeue removes Aniket. Further dequeue reports queue is empty.
Check out this guide on dynamic memory in C.
Learn more about the stack in C and its operations in this detailed guide on stack implementation.
The Implementation of Queue Using Linked List in C provides a dynamic and flexible approach to handle data in a first-in-first-out manner. It eliminates the limitations of fixed-size queues and enables efficient memory management for varying workloads. By mastering this implementation, developers can handle queue-based problems with confidence and adaptability.
The main advantage of implementing a queue using a linked list is dynamic memory allocation. It allows flexible growth and shrinkage of the queue without worrying about a fixed size or memory wastage like in arrays.
Both enqueue and dequeue operations update the front or rear pointers directly. Since no traversal or shifting is required, each operation takes constant time, resulting in an O(1) time complexity for insertion and deletion.
In a linked list queue, memory is allocated dynamically for each new node. Every time an element is added, a new node is created in memory. This continues until memory is full or the program ends.
A queue implemented using a linked list does not experience overflow in the traditional sense. It can keep growing as long as memory is available. Overflow occurs only when system memory is exhausted.
If we attempt to dequeue from an empty queue, an underflow condition occurs. Usually, a message or error is displayed indicating the queue is empty, and no element is removed or returned.
The peek operation retrieves the value at the front node without deleting it. It simply accesses the data stored at the front pointer, allowing the user to see the next element to be dequeued.
To handle multiple data types, the linked list node’s data field can be defined using a union or void pointer. This allows storing different types of data, making the queue more flexible in applications.
No, memory must be freed manually in languages like C. After deleting a node during dequeue, the program should explicitly call free() to release memory and avoid memory leaks in the system.
Common use cases include CPU task scheduling, print job management, handling server requests, and buffering data streams. The linked list-based queue ensures dynamic memory handling for variable-sized workloads efficiently.
Initially, both front and rear pointers are set to NULL, indicating the queue is empty. When the first element is enqueued, both pointers point to the same newly created node in the linked list.
A linked list-based queue uses extra memory per node for storing pointers. While it avoids unused slots like arrays, the pointer overhead makes space complexity slightly higher per element compared to array-based implementations.
A linked list is preferred because it eliminates the need to predefine a fixed size. It allows the queue to grow and shrink dynamically, avoiding the need to shift elements or deal with wasted memory.
A circular queue uses an array with wrapping indices to reuse space, while a linked list queue grows dynamically without wrapping. Both follow FIFO, but memory handling and overflow behavior differ significantly.
Yes, a deque can be implemented using a doubly linked list. It allows insertion and deletion from both front and rear ends, providing greater flexibility compared to a simple queue implementation.
After dequeuing the last element, both front and rear pointers are reset to NULL. This marks the queue as empty and prevents dangling pointers from accessing deleted or invalid memory locations.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.