Implementation of A Queue Using Linked Representation: Program
Implementation of A Queue Using Linked Representation: Program
Introduction
Initially, the list is empty, so both the front and rear pointers are NULL. The insert function creates a new
node, puts the new data value in it, appends it to an existing list, and makes the rear pointer point to it. A
delete function checks whether the queue is empty, and if not, retrieves the data value of the node pointed
to by the front, advances the front, and frees the storage of the node whose data value has been retrieved.
If the above strategy is used for creating a queue with four data values —10, 20, 30, and 40, the queue gets
created as shown in Figure 19.5.
Program
A complete C program for implementation of a stack using the linked list is shown here:
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *link;
};
mk:@MSITStore:D:\movie\C%20Programming%20Language%20Books\C-AndDataStruct... 6/8/2011
IMPLEMENTATION OF A QUEUE USING LINKED REPRESENTATION Page 2 of 4
{
printf("No Memory available Error\n");
exit(0);
}
temp->data = value;
temp->link=NULL;
if(*rear == NULL)
{
*rear = temp;
*front = *rear;
}
else
{
(*rear)->link = temp;
*rear = temp;
}
}
void main()
{
struct node *front=NULL,*rear = NULL;
int n,value;
do
{
do
{
printf("Enter the element to be inserted\n");
scanf("%d",&value);
insert(&front,&rear,value);
printf("Enter 1 to continue\n");
scanf("%d",&n);
} while(n == 1);
mk:@MSITStore:D:\movie\C%20Programming%20Language%20Books\C-AndDataStruct... 6/8/2011
IMPLEMENTATION OF A QUEUE USING LINKED REPRESENTATION Page 3 of 4
} while(n == 1);
}
Example
Input and Output
Enter 1 to continue
1
Enter 1 to continue
1
Enter 1 to continue
0
Enter 1 to continue
1
Enter 1 to continue
1
Enter 1 to continue
0
mk:@MSITStore:D:\movie\C%20Programming%20Language%20Books\C-AndDataStruct... 6/8/2011
IMPLEMENTATION OF A QUEUE USING LINKED REPRESENTATION Page 4 of 4
mk:@MSITStore:D:\movie\C%20Programming%20Language%20Books\C-AndDataStruct... 6/8/2011