0% found this document useful (0 votes)
59 views4 pages

Implementation of A Queue Using Linked Representation: Program

This document discusses the implementation of a queue using a linked list representation. A queue is created by inserting nodes at the rear and deleting nodes from the front. The insert function allocates a new node, stores the data value, and appends it to the rear of the list. The delete function retrieves the data value from the front node, advances the front pointer, and frees the deleted node. A C program example demonstrates inserting values 10, 20, 30, and 40 into the queue and deleting them in FIFO order.

Uploaded by

apslogin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views4 pages

Implementation of A Queue Using Linked Representation: Program

This document discusses the implementation of a queue using a linked list representation. A queue is created by inserting nodes at the rear and deleting nodes from the front. The insert function allocates a new node, stores the data value, and appends it to the rear of the list. The delete function retrieves the data value from the front node, advances the front pointer, and frees the deleted node. A C program example demonstrates inserting values 10, 20, 30, and 40 into the queue and deleting them in FIFO order.

Uploaded by

apslogin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

IMPLEMENTATION OF A QUEUE USING LINKED REPRESENTATION Page 1 of 4

< Day Day Up >

IMPLEMENTATION OF A QUEUE USING LINKED REPRESENTATION

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.

Figure 19.5: Linked queue.

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;
};

void insert(struct node **front, struct node **rear, int value)


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
/* creates new node
using data value
passed as parameter */
if(temp==NULL)

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 delete(struct node **front, struct node **rear, int *value)


{
struct node *temp;
if((*front == *rear) && (*rear == NULL))
{
printf(" The queue is empty cannot delete Error\n");
exit(0);
}
*value = (*front)->data;
temp = *front;
*front = (*front)->link;
if(*rear == temp)
*rear = (*rear)->link;
free(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);

printf("Enter 1 to delete an element\n");


scanf("%d",&n);
while( n == 1)
{
delete(&front,&rear,&value);
printf("The value deleted is %d\n",value);
printf("Enter 1 to delete an element\n");
scanf("%d",&n);
}
printf("Enter 1 to continue\n");
scanf("%d",&n);

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 the element to be inserted


10

Enter 1 to continue
1

Enter the element to be inserted


20

Enter 1 to continue
1

Enter the element to be inserted


30

Enter 1 to continue
0

Enter 1 to delete an element


1

The value deleted is 10


Enter 1 to delete an element
1

The value deleted is 20


Enter 1 to delete an element
0

Enter 1 to continue
1

Enter the element to be inserted


40

Enter 1 to continue
1

Enter the element to be inserted


50

Enter 1 to continue
0

Enter 1 to delete an element


1

The value deleted is 30


Enter 1 to pop an element
1

mk:@MSITStore:D:\movie\C%20Programming%20Language%20Books\C-AndDataStruct... 6/8/2011
IMPLEMENTATION OF A QUEUE USING LINKED REPRESENTATION Page 4 of 4

The value deleted is 40


Enter 1 to delete an element
1

The value deleted is 50

Enter 1 to delete an element


1

The queue is empty, cannot delete Error

< Day Day Up >

mk:@MSITStore:D:\movie\C%20Programming%20Language%20Books\C-AndDataStruct... 6/8/2011

You might also like