Queue Using Linked List
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning that the element that is inserted first will be the first one to be removed. Think of it like a real-world queue, such as people waiting in line; the first person who joins the queue is the first one to be served.
Implementing a queue using a linked list involves defining a structure for the nodes of the linked list and implementing functions to perform queue operations like enqueue (insertion) and dequeue (deletion)
Defining the node and Initializing the queue
struct Node
{
int data;
struct Node *next;
}*front=NULL,*rear=NULL;
Enqueue (Insertion) Algorithm:
1.Create a new node:
Allocate memory for a new node.
Set the data field of the new node with the given element.
2.Update pointers:
If the queue is empty, set both the front and rear pointers to the new node.
Otherwise, set the next pointer of the current rear node to the new node.
Update the rear pointer to the new node.
void enqueue()
{
struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
Dequeue (Deletion) Algorithm:
1.Check for an empty queue:
If the queue is empty, print an error message or return a special value to indicate an empty queue.
2.Retrieve data and update pointers:
Retrieve the data from the front node.
Move the front pointer to the next node.
If the front becomes NULL (indicating the queue is now empty), update the rear pointer to NULL as well.
3.Free memory:
Free the memory occupied by the node that was dequeued.
int dequeue()
{
if (front==NULL)
{printf("Queue is empty...underflow");return -1;}
else
{
struct Node *temp;
temp=front;
int rv=front->data;
front=front->next;
if(front==NULL) rear=NULL;
free(temp);
return(rv);
}
}
1.Check if the queue is empty:
If the front pointer is NULL, print "Queue is empty."
2.Traverse the linked list from front to rear:
2.Traverse the linked list from front to rear:
Initialize a temporary pointer to the front of the queue.
While the temporary pointer is not NULL:
While the temporary pointer is not NULL:
Print the data of the current node.
Move the temporary pointer to the next node.
Move the temporary pointer to the next node.
void displayQ()
{
struct Node *temp=front;
while(temp!=NULL)
{
printf(" %d---->",temp->data);
temp=temp->next;
}
printf(" NULL");
}
Complexity Analysis
let's analyze the time and space complexity of the enqueue, dequeue, and display operations in a queue implemented using a linked list.
Time Complexity
Enqueue (Insertion):Time complexity: O(1)
Enqueuing involves creating a new node and updating the rear pointer. Since these operations take a constant amount of time, the time complexity is constant.
Dequeue (Deletion):Time complexity: O(1)
Dequeuing involves updating the front pointer and freeing the memory of the dequeued node. These operations also take a constant amount of time.
Display:Time complexity: O(n)
Displaying all elements of the queue requires traversing the linked list from the front to the rear. In the worst case, where there are n elements in the queue, this takes O(n) time.
Space Complexity
Enqueue (Insertion):Space complexity: O(1)
Enqueuing involves creating a new node, which takes constant space.
Dequeue (Deletion):Space complexity: O(1)
Dequeuing involves freeing the memory of the dequeued node, which also takes constant space.
Display:Space complexity: O(1)
Explanation: The space required for displaying the queue elements is constant, as it does not depend on the size of the queue.
The linked list-based implementation of a queue provides efficient enqueue and dequeue operations with a constant time complexity of O(1). Displaying the entire queue takes O(n) time, where n is the number of elements in the queue. The space complexity for all operations is constant (O(1)) as each node consumes a fixed amount of memory.
These complexities make linked list-based queues a good choice for scenarios where frequent enqueue and dequeue operations are expected, and the size of the queue is not too large.
Queue using linked List - C Implementation
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
}*front=NULL,*rear=NULL;
void enqueue()
{
struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
int dequeue()
{
if (front==NULL)
{printf("Queue is empty...underflow");return -1;}
else
{
struct Node *temp;
temp=front;
int rv=front->data;
front=front->next;
if(front==NULL) rear=NULL;
free(temp);
return(rv);
}
}
void displayQ()
{
struct Node *temp=front;
while(temp!=NULL)
{
printf(" %d---->",temp->data);
temp=temp->next;
}
printf(" NULL");
}
void main()
{
int ch;
do
{
printf("\nMenu\n----\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
displayQ();
break;
case 2:
int data;
data=dequeue();
if(data!=-1)
printf("The dequed data is:%d\n",data);
displayQ();
break;
case 3:
displayQ();
break;
case 4:
exit(0);
default:
printf("There is no such operation:");
}
}
while(1);
}
Comments
Post a Comment