l-09 queues.ppt
TRANSCRIPT
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 1/45
Data Structures (FALL-2011)
Queues
“A Queue is a special kind of list, where items
are inserted at one end (the rear) And deleted
at the other end (the front)” Other Name:
First In First Out (FIFO) Difference from Stack:
Insertion go at the end of the list, ratherthan the beginning of the list.
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 2/45
Data Structures (FALL-2011)
2
What is a Queue
• In ordinary English a queue is defined as a
waiting line, like a line of people waiting to
purchase tickets
– Here, the first person in the line is the first person served
• A queueis an ordered collection of items in
which all insertions are made at one end (therear), and all deletions are made at the other end
(the front)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 3/45
Data Structures (FALL-2011)
3
Queues
12 69 50 32 78 88
frontrear
frontrear
A waiting queueComputer Implementation
of queue
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 4/45
Data Structures (FALL-2011)
4
• A queue is a FIFO structure: First In First Out
Queues in Our Life
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 5/45
Data Structures (FALL-2011)
queue examples Cont….:
• In computer implementation, there is anexample of accessing the printer in multiuser environment. If printer is in process
and more than one user wants to accessthe printer then it maintain the queuefor requesting user and serve as first in
first out manner, means gives the accesspermission to the user which comes firstin the queue.
PRINTER QUEUE
PC-4 PC-1 PC-3 PC-5 PC-2
FRONT REAR
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 6/45
Data Structures (FALL-2011)
6
Applications
• Direct Applications
– Access to shared resources
• Within a computer system there may be queues of tasks
waiting for access to disk storage, or even in a time- sharing system, for use of the CPU
• Indirect Applications
– Auxiliary data structure for algorithms – Component of other data structures
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 7/45
Data Structures (FALL-2011)
Applications of Queues
• Communication Software – queues to hold information received over networks
and dial up connections. (Information can betransmitted faster than it can be processed, so is
placed in a queue waiting to be processed)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 8/45
Data Structures (FALL-2011)
8
Checking Palindromes
• Read each letter in the phrase. Enqueue theletter into the queue, and push the letter ontothe stack.
• After we have read all of the letters in the phrase:
– Until the stack is empty, Dequeue a letter from thequeue and pop a letter from the stack.
– If the letters are not the same, the phrase is not a palindrome
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 9/45
Data Structures (FALL-2011)
9
Enqueue & Dequeue Operations
• Enqueue – Inserts an item at the rear of the queue.
Other names – Add , Insert
• Dequeue – Deletes an item from the front of the
queue. Other names – Delete, Remove, Serve• Since it is always the first item to be put into the
queue that is the first item to be removed, a queue is a
first-in, first-out or FIFO list
• Front and rear are also called headand tail respectively
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 10/45
Data Structures (FALL-2011)
10
Illustration
7869 50 32 69 50 32Enqueue(78)
7869 50 32
Enqueue(88)
88
7850 32 88
Dequeue() → 69
7832 88 Dequeue() → 50
f r rf
f r
f rf r
A queue is a FIFO list
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 11/45
Data Structures (FALL-2011)
Conditions of Queue:
• Overflow: It may be possible that a conditionarises when there is no place for adding theelement in queue. This is called overflow.
• Underflow: The second possibility arises whenthere is no element for deleting from queue. This
is called underflow.
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 12/45
Data Structures (FALL-2011)
QUEUE
The Queue operation can be explained as follows: Queue operation Content of array
Insert(a) Front=0 Rear=0
Insert(b) Front=0 Rear=1
Insert( c) Front=0 Rear=2
Delete() Front=1 Rear=2
Delete() Front=2 Rear=2
a
a b
a b c
b cc
If we try to insert
Overflow occurs
Though first two cells are empty
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 13/45
Data Structures (FALL-2011)
Linear Queue
int Q[5];
When INSERT is selected, Rear is incremented,
And data is added at that subscript location
When DELETE is selected, Front is decremented,
And data is removed from that subscript location
Queue array
int Front, Rear; To hold address of
location where data
is inserted or deleted
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 14/45
Data Structures (FALL-2011)
QUEUE
A Queue is a list, any list implementation can beused to implement Queue.
We can implement Queue by the following datastructures:
Array called Linear Queue
Linked List called Linked Queue
D S (FA 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 15/45
Data Structures (FALL-2011)
Lets see working of LINEAR QUEUE
8
9
10
Insert 7
7
8
9
10
Insert 20
20
7
89
10
Insert 14
OVERFLOW
QUEUE is full
Front
rear
rear
rear
FrontFront
Rear is incremented
Rear++
D t St t (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 16/45
Data Structures (FALL-2011)
Lets see working of Queue as an array
20
7
8
9
10
Delete
20
7
8
9
Delete
20
7
8
Underflow occurs
when QUEUE is
empty
Rear
Front
Rear Rear
FrontFront
Front is incremented
Front++
D t St t (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 17/45
Data Structures (FALL-2011)
18
Implementation of Queues
• The physical model: a linear array with the front always in the first position and all entries moved upthe array whenever the front is deleted
•Array Implementation: – A linear array with two indices always increasing
– A circular array with front and rear indices and one position left vacant
•Linked List – A linked list with pointers to the front and rear nodes
D t St t (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 18/45
Data Structures (FALL-2011)
Common Operations on Queues
(Queue ADT)1. MAKENULL(Q): Makes Queue Q be an empty list.
2. FRONT(Q): Returns the first element on Queue Q.
3. ENQUEUE(x,Q): Inserts element x at the end of Queue Q.
4. DEQUEUE(Q): Deletes the first element of Q.5. EMPTY(Q): Returns true if and only if Q is an empty
queue.
Example:
Line of customers in a bank
D t St t (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 19/45
Data Structures (FALL-2011)
Implementation
• Static
– Queue is implemented by an array, and size of
queue remains fix
• Dynamic
– A queue can be implemented as a linked list, and
expand or shrink with each enqueue or dequeue
operation.
Data Str ct res (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 20/45
Data Structures (FALL-2011)
Data Structures (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 21/45
Data Structures (FALL-2011)
A pointer Implementation of Queues
Keep two pointers:
FRONT: A pointer to the first element of the queue.
REAR: A pointer to the last element of the queue.
x y .zFront
Rear
Data Structures (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 22/45
Data Structures (FALL-2011)
A pointer Implementation of Queues
Q.front
Q.Rear
MAKENULL(Q)
Q.front
Q.Rear
ENQUEUE(x,Q)
.x
NULL
Data Structures (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 23/45
Data Structures (FALL-2011)
Q.frontQ.Rear
ENQUEUE(y,Q)
x .y
Q.front
Q.Rear
DEQUEUE(Q)
.y
A pointer Implementation of Queues
Data Structures (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 24/45
Data Structures (FALL-2011)
A class for Dynamic Queue implementation
class DynIntQueue
{
private:
struct QueueNode
{
int value;
QueueNode *next;
};
QueueNode *front;QueueNode *rear;
int numItems;
public:
DynIntQueue(void);
~DynIntQueue(void);
void enqueue(int);int dequeue(void);
bool isEmpty(void);
void makeNull(void);
};
Data Structures (FALL 2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 25/45
Data Structures (FALL-2011)
Implemenaton
//************************
// Constructor *//************************
DynIntQueue::DynIntQueue(void)
{
front = NULL;
rear = NULL;
numItems = 0;}
//************************
// Destructor *
//************************
DynIntQueue::~DynIntQueue(void){
makeNull();
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 26/45
Data Structures (FALL-2011)
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void DynIntQueue::enqueue(int num)
{
QueueNode *newNode;
newNode = new QueueNode;
newNode->value = num;
newNode->next = NULL;
if (isEmpty())
{
front = newNode;
rear = newNode;
}
else{
rear->next = newNode;
rear = newNode;
}
numItems++;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 27/45
Data Structures (FALL-2011)
//**********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies it into num. *
//**********************************************
int DynIntQueue::dequeue(void)
{
QueueNode *temp;
int num;
if (isEmpty())
cout << "The queue is empty.\n";else
{
num = front->value;
temp = front->next;
delete front;
front = temp;
numItems--;
}
return num;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 28/45
Data Structures (FALL-2011)
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool DynIntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 29/45
Data Structures (FALL 2011)
//********************************************
// Function makeNull dequeues all the elements *
// in the queue. *
//********************************************
void DynIntQueue::makeNull(void)
{
while(!isEmpty())
dequeue();
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 30/45
Data Structures (FALL 2011)
Program
// This program demonstrates the DynIntQeue class
void main(void)
{
DynIntQueue iQueue;
cout << "Enqueuing 5 items...\n";
// Enqueue 5 items.
for (int x = 0; x < 5; x++)
iQueue.enqueue(x);
// Deqeue and retrieve all items in the queue
cout << "The values in the queue were:\n";
while (!iQueue.isEmpty())
{
int value;
value =iQueue.dequeue();
cout << value << endl;}
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 31/45
Data Structures (FALL 2011)
Program Ouput
Enqueuing 5 items...
The values in the queue were:
0
1
2
3
4
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 32/45
Data Structures (FALL 2011)
Array Implementation
First Element
Last Element
maxlength
Front
Second
Element.
.
Rear
When queue is empty both front and rear are set to -1
While enqueueing increment rear by 1, and while dequeueing
increment front by 1
When there is only one value in the Queue, both rear and front
have same index
Can we implement Queue by using only one
index variable Front or Rear??
YES, by moving elements of array to neighboring
locations like we did in STACK but this is in-efficient
Why it is inefficient?
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 33/45
Data Structures (FALL 2011)
Array Implementation
5 4 6 7 8 7 6
0 1 2 3 4 5 6 7 8
Front=0
Rear=6
8 7 6
0 1 2 3 4 5 6 7 8
Front=4
Rear=6
7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=8How can we insert more elements? Rear index can
not move beyond the last element….
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 34/45
ata St uctu es ( 0 )
Solution: Using circular queue
• Allow rear to wrap around the array.
if(rear == queueSize-1)
rear = 0;else
rear++;
• Or use module arithmeticrear = (rear + 1) % queueSize;
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 35/45
( )
7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5Rear=8
Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0
39 7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=0
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 36/45
( )
How to determine empty and full
Queues?
• It is some tricky
• Number of approaches
– A counter indicating number of values in the queue
can be used (We will use this approach)
– We will see another approach as well at the end
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 37/45
( )
Implementationclass IntQueue
{ private:
int *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
int dequeue(void);
bool isEmpty(void);
bool isFull(void);void clear(void);
};
Note, the member function clear, which clears the queue by resetting the front
and rear indices, and setting the numItems to 0.
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 38/45
( )
IntQueue::IntQueue(int s) //constructor
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void) //destructor
{
delete [] queueArray;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 39/45
( )
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
cout << "The queue is full.\n";
else
{
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;// Update item count
numItems++;
}
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 40/45
//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************
int IntQueue::dequeue(void)
{
if (isEmpty())
cout << "The queue is empty.\n";
else
{
// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
int num = queueArray[front];
// Update item count
numItems--;
}
return num;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 41/45
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty(void)
{
if (numItems)
return false;
elsereturn true;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 42/45
//********************************************
// Function isFull returns true if the queue *// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
if (numItems < queueSize)return false;
else
return true;
}
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 43/45
TYPES of queue:
• Priority Queue: As define earlier, queue is an
ordered list of elements in which we can add theelements only at one end called the rear of queue &
delete element at the other end called the front of queue. But in priority queue every element of queuehas some priority and based on that priority it will beprocessed. So the element of more priority will be
processed before the element which has less priority.
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 44/45
tYpes of queue Cont…: •
Dequeue: Dequeue is also called Double endedqueue, as the name implies we can add or delete theelements from both sides. Dequeue can be of twotypes:
Input Restricted. Output Restricted.
In Input restricted dequeue, elements can be added atonly one end but we can delete the element from both
sides.In Output restricted dequeue, elements can added from
both sides but deletion is allowed only at one end.
Data Structures (FALL-2011)
7/29/2019 L-09 Queues.ppt
http://slidepdf.com/reader/full/l-09-queuesppt 45/45
Exercise
P R I O * R * * I * T * Y * * * Q U E * * * U * E
(where a letter means insert and an asterisk
means remove the maximum) is applied to an
initially empty priority queue. Give the sequenceof values returned by remove the maximum
operations.