Professional Documents
Culture Documents
UNIT II
Queues: Queue abstract data type - Array implementation – circular queue - linked list
implementation of queues – priority queues – double ended queues – multiple stacks and queues -
application
Objective:
To introduce and understand the concept of queue and its operations
To understand the application of queues
Outcome:
Able to apply the queue data structures in programming
Enqueue (Insert)- adds a new element in the queue, this process is carried out by
incrementing the rear end and adding the new element in the rear position.
Dequeue(Delete)- delete an element from the queue, this process is carried out by
incrementing the front end and deleting the element a front end position.
Types of queue
1. Linear Queue
2. Circular Queue
3. Double ended queue
4. Priority Queue
Types of deque
1. Linear deque
2. Circular deque
Linear Deque
It is similar to the linear queue except the following conditions:
If the front pointer is in the first position (zeroth position), we can’t insert data at front end.
If the rear is in the last position (queuesize-1) we can’t insert data at rear end.
Circular Deque
Circular deque is similar to circular queue except the following conditions:
Insertion and deletion are made at front and rear end of the queue.Irrespective of the
position of the front and rear we can insert and delete
Basic operations
1. Enqueue
2. Dequeue
3. Display
Initially the front end and the rear end are at the same position (-1).
The rear pointer is in the last position (size-1) then queue is said to be full, if front pointer is
(-1) then the queue is empty.
When you insert elements, rear pointer moves one by one until the last position is
reached.
When we delete elements, the front pointer moves one by one until the rear pointer is
reached.
2.2.1 Enqueue
Algorithm Enqueue(queue,rear,front,queuesize)
{
if rear=queuesize-1
{ write "queue is in overflow condition. Thus enqueue operation is not possible.";
return;
}
if rear=-1 and front=-1
{ rear=front=0;
}
else
{ rear=rear+1;
}
read rollno;
queue[rear]=rollno;
}
2.2.3 Dequeue
Algorithm Dequeue(queue,rear,front,queuesize)
{
if rear=queuesize-1
{
write "queue is in overflow condition. Thus enqueue opeation is not possble.";
return;
}
if rear=front
{ rear=front=-1;
}
else
{ front=front+1;
}
}
2.2.4 Display
Algorithm display(queue,front,rear)
{
if front=-1&rear=-1&rear
{ write "Queue is in underflow condition. Display operation is not possible.";
return;
}
for i=front to rear
{ write queue[i];
}
return;
}
2.3.2 Dequeue
Algorithm dequeue(struct node *front, struct node *rear)
{
struct node *temp;
if front=NULL and rear=NULL
{
write "Queue is in underflow condition. Dequeue operaation is not possible.";
return;
}
if front=rear
{
front=rear=NULL;
}
else
{
front=front->link;
}
delete temp;
return;
}
2.3.3 Display
Algorithm display(struct node *front, struct node *rear)
{
struct node *temp;
if front=NULL and rear=NULL
{
write "Queue is in underflow condition. Dequeue operaation is not possible.";
return;
}
temp=front;
while(temp!=front)
{
write temp->data;
temp=temp->link;
}
return;
}
Circular Queue
When a new item is inserted at the rear, the pointer to rear moves upwards.
When an item is deleted from the queue the front arrow moves downwards.
After a few insert and delete operations the rear might reach the end of the queue and no
More items can be inserted although the items from the front of the queue have been
deleted and there is space in the queue.
The priority queue elements are arranged in any order and out of which only the smallest or
largest element allowed to be deleted each time.
The implementation of priority can be done by using array and linked list. The data structure
heap is used to implement priority queue efficiently.
Basic Operations
Enqueue
Dequeue
2.5.1 Enqueue
Algorithm enqueue(queue,front,rear,queuesize)
{
if rear=queuesize-1;
{
write "Queue is in overflow condition. Enqueue is not possible.";
return;
}
read rollno;
if front=-1 and rear=-1
{
front=rear=0;
}
else
{
i=rear;
while(i>=0)and (rollno<queue[i])
{
queue[i+1]=queue[i];
i=i-1;
}
queue[i+1]=rollno;
rear=rear+1;
return;
}
}
SMVEC- Department of Information Technology 7
IT T33- Data Structures
2.5.2 Dequeue
Algorithm dequeue(queue,front,rear)
{
if front=-1 and rear=-1
{
write "Queue is in underflow condition. Dequeue operation is not possible.";
return;
}
write "Element to be deleted", queue[front];
if front=rear
{
front=rear=-1;
}
else
{
front=front+1;
}
return;
}
Types of deque
Linear dequeue
Circular dequeue
Linear Deque
It is similar to the linear queue except the following conditions:
If the front pointer is in the first position (zeroth position), we can’t insert data at front end.
If the rear is in the last position (queuesize-1) we can’t insert data at rear end.
Circular Deque
Circular deque is similar to circular queue except the following conditions:
Insertion and deletion are made at front and rear end of the queue.
Irrespective of the position of the front and rear we can insert and delete he data.
T[i] = T[i] – 1;
}
}
Free elements could be anywhere in the Queue such as before the front, after the rear, and
between front and rear in the Queue
Queue 1’s and Queue 2 ‘s size could be change if it is necessary. When the Queue 1 is full and
the Queue 2 has free space; the Queue 1 can increase the size to use that free space from the
Queue 2. Same way for the Queue 2
Serving requests of a single shared resource like (printer, disk, CPU), between two
processes
Job Scheduling
Used in multitasking operating System
Categorizing of data
Simulation and Modeling
11. List out the difference between Linked stack and Linked Queue.
Stack Queue
Assignment questions
1. Efficiently implement a queue class using a circular array. You may use a vector (rather
than a primitive array) as the underlying array structure.
2. Design a data representation sequentially mapping n queues into an array M
[m]. Represent each queue as a circular queue within M. Write functions Add, Delete, and
QueueFull for this representation.
3. Write routines to implement two stacks using only one array. Your stack routines should not
declare an overflow unless every slot in the array is used.
4. Obtain a data representation mapping a stack s and a queue q into a single array M [n].
Write algorithms to add and delete elements from these two data objects. What can you
say about the suitability of your data representation?
5. Write a routine for implementing the priority queue?