![]() |
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 CA 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 C Representation of Linked Queue in CIn 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 { Basic Operations of Linked List Queue in CFollowing are the basic operations of the queue data structure that help us manipulate the data structure as needed:
Let’s see how these operations are implemented in the queue. Enqueue FunctionThe 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 FunctionFollowing is the algorithm for the enqueue function:
Dequeue FunctionThe 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 FunctionFollowing is the algorithm for the dequeue function:
Peek FunctionThe 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 FunctionThe following is the algorithm for the peek function:
IsEmpty FunctionThe 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 FunctionThe following is the algorithm for the isEmpty function:
C Program to Implement a Queue Using Linked ListThe below example demonstrates how to implement a queue using a linked list in C.
Output Queue: 10 -> 20 -> 30 -> 40 -> 50 -> NULL Queue: 30 -> 40 -> 50 -> NULL Benefits of Linked List Queue in CThe following are the major benefits of the linked list implementation over the array implementation:
ConclusionThe 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 ArticlesThe following are some articles about the Queue data structure that can improve your understanding of it: |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |