Lecture 12 - Queues (Arrays)
Lecture 12 - Queues (Arrays)
12. Queues
12-Queues 1
Queues
• Queue is First-In-First-Out (FIFO) data structure
– First element added to the queue will be first one to be
removed
12-Queues 2
Queue – Analogy (1)
• A queue is like a line of people waiting for a bank teller
• The queue has a front and a rear
Rear Front
12-Queues 3
Queue – Analogy (2)
• New people must enter the queue at the rear
Front
Rear
12-Queues 4
Queue – Analogy (3)
• An item is always taken from the front of the queue
Rear Front
12-Queues 5
Queues – Examples
• Billing counter
– Booking movie tickets
– Queue for paying bills
• A print queue
• And others?
12-Queues 6
Queues – Applications
• Operating systems
– Process scheduling in multiprogramming environment
– Controlling provisioning of resources to multiple users (or
processing)
• Middleware/Communication software
– Hold messages/packets in order of their arrival
Messages are usually transmitted faster than the time to process
them
12-Queues 7
Basic Operations (Queue ADT)
• MAKENULL(Q)
– Makes Queue Q be an empty list
• FRONT(Q)
– Returns the first element on Queue Q
• ENQUEUE(x,Q)
– Inserts element x at the end of Queue Q
• DEQUEUE(Q)
– Deletes the first element of Q
• EMPTY(Q)
– Returns true if and only if Q is an empty queue
12-Queues 8
Enqueue And Dequeue Operations
12-Queues 9
Implementation
• Static
– Queue is implemented by an array
– Size of queue remains fix
• Dynamic
– A queue can be implemented as a linked list
– Expand or shrink with each enqueue or dequeue operation
12-Queues 10
Array Implementation
• Use two counters that signify rear and
front
front= -1
rear = -1
0 1 2 3 4 5 6 7
8
front= 0
5 rear = 0
0 1 2 3 4 5 6 7
8
front= 0
5 4 rear = 1
0 1 2 3 4 5 6 7
8
12-Queues 12
Array Implementation Example (2)
front=0
5 4 6 7 8 7 6 rear=6
0 1 2 3 4 5 6 7
8
front=4
8 7 6
rear=6
0 1 2 3 4 5 6 7
8
front=5
7 6 12 67
rear=8
0 1 2 3 4 5 6 7
8
Problem: How can we insert more elements?
Rear index can not move beyond the last
element….
12-Queues 13
Using Circular Queue
• Allow rear to wrap around the array
if(rear == queueSize-1)
rear = 0;
else
rear++;
12-Queues 14
Example
7 6 12 67 front=5
rear=8
0 1 2 3 4 5 6 7
8
Enqueue 39
• Rear = (Rear+1) mod queueSize = (8+1) mod 9
= 0
39 7 6 12 67 front=5
rear=0
0 1 2 3 4 5 6 7
8
Problem: How to avoid overwriting an existing
element?
12-Queues 15
How to Determine Empty and Full Queues?
• A counter indicating number of values/items in the queue
– Covered in first array-based implementation
12-Queues 16
Array-based Implementation
12-Queues 17
Array Implementation – Code (1)
class IntQueue
{
private:
int *queueArray; // Pointer to array implemented as Queue
int queueSize; // Total size of the Queue
int front;
int rear;
int numItems; // Number of items currently in the Queue
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
int dequeue(void);
bool isEmpty(void);
bool isFull(void);
void makeNull(void);
};
12-Queues 18
Array Implementation – Code (2)
class IntQueue
{
private:
int *queueArray; // Pointer to array implemented as Queue
int queueSize; // Total size of the Queue
int front;
int rear;
int numItems; // Number of items currently in the Queue
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
int dequeue(void);
bool isEmpty(void);
bool isFull(void);
void makeNull(void); Clears the queue by resetting the front and
rear indices, and setting the numItems to 0.
};
12-Queues 19
Array Implementation – Code (3)
• Constructor
IntQueue::IntQueue(int s) //constructor
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
• Destructor
IntQueue::~IntQueue(void) //destructor
{
delete [] queueArray;
}
12-Queues 20
Array Implementation – Code (4)
• isFull() returns true if the queue is full and false
otherwise
bool IntQueue::isFull(void)
{
if (numItems < queueSize)
return false;
else
return true;
}
12-Queues 22
Array Implementation – Code (6)
• Function enqueue inserts the value in num at the end of
the Queue
else {
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}
}
12-Queues 23
Array Implementation – Code (7)
• Function dequeue removes and returns the value at the
front of the Queue
int IntQueue::dequeue(void)
{
int num;
if (isEmpty())
cout << "The queue is empty.\n";
else {
// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
num = queueArray[front];
// Update item count
numItems--;
}
return num;
}
12-Queues 24
Using Queues Output:
Enqueuing 5 items...
void main(void) Now attempting to enqueue
{ again...
IntQueue iQueue(5); The queue is full
cout << "Enqueuing 5 items...\n"; The values in the queue were:
// Enqueue 5 items. 0
for (int x = 0; x < 5; x++) 1
iQueue.enqueue(x); 2
// Attempt to enqueue a 6th item. 3
4
cout << "Now attempting to enqueue again...\n";
iQueue.enqueue(5);
// 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;
}
}
12-Queues 25
Alternative Array-based
Implementation
12-Queues 26
Alternative Implementation – Code (1)
class CQueue
{
Private:
int *queueArray; // Pointer to array implemented as Queue
int queueSize; // Total size of the Queue
int front;
int rear;
public:
CQueue(int size);
~CQueue( );
bool IsFull();
bool IsEmpty();
void enqueue(int num);
int dequeue();
void MakeNull();
};
12-Queues 27
Alternative Implementation – Code (2)
• isEmpty() returns true if the queue is empty and false
otherwise
bool CQueue::IsEmpty()
{
if (front==-1)
return true; // we can check “rear” too
else
return false;
}
12-Queues 29
Comparison: enqueue Operation
else {
// Calculate the new rear
position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}
}
12-Queues 30
Alternative Implementation – Code (4)
• Function dequeue removes and returns the value at the
front of the Queue
int CQueue::dequeue()
{
if ( IsEmpty() ) {
cout<<“Underflow”;
return;
}
int ReturnValue = queueArray[front];
return ReturnValue;
}
12-Queues 31
Any Question So Far?
12-Queues 32
9-Queues 33
• Function enqueue
else {
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}
}
9-Queues 34
Constructor And Destructor
9-Queues 35
Array Implementation – Code (2)
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); Clears the queue by resetting the front and rear
}; indices, and setting the numItems to 0.
9-Queues 36
Roadmap
• List as an ADT
• An array-based implementation of lists