0% found this document useful (0 votes)
2 views

DS-Unit-4-Queue

A queue is a non-primitive linear data structure that follows the first-in-first-out (FIFO) principle, where elements are added at the rear and removed from the front. It can be implemented using arrays with pointers to track the front and rear, and basic operations include insertion and deletion, which require checking for overflow and underflow conditions. Circular queues address limitations of linear queues by allowing reuse of empty spaces, enabling more efficient storage management.

Uploaded by

Mayur Gurav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DS-Unit-4-Queue

A queue is a non-primitive linear data structure that follows the first-in-first-out (FIFO) principle, where elements are added at the rear and removed from the front. It can be implemented using arrays with pointers to track the front and rear, and basic operations include insertion and deletion, which require checking for overflow and underflow conditions. Circular queues address limitations of linear queues by allowing reuse of empty spaces, enabling more efficient storage management.

Uploaded by

Mayur Gurav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

fi

fi

fi
on

on

on
C

C
Unit 4
Queues
l

l
ia

tia

tia

tia
nt

en

en

en
de

fid

fid

fid
Definition and Concept :
Queue is a non-primitive linear data structure, It is an ordered list of elements in which addition
on

on

on
of new element can take place from one end called rear, and deletion of existing element is done from
C

C
another end, called as front. This means that elements are removed from a queue in the same order of
that in which they were inserted into queue. As the element which inserted first in queue is deleted first
from queue so queues are also called first in first out (FIFO) lists or first come first served(FCFS).
l

l
tia

tia

tia

tia
n

en

en

en
A
Queue with its pointers
de

fid

fid

fid
Ilem 1 tem 2
tem 3 hem 4
Flo
on

on

on
REA
C

C
Frort pointer Rear palnter

Queues may be represented by a linear array with two pointer variables. Front is the pointer
l

l
containing location of front element of queue and Rear containing rear element of queue. Whenever a
ia

ia

ia

ia
nt

nt

nt

nt
new element is added to queue rear pointer is increased by one and when existing element deleted from
de

queue, front pointer is increased by one.


e
id

id

id
of a queue is the queue
f

f
A good real-life example of people waiting at railway station to get
on

on

on

the ticket. Each new person who will come joins at the end of queue. The person which
C

come first get the ticket first.


Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the
booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out
l

l
ia

ia

ia

ia

(FIFO) strategy of queue.


nt

nt

nt

nt

Other examples
de

:
id

id

id

Operating systems often maintain a queue of processes that are ready to execute or that are
f

f
on

on

on

waiting for a particular process to occur.


C

When placed on hold for telephone operators. For example, when you phone the toll-free
number for your bank, you may get a recording that says, "Thank you for calling A-1 Bank.

CS- 231 : Data Structure


l

59
tia

tia

tia

tia
en

en

en

en
fi

fi

fi
on

on

on
operator. Please wait." This is a queuing

C
next available
answered by the
Your call will be
system
:
Queue
Static representation of dimensional array is
l

l
arrays. One used
ia

tia

tia

tia
queue can be obtained using
lo
of
Static representation REAR contain integer vale+
nt

two pointers FRONT and


en

en

en
of queue is N and remov
represent queue. Suppose size
de

REAR is incremented and when data is


time data is added
fid

fid

fid
represent two ends of queue. Each REAR=N
FRONT=0 will indicate that the queue is empty and
on

on

on
FRONT is incremented. Condition to 0.
Empty queue is indicated by setting FRONT and REAR value
represent that queue is full.
C

C
a an array is declared, its size cannot be modified durin
Array has few limitations. Once size of
executing program. If we store less number of elements than declared
size t
runtime or at the time of
we to store more elements than declared size then array cannos
array then memory is wasted and if want
l

l
tia

tia

tia

tia
suitable when prior we know exact number of elements to be stored.
be expanded. Array is only
n

en

en

en
declared as array variable by following statement
de

Queue canbe of
fid

fid

fid
int queue[max_queue_size); char stack[max_queue_size]:
on

on

on
max queue size is maximum number of elements inserted into
queue.
C

C
Basic operations which are performed on queue are :

(a) Insert: (This is the process of addition of new element onto the queue called as insertion
operation) When an element is added it will add to the rear end of queuel At that time rear
l

l
ia

ia

ia

ia
value is increased by one and then the element is inserted. Before inserting the element one
nt

nt

nt

nt
must test whether there is room in the queue for the new item{1f there is no space to store
de

e
id

id

id
element in queue means queue is full or overlow
f

f
on

on

on

(b) Delete: This is the process of deletion of an existing element from the front end of queue
C

called as delete operation When as element is deleted from front end


of queue at that time
element is deleted and then front value is increased by one.
Before deleting the element one
must test whether there is any element in
the queue or not. If there is no element in queue
l

means queue
ia

ia

ia

ia

is underflow or empty)
nt

nt

nt

nt

Algorithms on different operation performed on queue:


de

Algorithm to insert an element in queue - In first step


id

id

id

overflow condition is checked. If queue


f

becomes overflow then insertion cannot


on

on

on

be performed and appropriated error message


is return.
Procedure QINSERT (Q,N,F,R,ITEM).
C

This procedure inserts an ITEM onto rear


position of queue. Queue is represented
by vector
containing N elements. F and R are the
pointers pointing to the front and rear elements
of Q. Initiall
l

l
tia

tia

tia

tia

s0 Prashant Publications
en

en

en

en
fi

fi

fi
on

on

on
F&R are set to Zero,

C
1. | Is queue already full?]
if R>= N
Then write("Queue Overflow")
l

l
ia

tia

tia

tia
Return
nt

en

en

en
2. [Increase rear by I]
de

fid

fid

fid
RER+ |

| Insert ITEM at rear position]


on

on

on
3

QR] ITEM
C

C
4 [Is front pointer property 1]

IfF =0
l

l
F I
Then
tia

tia

tia

tia
Return
n

en

en

en
de

agorithm to delete an element from queue - Initially underflow condition is checked. If queue
fid

fid

fid
becomes empty then deletion
of element cannot be performed and appropriated error message is return.
on

on

on
Procedure QDELETE(Q,F,R).
C

C
This procedure removes an ITEM from front pointer of queue Q and returns the element. F&R are
the pointers pointing to front and rear position of queue resp.

[ Is queue empty?]
l

l
ia

ia

ia

ia
If F =0
nt

nt

nt

nt
de

Then write("Queue Underflow")


e
id

id

id
Return
f

f
on

on

on

2. (Assign front element to ITEM]


C

ITEM QF]
3 [Increase F by 1]
l

l
ia

ia

ia

ia

4. [Finished]
nt

nt

nt

nt

Return
de

e
id

id

id
f

f
on

on

on

Circular Queue:
rear end and deletion take place from front end. When
linear queue, insertion take place from
C

In
space is created there and even if the queue has
Some elements are deleted from queue then empty

CS- 231 : Data Structure 61


l

l
tia

tia

tia

tia
en

en

en

en
fi

fi

fi
on

on

on
This
storage.

C
further
Means space is not utilize for
empty cells we cannot insert any new element there. space at front end. then
rear full
is but there is
problem is solved in case of circular queue. Even if the overflows. In circular
queue after
queue
the element can be inserted in the beginning nodes until
l

l
make circle.
ia

tia

tia

tia
first element to
pointing the last element of queue, rear points to the
nt

en

en

en
de

fid

fid

fid
FRONT AR
on

on

on
Circalar Qugag ing Linkd
C

C
RE AR
A2) FRONT
l

l
tia

tia

tia

tia
A[2]
n

en

en

en
operation on circular queue
:
de

Algorithms to perform different condition is checked


fid

fid
-

fid
circular queue In fist step overflow
Algorithm to insert an element in message is retum.
and appropriated eror
on

on

on
cannot be performed
queue becomes overflow then insertion
C

C
Procedure CQINSERT(Q,N,F,R,ITEM). by vector
an ITEM onto rear position of queue. Queue is represented
This procedure inserts
to the front and rear elements
of Q. Initia
F and R are the pointers pointing
containing N elements.
l

l
ia

ia

ia

ia
F&R are set to zero.
nt

nt

nt

nt
[Is queue already ful1?]
de

e
id

id

id
If F=l and R=N or if F=R+1
f

f
on

on

on

Then write("Queue Overflow'")


C

Return
2 [Find new value of R]

If F=0
l

l
ia

ia

ia

ia

Then F=l and R=1


nt

nt

nt

nt

Else If R=N
de

Then R¢I
id

id

id
f

Else RER+1
on

on

on

3. [Insert ITEM at rear position]


C

Q(R]
€ ITEM
4. Return
l

l
tia

tia

tia

tia

62 Prashant Pubications
en

en

en

en
fi

fi

fi
on

on

on
LAHgorithm to delete
an element
from circular queue -

C
If circular queue becomes Initially underflow condition is checked.
empty then
deletion of element
message is return. cannot be performed and
appropriated error
Procedure CQDELETE
(Q,F,R).
l

l
ia

tia

tia

tia
This procedure removes an
ITEMfrom front
nt

en

en

en
the pointers pointing pointer of queue Q
and returns the element. F&R are
to front and rear position
de

of queue resp.
fid

fid

fid
1. [Is queue empty?]
on

on

on
If F=0
C

C
Then write ("Queue Underflow")
Return
2. (Assign front element to ITEM
l

l
tia

tia

tia

tia
ITEM Q[F]
n

en

en

en
[Find new value
de

3. of front]
fid

fid

fid
If F
=R
on

on

on
Then F -0 and R=0
C

C
Else IfF = N

Then
FI
Else
l

l
ia

ia

ia

ia
+ |
FEF
nt

nt

nt

nt
de

4. [Finished]
e
id

id

id
f

f
Return
on

on

on

1
C

Deque: elements will be of


which insertions and deletions
Deque is a special
type of data structure in
queue called deque or double ended queue. This
or at the rear end of the
done either at the front end
l

other end.
one end and remove from the
ia

ia

ia

ia

elements can added to


queue where
differs from linear
nt

nt

nt

nt

representation of deque.
de

Following figure shows the


id

id

id
f

rear
on

on

on

|front
insertion
C

deletion
deleti on

insertion
l

CS- 231 : Data Structure 63


tia

tia

tia

tia
en

en

en

en
fi

fi

fi
on

on

on
C

C
are
Basic operations that can be performed on deques
:

a) nsert an item from front end


l

l
ia

tia

tia

tia
an item from rear end
b)Jnsert
nt

en

en

en
Delete an item from front end
de

c)
fid

fid

fid
d) Delete an item from rear end in
deque, one
list. There are two variations
array or link
on

on

on
The DeQue can be constructed by using restricted deque allows
one is output restricted deque. Input
and the second
C

C
is input restricted deque at both ends. Output restricted deque
of array or list but deletions allowed
insertions at only one end
one the array or list but insertions allowed at both ends.
end of
allows deletions at only
l

l
Prlority queue
tia

tia

tia

tia
:

element is assigned a priority and the order ín which


queue in which each
(A special type of
n

en

en

en
de

comes fromthe follow rules


elements are(deleted and processed
fid

fid

fid
first before any element of lower priority.
priority is processed
An element wth highest
on

on

on
a) which they
same priority are processed according to the order in
b) Two or more elements with
C

C
were added to the queue.
queye.
Such type of queue is called priority
queues Priority Queue 2.Desceding priorityqueue.
priority
:
1.Ascending
There are two types of
l

l
ia

ia

ia

ia
and
queue is a collection of items into which items can be inserted arbitrarily
Ascending priority
nt

nt

nt

nt
can be removed first.
from which only the smallest item
de

e
id

id

id
In Descending priority queue allows deletion of only
the largest item.
f

f
on

on

on

queue.
Stack may be viewed as descending priority
queue.
C

Queue may be viewed aslascending priority


Examples of priority queues are
:

a)
In CPU.scheduling shortest job is given the highest-prierity toprocess over the longer jobs.
l

important job is given the highest priority over


ia

ia

ia

ia

a
b)
An
routine type job.
nt

nt

nt

nt
de

e
id

id

id
f

f
on

on

on
C

C
l

l
tia

tia

tia

tia
en

en

en

en
fi

fi

fi
Following pictures

on

on

on
are two ways to
None fixed

C
1.
size of the queues: dotwoqueucksn array
Grap
hicalPicture:
wthout fuxed
size ofthe queue
l

l
ia

tia

tia

tia
Aray_tr
nt

en

en

en
de

-6 5
fid

fid

fid
4 3 -2
on

on

on
5 2 3 4
7
S
C

C
Count Front
Count
Size Front
Size
l

l
tia

tia

tia

tia
2 2 2
1
Queue
ue ue 1 1 Queue 1

Queue ue ue
n

en

en

en
Que Que
Que
Que
de

Temporary
fid

fid

fid
Boundary
on

on

on
C

C
a) Queue I
expands from the 0" element to the right and circular
back to the element O

Queue 2 expands from the 8" element to the left and circular back to
the 8 element
C) Temporary boundary between the Queue I and the Queue 2; as long as there has
l

l
free
ia

ia

ia

ia
elements in the array and boundary would be shif.
nt

nt

nt

nt
Free elements could be anywhere in the Queue such as before the front, after the rear, and
de

e
id

id

id
between front and rear in the Queue.
f

f
on

on

on

) Queue 1's and Queue 2's size could be change if it is necessary. When the Queue I is full
C

use that free space


and the Queue has free space; the Queue l can increase the size to
2

way for the Queue 2.


from the Queue 2. Same
Queue 1. the front the Queue 1. of

Elements -I, -2, and -3 are using to store the SIze the
of
)
l

l
ia

ia

ia

ia

I.
needed to manipulate the Queue
and the data count for the Queue
I
nt

nt

nt

nt

size of the Queue 2, the front


of
the Oueue )
Elements-4,-5, and -6 are using to store the
de

8) manipulate the Queue 2


id

id

id

the Queue 2 needed to


and the data count for
f

f
on

on

on
C

65
l

Datu Seracture
:

CS- 23l
tia

tia

tia

tia
en

en

en

en
fi

fi

fi
on

on

on
2. Fixed size of the queue:

C
Graphical Plc ture: with ixed
aze ofthe queue
Army_ ptr
l

l
ia

tia

tia

tia
1 2 3 4 5
nt

4 -3 -2 1 0

en

en

en
de

4 0
fid

fid

fid
on

on

on
C

C
Sie Queue 2
Queue 1

1
e

Que Boundarybetwe en
the Que ues
l

l
tia

tia

tia

tia
n

en

en

en
element to the 4h element and
circular back to 0" element
de

0
Queue 1 expands from the
fid

fid

fid
a)
back to 8" element
Queue 2 expands from the 8" element to the s" element and circular
on

on

on
b)
I andthe Queue 2
c) The boundary is fixed between the Queue
C

C
in the Queue such as before the front, after the rear, and
d) Free elements could be anywhere
between front and rear in the Queue
of the Queue I.
Elements -1, -2, and -3 are using to store the size of the Queue I, the front
l

l
e)
ia

ia

ia

ia
I
and the data count for the Queue I needed to manipulate the Queue
nt

nt

nt

nt
Elements -4, -5, and -6 are using to store the size of the Queue 2, the front
de

e of the Queue 2.
id

id

id
and the data count for the Queue 2 needed to manipulate the Queue 2
f

f
on

on

on
C

tpplications of queues:
Used in time sharing systemin which programs
() with the same priority form a queue while
waiting to be executed.
l

l
ia

ia

ia

ia

(i) Used for performing level order traversal of a binary tree and for performing breadth firs
nt

nt

nt

nt

search at a graph.
de

e
id

id

id

(ii) Used in simulation related problem.


f

f
on

on

on

When jobs are


submitted to a networked
(ii) printer, they are
arranged in order of arrival.
C

Jobs are placed in a queue.


(iv) Checking string of language
l

l
tia

tia

tia

tia

66 Prashant Publications
en

en

en

en
fi

fi

fi
on

on

on
C

C
(v) Simulation (Queuing Theory)
l

l
S
ia

tia

tia

tia
(vi) Input output buffers.
nt

en

en

en
(vi) Graph Searching.
de

fid

fid

fid
on

on

on
Questions:
C

C
1. How is the queue different from stack?

2. What are the limitations of a queue?


What is circular queue? Explain with suitable example.
l

l
tia

tia

tia

tia
3.
memory of computer?
What is queue? How it is implemented in
n

en

en

en
4.
de

Differentiate queue and circular queue.


fid

fid

fid
5.
on

on

on
6. What is priority queue?
on circular queue.
C

C
insertion and deletion operations
7. Write an algoritham for
queue over linear queue?
8. Whar are the advantages of circular
l

l
ia

ia

ia

ia
9. Define Queue.
nt

nt

nt

nt
Give real world example of Queue.
de

10.
e
id

id

id
List the operations on Queue.
f

f
11.
on

on

on

12. List the application on Queue.


C

on Queue.
13. Define Insertion operation
14. Define Deletion operation on Queue.
l

l
ia

ia

ia

ia
nt

nt

nt

nt

15. Define Circular Queue.


de

List out limitation queue.


id

id

id

l6. of linear
circular queue.
f

f
on

on

on

17. Write algorithm to insert element into


C

18. What is Deques? Explain with example.


19.
Define priority queue.
l

l
tia

tia

tia

tia
en

en

en

en

You might also like