You are on page 1of 11

Chapter 4- Queue

Queue 4

4.1 What is Queue?


4.2 Operations on Queue
4.3 Applications of Queue
4.4 Array based implementation of Queue
4.5 Linked list implementation of Queue
4.6 Circular Queue Implementation
4.7 Summary
4.8 Key Terms
4.9 Review Questions

49
Chapter 4- Queue

Objectives
To understand the concept of queue
To learn the array and linked list implementation of queue
To understand the applications of queue

4.1 What is Queue?

A queue is a linear list of elements in which deletions can take place only at one end,
called the " front" and insertion can take place only at the other end, called " rear ". The term
"front " and " rear " are used in describing a linear list only when it is implemented as a queue.
Queues are also called " first-in first-out " (FIFO) list.
In other words, the order in which elements enter in a queue is the order in which they
leave. The real life example: the people waiting in a line at Railway ticket Counter form a queue,
where the first person in a line is the first person to be waited on. An important example of a
queue in computer science occurs in timesharing system, in which programs with the same
priority form a queue while waiting to be executed.

QUEUE

AA BB CC DD
FRONT REAR

Figure 4.1 – Structure of Queue

The structure of a queue can represented as:


struct queue
{
int data;
struct queue *next;
};
typedef struct queue node;
node *front, *rear ;

50
Chapter 4- Queue

4.2Operations on Queue

Enqueue - Inserts an element at the end of the list (called the rear)
Dequeue – Deletes the element at the start of the list (called the front)

Queue
Front Rear

Deletion Insertion
Figure 4.2 – Operations of Queue

4.3Applications of Queue

1. Jobs sent to a line pointer on a queue.


2. Lines at ticket counters.

4.4Array based implementation of Queue

Queue is implemented using an array. It can insert elements upto the size of an array. All the
insertion(enqueue) and deletion(dequeue) operation can be performed in FIFO (First In First
Out) manner. Here there are two poitera are used. Rear is ued for inserion and front pointer is use
for deletion.

(i) enqueue()
Thi s procedure is used to insert an element into the queue using an array. Before inserting any
element first check whether the queue is full or not. If it is full, then display an error. Otherwise
identify the successor rear position using succ() function and insert an element into the rear
position of queue.
Algorithm
1. if rear >= Queuesize – 1 then
Write “ Queue overflow”
Exit
Else
rear = rear + 1
Q [rear] = item
2. stop

51
Chapter 4- Queue

(ii) Dequeue ()
This is used to delete an element from the queue. This is done by identify the successor of
front. Then the first inserted element is deleted first.

Algorithm

1. if front = - 1 then
Write “ Queue underflow”
Exit
2. item = Q [ front]
3. if (front = = rear) then
front=rear=0
else
front=front+1
4. stop

4.5 Linked list implementation of Queue

This is one of the implementation of queue in which the elements can be inserted in linked list
based on FIFO (First in First Out) manner. Any number of elements can be inserted into the
queue, since it can be implemented in linked list.

(i) Enqueue ()
This function is used to insert an element into the queue using linked list. If the linked list is
empty, then insert an element into the linked list after the header node. If the linked list is not
empty, then insert an element to the end of the linked list which means than the elements can be
inserted at the end of the queue.

Algorithm
void enqueue( element_type x, Queue Q )
{
node_ptr newnode, rear;
newnode = (node_ptr) malloc( sizeof ( struct node ) );
newnode->element = x;
rear->next =newnode;
newnode->next=NULL;
}

52
Chapter 4- Queue

Figure 4.3 –Enqueue operation

(ii) Dequeue
This is used to delete an element from the queue. This is done by identify the successor of front.
Then the first inserted element is deleted first.

Figure 4.4 –Dequeue operation

Algorithm

void dequeue( Queue Q )


{
node_ptr front;
if(front==NULL)
print(‘queue is empty’);

53
Chapter 4- Queue

else
{
temp=front;
front=front->next;
free(temp);
}
}

4.5.1 C Program for linked list implementation of queue

/* C Program for linked list implementation of queue*/


# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# define NULL 0
struct queue{
int data;
struct queue *next;
};
typedef struct queue node;
node *front = NULL, *rear = NULL;
void main()
{
int choice;
void insert();
voiddelet();
void display();
clrscr();
while(1)
{
printf(“\t\t\t Queue \n\n\n”);
printf(“\t 1. Insert \n”);
printf(“\t 2. Deletion \n”);
printf(“\t 3. Display \n”);
printf(“\t 4. Exit \n”);
printf(“\t Enter Your Choice:\n”);

54
Chapter 4- Queue

scanf(“%d”,&choice);
switch (choice)
{
case 1 : insert();
break;
case 2 : delet();
break;
case 3 : display();
break;
case 4: exit(0);
}/* switch end*/
} /*while end*/
}/*main end*/
// creation of Queue- insertion
void insert()
{
node *newnode, *temp;
newnode = (node *)malloc(sizeof(node));
printf(“Enter the data field :”);
scanf(“%d”,&newnode->data);
newnode->next = NULL;
if(front == NULL)
{
front= newnode;
rear = newnode;
}
else
{
rear->next =newnode;
rear = rear->next;
}
return;
}
/* Function to delete an element */
void delet()
{
node *temp;
if(front == NULL)
printf(“Queue is empty”);

55
Chapter 4- Queue

else
{
temp = front;
printf(“deleted data :%d”, temp->data);
front = front ->next;
temp->next = NULL;
free(temp);
} / *else end*/
return;
}
/* Function to display the elements */
void display()
{
node *temp;
temp =front;
while(temp != NULL)
{
printf(“%d---------- >”, temp->data);
temp = temp->next;
}
return;
}

4.6Circular Queue Implementation

4.6.1 Limitations of Linear Array Based Queues


For example, consider the following empty queue

0 1 2 3

Empty Queue

F R After inserting the element A, front=rear = 0

56
Chapter 4- Queue

A B C D

F R

After inserting the elements A, B, C and D, Front =0 and Rear = 3

B C D

F R

The value of front is increased due to deletion of one element. Now front =1 and rear = 3.
If more elements are deleted, the value of front will be increased and as a result, the
beginning portion of queue will be empty.

In such a situation if we try to insert new elements to queue, no elements can be inserted
in the queue. This is because, new elements are always inserted from the rear and if the rear is
equal to the size of the queue, even though the space is available at the front portion of the
queue, no elements will be inserted. This problem is known as memory underutilization
problem.
To solve this problem, circular queue concept is used.
In the circular queue, the first element is stored immediately after last element. If the last
location (size of the queue) is occupied, the new element is inserted at the first memory location
of array based queue.
A circular queue overcomes the limitation of array based queue by utilizing the entire
space of the queue.
In circular queue, the following steps are followed for initialization, insertion and
deletion:
1. The front end of the queue always points to the first element of the queue.
2. If front = rear then queue is empty.
3. The values of front and rear are increased / decreased after insertion / deletion operation.

57
Chapter 4- Queue

Figure 4.5 – Circular Queue Implementation

4.7 Summary

Queue is a linear data structure. FIFO list. The insertion operation is done at rear end of
the queue. The deletion operation is done at the front of the queue. It can be implemented using
arrays and linked list. Queue is having memory utilization problem. To avoid this, we use
circular queue.

4.8 Key Terms

Queue, Enqueue, Dequeue, Circular Queue, Front end, Rear end and Memory
underutilization Problem

4.9 Review Questions

1. What is a queue and how can you implement queue using a linked list?
2. Given below is the subroutine for deletion of a node in a queue.
Delete (struct node *list, struct node *first, struct node *last){
Struct node *temp;
temp=first;
first=first->next;
first->prev=NULL;
free(first);
}

58
Chapter 4- Queue

There is an error in this code. Modify the code so that it deletes exactly the first node of the
queue.
3. Explain the insertion and deletion procedures on Queues implemented using linked list
and arrays.
4. What is the problem of using array based queues? How are they overcome?
Explain with an example.

59

You might also like