Академический Документы
Профессиональный Документы
Культура Документы
Lecture-14
Umar Manzoor
1
Queues
Example:
Line of customers in a bank
3
Applications of Queues
Operating system
multi-user/multitasking environments, where
several users or task may be requesting the same
resource simultaneously.
Communication Software
queues to hold information received over networks
and dial up connections. (Information can be
transmitted faster than it can be processed, so is
placed in a queue waiting to be processed)
Some other?
4
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.
5
6
A pointer Implementation of Queues
Front x y z .
Rear
7
A pointer Implementation of Queues
MAKENULL(Q)
NULL
Q.front
Q.Rear
ENQUEUE(x,Q)
x .
Q.front
Q.Rear
8
A pointer Implementation of Queues
ENQUEUE(y,Q)
x y .
Q.front
Q.Rear
DEQUEUE(Q)
y .
Q.front
Q.Rear
9
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);
};
10
Implemenaton
//************************
// Constructor *
//************************
DynIntQueue::DynIntQueue(void)
{
front = NULL;
rear = NULL;
numItems = 0;
}
//************************
// Destructor *
//************************
DynIntQueue::~DynIntQueue(void)
{
makeNull();
}
11
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
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;
}
13
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool DynIntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;
}
14
//********************************************
// Function makeNull dequeues all the elements *
// in the queue. *
//********************************************
void DynIntQueue::makeNull(void)
{
while(!isEmpty())
dequeue();
}
15
Program
// This program demonstrates the DynIntQeue class
void main(void)
{
DynIntQueue iQueue;
16
Program Ouput
Enqueuing 5 items...
The values in the queue were:
0
1
2
3
4
17
Array Implementation
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
Front
First Element
Can we implement Queue by using only one
Second
Element
.
index variable Front or Rear??
. YES, by moving elements of array to neighboring
locations like we did in STACK but this is in-
efficient
Rear
Last Element Why it is inefficient?
maxlength
18
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
How can we insert more elements? Rear index can
Rear=8
not move beyond the last element….
19
Solution: Using circular queue
Allow rear to wrap around the array.
if(rear == queueSize-1)
rear = 0;
else
rear++;
Or use module arithmetic
rear = (rear + 1) % queueSize;
20
7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=8
21
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
22
Implementation
class 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.
23
IntQueue::IntQueue(int s) //constructor
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void) //destructor
{
delete [] queueArray;
}
24
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
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;
}
26
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;
}
27
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
if (numItems < queueSize)
return false;
else
return true;
}
28
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear(void)
{
front = - 1;
rear = - 1;
numItems = 0;
}
29
//Program demonstrating the IntQueue class
void main(void)
{
IntQueue iQueue(5);
31
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;
};
32
CQueue::CQueue(int size)
{
Front=Rear=-1;
Data=new int[size];
}
void CQueue ::Enqueue(int num);
{
if (IsFull()) { cout<<“Overflow” return; }
if (IsEmpty() Rear=Front=0;
else Rear=(Rear+1) % QueueSize;
Data[Rear]=num;
}
33
int CQueue ::Dequeue(int num);
{
if (IsEmpty()) { cout<<“Underflow”; return; }
int ReturnValue=Data[Front];
if (Front==Rear) //only one element in the queue
Front=Rear=-1;
else
Front=(Front+1) % QueueSize;
return ReturnValue;
}
34
bool CQueue::IsEmpty()
{
if (Front==-1) return true;
else return false;
}
bool CQueue::IsFull()
{
If (((Rear+1)%QueueSize)==Front)
return true;
else return false;
}
35