ds lec 4 queue
TRANSCRIPT
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 1/26
Linear Data Structures
QueueLecture # 4
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 2/26
Problem to be SolvedIt is so often necessary to wait one’s turn before having access to something.We may want to simulate a real life situation of a waiting line, like
A line of eo le waiting to urchase tickets, where the first erson in line is thefirst erson serve!.
With in a com uter system there may be lines of tasks
Waiting for the rinter
Waiting for access to !isk storage
"r in a time sharing system for use of the $%.&he !ata structures use! to solve this ty e of roblems is calle!Queue
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 3/26
The QueueA 'ueue is an or!ere! collection of items from which items maybe !elete! at one en! (front of the queue) an! into which itemsmay be inserte! at the other en! (rear of the queue).
Insertion(enqueue) occurs at the rear location of a 'ueue.(eletion (dequeue) occurs at the front location of a 'ueue.Queues are first)in)first)out(FIFO)or first)come)first serve!(FCFS) lists.&he general 'ueue mo!el is that elements are inserte! at oneen! an! !elete! from other en!.
Queue QDequeue () Enqueue (x)
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 4/26
The Queue
Assume an initially em ty 'ueue an! followingse'uence of insertions an! !eletions*
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 5/26
The Queue
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 6/26
Implementation of QueueThe Physical Model:+ Im lement a 'ueue by using an array.+ We must kee track of both the front an! the rear of the 'ueue.+ "ne metho! woul! be to kee the front of the 'ueue always in the first
location of the array.+ An entry coul! be a en!e! to the 'ueue sim ly by increasing the counter
showing the rear.10 12 7
(elete an entry ointer by the front, but after the fist entry was serve!,all the remainingentries woul! nee! to be move! one osition u the 'ueue to fill in the vacancy, which woul! be an e ensive rocess.
12 7
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 7/26
Implementation of QueueLinear I !le entation+ In this im lementation an array with two in!ices will be use!.+ "ne in!e in!icates front of 'ueue an! secon! in!e in!icates rear of
'ueue.+ %sing front an! rear in!ices there is no nee! of moving any entries.+ &o en'ueue an entry to the 'ueue, we sim ly increase the rear by one an!
ut the entry in that osition
12 7
&o !e'ueue an entry, we take it from the osition at the front an! then increase the front by one.
7 8
12 7 8
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 8/26
Queue Operations
-our common o erations which are associate! with a 'ueue are*
initiali e* Initiali es the 'ueue to be em ty.en'ueue* Inserts an element at the rear of the'ueue.!e'ueue* /emoves the front element from the'ueue an! returns its value.is0m ty* /eturns 1true2 if 'ueue contains noelements3 otherwise returns 1false2.
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 9/26
QUEUE (queue h!#include<iostream.h> template<class Type> class Queue{
private:
Type *data; 5 &his ointer oints to the array which behaves as 'ueue, the s ace for this array isallocate! !ynamically 5int front; 5 &his variable is use! to in!icate front of the 'ueue 5int rear; 5 &his variable is use! to in!icate rear of the 'ueue5const int size; 5 &his variable is use! to in!icate 'ueue si e5
public:
Queue int!; 5&his constructor creates a 'ueue5"Queue !; 5&his !estructor frees the !ynamically allocate! s ace 5void en ueue Type!; 5 inserts an element, if 'ueue is not full 5
Type de ueue !; 5 !eletes an element, if 'ueue is not em ty5 bool is$mpty !; 5 returns &/%0 if the 'ueue is em ty5
%; 5en! of class5
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 10/26
QUEUE (queue h!5&his constructor creates a 'ueue5
te !late"class Ty!e#Queue"Ty!e#::Queue(int qsi$e):front(%&)'rear(%&)'si$e(qsi$e)
data ne* Ty!e+si$e,-
5&his !estructor frees the !ynamically allocate! s ace 5te !late"class Ty!e#
Queue"Ty!e#::/Queue()
delete+, data-
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 11/26
QUEUE (queue h!01 inserts an ele ent' if queue is not full 10te !late"class Ty!e#2oid Queue"Ty!e#::enqueue(Ty!e ite )
if(rear si$e%&)cout""3Queue Full3""endl-
else
if(front %&)front 4-data+55rear, ite -
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 12/26
QUEUE (queue h!5 !eletes an element, if 'ueue is not em ty5
te !late"class Ty!e#Ty!e Queue"Ty!e#::dequeue()
Ty!e te !-if(front %&)
cout""3Queue 6 !ty3""endl- return 78LL- else
te ! data+front,-if (front rear)
front %&- rear %&-elsefront55-
return te !-
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 13/26
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 14/26
QUEUE (driver cpp!;include"iostrea .h#;include3queue.h32oid ain()
int s-
cout""36nter the queue si$e: 3-cin##s-Queue"int# qint(s)-qint.enqueue(<)-qint.enqueue(=)-qint.enqueue(>)-
cout""qint.dequeue()""endl-cout""qint.dequeue()""endl-cout""qint.dequeue()""endl-
OutputEnter the queue size: 3
5
8
2
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 15/26
Implementations of QueuesCircular ?rrays+ &hinking of the array as a circle rather than a straight line.+ As entries are a!!e! an! remove! from the 'ueue, the hea!
will continually chase the tail aroun! the array.+ &o im lement a circular array as an or!inary array, we think of
the ositions aroun! the circle as numbere! from 6 tosize)7, where size is the total number of entries in the array.
+ @hen *e increase an indeA !astsize
%&' *e start o2eraBain at 4.
7 8 9 12 7 8 9
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 16/26
Implementations of Queues
we can increase an in!e rear by 7 in a circulararray by writingif8rear 9: si e)7;
rear : 63else
rear<<3
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 17/26
"ne ossible 'ueue im lementation is an array.0lements are a!!e! to the en! of the 'ueue, but&hey may be remove! from its beginning, thereby releasing array
cells.&hese cells shoul! not be waste!. &herefore,&hey are utili e! to en'ueue new elements,Whereby the en! of the 'ueue may occur at the beginning of the
array.&his situation is icture! in following figure
84
2
151110
6
l st !irst
"he queue is !ull i! the !irst ele#enti##e$i tel% pre&e$es in the
&'unter&l'& ise $ire&ti'n the l st ele#ent*
Implementations of Queues
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 18/26
=owever, because a circular array is im lemente! with a 1normal2array,
&hequeue is full if either theFirst ele ent is in the first cell and the last ele ent is in the last cell .
4 2 15 11 10 6 8
!irst l st
> or if the first ele ent is riBht after the last
10 6 8 4 2 15 11
l st !irst
Implementations of Queues
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 19/26
?imilarly,enqueue() an! dequeue() have to consi!er the ossibilityof wra ing aroun! the array when a!!ing or removing elements.0.g. enqueue() can be viewe! as o erating on a circular array, butin reality, it is o erating on a one !imensional array. &herefore,If the last element is in the last cell an! if any cells are available atthe beginning of the array,a new element is lace! there.
2 4 8
!irst l stenqueue(6)
6 2 4 8
!irstl st
Implementations of Queues
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 20/26
If the last element is in any other osition, then the new element isut after the last, s ace ermitting.
2 4 8
!irst l stenqueue(6)
2 4 8 6
!irst l st
&hese two situations must be !istinguishe! when im lementing a'ueue viewe! as a circular array.
2
4
8l st
!irst2
4
8 6
l st
!irst
enqueue(6)
Implementations of Queues
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 21/26
"I#"U $# QUEUE (queue h!#include<iostream.h> template<class Type> class &ircularQueue{
private:
Type *data; 5 &his ointer oints to the array which behaves as 'ueue, the s ace for this array is allocate!!ynamically 5
int front; 5 &his variable is use! to in!icate front of the 'ueue 5int rear; 5 &his variable is use! to in!icate rear of the 'ueue5const int size; 5 &his variable is use! to in!icate 'ueue si e5
public:
&ircularQueue unsi'ned int!; 5&his constructor creates a 'ueue5"&ircularQueue !; 5&his !estructor frees the !ynamically allocate! s ace 5void en ueue Type!; 5 inserts an element, if 'ueue is not full 5Type de ueue !; 5 !eletes an element, if 'ueue is not em ty5
bool is$mpty !; 5 returns &/%0 if the 'ueue is em ty5%; 5en! of class5
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 22/26
"I#"U $# QUEUE (queue h!5&his constructor creates a 'ueue5
te !late"class Ty!e#CircularQueue"Ty!e#:: CircularQueue(unsiBned int qsi$e):front(%&)'rear(%&)
si$e qsi$e-data ne* Ty!e+si$e,-
5&his !estructor frees the !ynamically allocate! s ace 5te !late"class Ty!e#CircularQueue "Ty!e#::/ CircularQueue()
delete+, data-
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 23/26
01 inserts an ele ent' if queue is not full 10te !late"class Ty!e#2oid CircularQueue"Ty!e#::enqueue(Ty!e ite )
if((rear si$e%& front 4)DD(rear5& front))cout""3Queue Full3""endl-
else
if(front %&)front 4-
if(rear si$e%&)rear 4-
elserear55-
data+rear, ite -
"I#"U $# QUEUE (queue h!
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 24/26
01 deletes an ele ent' if queue is not e !ty10te !late"class Ty!e#Ty!e CircularQueue"Ty!e#::dequeue()
Ty!e te !-
if(front %&)cout""3Queue 6 !ty3""endl- return 78LL-
elsete ! data+front,-
if (front rear)
front rear %&-elseif(front si$e%&)
front 4- elsefront55-
return te !-
"I#"U $# QUEUE (queue h!
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 25/26
01 returns T986 if the queue is e !ty10te !late"class Ty!e#
ool CircularQueue"Ty!e#::is6 !ty()
if(rear %&)return true-
else
return false-
"I#"U $# QUEUE (queue h!
8/20/2019 DS Lec 4 Queue
http://slidepdf.com/reader/full/ds-lec-4-queue 26/26
"I#"U $# QUEUE (driver cpp!;include"iostrea .h#;include3queue.h32oid ain()
int s-cout""36nter the queue si$e: 3-
cin##s-CircularQueue"int# qint(s)-qint.enqueue(<)-qint.enqueue(=)-qint.enqueue(>)-cout""qint.dequeue()""endl-
qint.enqueue(E)-cout""qint.dequeue()""endl-cout""qint.dequeue()""endl-cout""qint.dequeue()""endl-
Output
Enter the queue size: 3
5
8
2
4