Queue-linked List Implementation
Queue-linked List Implementation
The array implementation cannot be used for the large-scale applications where
the queues are implemented. One of the alternatives of array implementation is
linked list implementation of queue.
In a linked queue, each node of the queue consists of two parts i.e. data part and
the link part. Each element of the queue points to its immediate next element in
the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front
pointer and rear pointer. The front pointer contains the address of the starting
element of the queue while the rear pointer contains the address of the last
element of the queue.
Insertion and deletions are performed at rear and front end respectively. If front
and rear both are NULL, it indicates that the queue is empty.
There are two basic operations which can be implemented on the linked queues.
The operations are Insertion and Deletion.
Insert operation
The insert operation append the queue by adding an element to the end of the
queue. The new element will be the last element of the queue.
Firstly, allocate the memory for the new node ptr by using the following
statement.
1. Ptr = (struct node *) malloc (sizeof(struct node));
There can be the two scenarios of inserting this new node ptr into the linked
queue.
In the first scenario, we insert element into an empty queue. In this case, the
condition front = NULL becomes true. Now, the new element will be added as
the only element of the queue and the next pointer of front and rear pointer both,
will point to NULL.
In the second case, the queue contains more than one element. The condition
front = NULL becomes false. In this scenario, we need to update the end pointer
rear so that the next pointer of rear will point to the new node ptr. Since, this is a
linked queue, hence we also need to make the rear pointer point to the newly
added node ptr. We also need to make the next pointer of rear point to NULL.
In this way, the element is inserted into the queue. The algorithm and the C
implementation is given as follows.
Algorithm
ELSE
[END OF IF]
Step 4: END
Deletion
Deletion operation removes the element that is first inserted among all the
queue elements. Firstly, we need to check either the list is empty or not. The
condition front == NULL becomes true if the list is empty, in this case , we
simply write underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For
this purpose, copy the node pointed by the front pointer into the pointer ptr.
Now, shift the front pointer, point to its next node and free the node pointed by
the node ptr. This is done by using the following statements.
ptr = front;
free(ptr);
Algorithm
[END OF IF]
Step 5: END
#include<stdlib.h>
struct node
int data;
};
void insert();
void delete();
void display();
void main ()
int choice;
while(choice != 4)
printf("\n*************************Main
Menu*****************************\n");
printf("\
n========================================================
=========\n");
scanf("%d",& choice);
switch(choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
void insert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
return;
else
printf("\nEnter value?\n");
scanf("%d",&item);
if(front == NULL)
{
front = ptr;
rear = ptr;
else
rear = ptr;
rear->next = NULL;
void delete ()
if(front == NULL)
printf("\nUNDERFLOW\n");
return;
else
{
ptr = front;
free(ptr);
void display()
ptr = front;
if(front == NULL)
printf("\nEmpty queue\n");
else
while(ptr != NULL)
OUTPUT:
*************************Main Menu*****************************
=========================================================
========
1.insert an element
2.Delete an element
4.Exit
Enter value?
*************************Main Menu*****************************
=========================================================
========
1.insert an element
2.Delete an element
4.Exit
Enter your choice ?1
Enter value?
*************************Main Menu*****************************
=========================================================
========
1.insert an element
2.Delete an element
4.Exit
8
*************************Main Menu*****************************
=========================================================
========
1.insert an element
2.Delete an element
4.Exit
*************************Main Menu*****************************
=========================================================
========
1.insert an element
2.Delete an element
4.Exit
*************************Main Menu*****************************
=========================================================
========
1.insert an element
2.Delete an element
4.Exit