Lec 14 Queues
-
Upload
raja-mustafa -
Category
Documents
-
view
215 -
download
0
Transcript of Lec 14 Queues
-
8/14/2019 Lec 14 Queues
1/35
1
Queues
Lecture-14
Umar Manzoor
-
8/14/2019 Lec 14 Queues
2/35
2
Queues
A Queueis 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, rather thanthe beginning of the list.
-
8/14/2019 Lec 14 Queues
3/35
3
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 ofQ.
5. EMPTY(Q): Returns true if and only if Q is an empty queue.
Example:
Line of customers in a bank
-
8/14/2019 Lec 14 Queues
4/35
4
Applications of Queues
Operating systemmulti-user/multitasking environments, where
several users or task may be requesting the sameresource simultaneously.
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)Some other?
-
8/14/2019 Lec 14 Queues
5/35
5
Implementation
StaticQueue is implemented by an array, and size of
queue remains fix
Dynamic A queue can be implemented as a linked list,
and expandorshrinkwith each enqueue or
dequeue operation.
-
8/14/2019 Lec 14 Queues
6/35
6
-
8/14/2019 Lec 14 Queues
7/35
7
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
-
8/14/2019 Lec 14 Queues
8/35
8
A pointer Implementation of Queues
Q.front
Q.Rear
MAKENULL(Q)
Q.front
Q.Rear
ENQUEUE(x,Q)
.x
NULL
-
8/14/2019 Lec 14 Queues
9/35
9
Q.frontQ.Rear
ENQUEUE(y,Q)
x .y
Q.front
Q.Rear
DEQUEUE(Q)
.y
A pointer Implementation of Queues
-
8/14/2019 Lec 14 Queues
10/35
10
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);
};
-
8/14/2019 Lec 14 Queues
11/35
11
Implemenaton
//************************
// Constructor *
//************************
DynIntQueue::DynIntQueue(void)
{
front = NULL;
rear = NULL;
numItems = 0;
}
//************************
// Destructor *
//************************
DynIntQueue::~DynIntQueue(void){
makeNull();
}
-
8/14/2019 Lec 14 Queues
12/35
12
//********************************************
// 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++;
}
-
8/14/2019 Lec 14 Queues
13/35
13
//**********************************************
// 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 value;
temp = front->next;
delete front;
front = temp;
numItems--;
}
return num;
}
-
8/14/2019 Lec 14 Queues
14/35
14
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool DynIntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;}
-
8/14/2019 Lec 14 Queues
15/35
15
//********************************************
// FunctionmakeNull dequeues all the elements *
// in the queue. *
//********************************************
void DynIntQueue::makeNull(void)
{
while(!isEmpty())
dequeue();
}
-
8/14/2019 Lec 14 Queues
16/35
16
Program
// This program demonstrates the DynIntQeue class
void main(void)
{
DynIntQueue iQueue;
cout
-
8/14/2019 Lec 14 Queues
17/35
17
Program Ouput
Enqueuing 5 items...
The values in the queue were:
0
1
2
3
4
-
8/14/2019 Lec 14 Queues
18/35
18
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?
-
8/14/2019 Lec 14 Queues
19/35
19
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.
-
8/14/2019 Lec 14 Queues
20/35
20
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;
-
8/14/2019 Lec 14 Queues
21/35
21
7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=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
-
8/14/2019 Lec 14 Queues
22/35
22
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
-
8/14/2019 Lec 14 Queues
23/35
23
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.
-
8/14/2019 Lec 14 Queues
24/35
24
IntQueue::IntQueue(int s) //constructor
{
queueArray = new int[s];
queueSize = s;front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void) //destructor
{
delete [] queueArray;
}
-
8/14/2019 Lec 14 Queues
25/35
25
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
cout
-
8/14/2019 Lec 14 Queues
26/35
26
//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************
int IntQueue::dequeue(void)
{
if (isEmpty())
cout
-
8/14/2019 Lec 14 Queues
27/35
-
8/14/2019 Lec 14 Queues
28/35
28
//********************************************
// Function isFull returns true if the queue *// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
if (numItems < queueSize)return false;
else
return true;
}
-
8/14/2019 Lec 14 Queues
29/35
29
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear(void)
{
front = - 1;
rear = - 1;
numItems = 0;
}
-
8/14/2019 Lec 14 Queues
30/35
30
//Program demonstrating the IntQueue class
void main(void)
{
IntQueue iQueue(5);
cout
-
8/14/2019 Lec 14 Queues
31/35
31
Program Output
Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.The values in the queue were:
0
1
2
3
4
-
8/14/2019 Lec 14 Queues
32/35
32
Another implementation of Queues using
Arrays
class CQueue{
int Data*,QueueSize,Front,Rear;
public:
CQueue(int size);~CQueue(int size);
bool IsFull();
bool IsEmpty();
void Enqueue(int num);int Dequeue();
void MakeNull;
};
-
8/14/2019 Lec 14 Queues
33/35
33
CQueue::CQueue(int size)
{
Front=Rear=-1;Data=new int[size];
}
void CQueue ::Enqueue(int num);
{ if (IsFull()) { cout
-
8/14/2019 Lec 14 Queues
34/35
34
int CQueue ::Dequeue(int num);{
if (IsEmpty()) { cout
-
8/14/2019 Lec 14 Queues
35/35
35
bool CQueue::IsEmpty()
{
if (Front==-1) return true;else return false;
}
bool CQueue::IsFull()
{
If (((Rear+1)%QueueSize)==Front)
return true;
else return false;}