Data Structure
Data Structure
Queue ADT
• Like a stack, a queue (pronounced "cue") is a List
ADT that holds a sequence of elements.
• Restriction: Items are inserted at one end while
deleted from other end.
• A queue, provides access to its elements in first-in,
first-out (FIFO) order.
• The elements in a queue are processed like
customers standing in a grocery check-out line: the
first customer in line is the first one served.
Example Applications of Queues
• In a multi-user system, a queue is used to hold
print jobs submitted by users , while the printer
services those jobs one at a time.
Dequeue Enqueue
Array Implementation
• Suppose we have an empty static queue that
is capable of holding a maximum of three
values. With that queue we execute the
following enqueue operations.
Enqueue(3);
Enqueue(6);
Enqueue(9);
Queue Operations
• The state of the queue after each of the
enqueue operations.
Queue Operations
• The state of the queue after each of three
consecutive dequeue operations:
Queue Operations
• When the last deqeue operation is
performed in the illustration, the queue is
empty. An empty queue can be signified by
setting both front and rear indices to –1.
0 1 2 3 4 5 6
F = R = -1
Empty Queue
Array Implementation - Limitations
0 1 2 3 4 5 6
2
F R
0 1 2 3 4 5 6
2 4 0 12 5 7 9
F R
0 1 2 3 4 5 6
12 5 7 9
F R
0 1 2 3 4 5 6
12 5 7 9
F R
//*************************
// Constructor *
//*************************
Queue::Queue(int s)
{
queueArray = new double[s];
maxSize = s;
front = -1;
rear = -1;
numItems = 0;
}
O(1)
Destructor
//*************************
// Destructor *
//*************************
Queue::~Queue(void)
{
delete [] queueArray;
}
O(1)
Enqueue operation
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
bool Queue::isEmpty(void)
{
bool status;
return status;
}
O(1)
isFull operation
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool Queue::isFull(void)
{
bool status;
return status;
}
O(1)
Clear operation
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void Queue::clear(void)
{
front = -1;
rear = -1;
numItems = 0;
}
O(1)
Illustrating Program
// This program demonstrates the Queue class
void main(void)
{
Queue queue(5);
7 0
6 1
5 2
4 3
Empty Queue
Improving efficiency
• Use circular queue F
R
2
7 0
6 1
5 2
4 3
2
7 0
6 1 8
6 5 2 5
R 4 3
4 10
7 0
6 1
6 5 2 5
R 4 3 F
4 10
3 7
7 0 R
16 6 1 9
6 5 2 5
4 3 F
4 10
//*************************
// Constructor *
//*************************
Queue::Queue(int s)
{
queueArray = new double[s];
maxSize = s;
front = -1;
rear = -1;
numItems = 0;
}
O(1)
Destructor
//*************************
// Destructor *
//*************************
Queue::~Queue(void)
{
delete [] queueArray;
}
O(1)
Enqueue operation
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
double Queue::dequeue(void)
{
if (isEmpty())
cout << "The queue is empty.\n";
else
{
// Retrieve the front item
num = queueArray[front];
// Move front
front = (front + 1)%maxSize;
// Update item count and return num
numItems--;
// Last element deleted
if (numItems == 0){front = -1; rear = -1;}
return(num);
}
} O(1)
isEmpty operation
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool Queue::isEmpty(void)
{
bool status;
return status;
}
O(1)
isFull operation
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool Queue::isFull(void)
{
bool status;
return status;
}
O(1)
Clear operation
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void Queue::clear(void)
{
front = -1;
rear = -1;
numItems = 0;
}
O(1)
Linked List-based Queues
• A dynamic queue starts as an empty linked list.
QueueNode *front;
QueueNode *rear;
int numItems;
public:
Queue(void);
~Queue(void);
void enqueue(double);
double dequeue(void);
bool isEmpty(void);
void clear(void);
};
Constructor and Destructor
//************************
// Constructor *
//************************
Queue::Queue(void)
{
front = NULL;
rear = NULL; O(1)
numItems = 0;
}
//************************
// Destructor *
//************************
Queue::~Queue(void)
{
clear();
} O(n)
Enqueue operation
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
}
numItems++; O(1)
Dequeue operation
//**********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies it into num. *
//**********************************************
double Queue::dequeue(void)
{
QueueNode *temp;
if (isEmpty())
cout << "The queue is empty.\n";
else
{
num = front->value;
temp = front->next;
delete front;
front = temp;
numItems--;
return(num);
}
} O(1)
isEmpty operation
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool Queue::isEmpty(void)
{
bool status;
return status;
}
O(1)
Clear operation
//********************************************
// Function clear dequeues all the elements *
// in the queue. *
//********************************************
void Queue::clear(void)
{
int value; // Dummy variable for dequeue
while(!isEmpty())
value = dequeue();
}
O(n)
Illustrating Program
// This program demonstrates the Queue class
#include <iostream.h>
void main(void)
{
Queue queue;
Program Ouput
Enqueuing 5 items...
The values in the queue were:
0
1
2
3
4
PRIORITY QUEUE
• A priority queue is a type of queue in which each element is
assigned a priority.
• The elements are inserted at the rear.
• The elements are deleted according to the priority.
• While implementing a priority queue, following two rules are
applied.
– The element with higher priority is processed
before any element of lower priority.
– The elements with the same priority are processed
according to the order in which they were added
to the queue.
• A priority queue can be represented in many ways. Here, we
are discussing multi-queue implementation of priority queue.
Multi-queue Implementation (Array
Representation of Priority Queue)
Input-restricted Deque
Output-restricted Deque
Deque using Singly-Linked List
7 1 5 9 NULL
Front Rear
NULL 7 1 5 9 NULL
Front Rear
• All the processes that are residing in memory and are ready to
execute are kept in a list referred as ready queue. It is the job
of scheduling algorithm to select a process from the processes
and allocate the CPU to it.