0% found this document useful (0 votes)
7 views

Lecture 12 - Queues (Arrays)

Uploaded by

nhqureshi7717
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 12 - Queues (Arrays)

Uploaded by

nhqureshi7717
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structures

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

• Queue implements a special kind of list


– Items are inserted at one end (the rear)
– Items are deleted at the other end (the front)

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

• Vehicles on toll-tax bridge

• Luggage checking machine

• 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

– The most common application is in client-server models


 Multiple clients may be requesting services from one or more
servers
 Some clients may have to wait while the servers are busy
 Those clients are placed in a queue and serviced in the order of
arrival

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

• When queue is empty


– Both front and rear are set to -1
front A First
Element
• When there is only one value in the
B Second
Queue, Element

– Both rear and front have same index C


D .
• While enqueueing increment rear by 1
E .
.

• While dequeueing, increment front by 1


F
rear G Last
Element
12-Queues 11
Array Implementation Example (1)

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++;

• Alternatively, use modular arithmetic

rear = (rear + 1) % queueSize;

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

• Without using an additional counter (only relying on front


and rear)
– Covered in alternative 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;
}

• isEmpty() returns true if the queue is empty and false


otherwise
bool IntQueue::isEmpty(void)
{
if (numItems == 0)
return true;
else
return false;
12-Queues 21
}
Array Implementation – Code (5)
• makeNull() resets front & rear indices and sets
numItems= 0
void IntQueue::makeNull(void)
{
front = - 1;
rear = - 1;
numItems = 0;
}

12-Queues 22
Array Implementation – Code (6)
• Function enqueue inserts the value in num at the end 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++;
}
}

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;
}

• isFull() returns true if the queue is full and false


otherwise
bool CQueue::IsFull()
{
if ( ( (rear+1)%queueSize ) == front )
return true;
else
return false;
12-Queues 28
}
Alternative Implementation – Code (3)
• Function enqueue inserts the value in num at the end of
the Queue

void CQueue ::enqueue(int num);


{
if ( IsFull() ) {
cout<<“Overflow”;
return;
}
if (IsEmpty())
rear = front = 0;
else
rear=(rear+1) % queueSize;
queueArray[rear] = num;
}

12-Queues 29
Comparison: enqueue Operation

void CQueue ::enqueue(int num);


{
if ( IsFull() ) {
cout<<“Overflow”;
return;
}
if (IsEmpty())
rear = front = 0;
else
rear=(rear+1) % queueSize; void IntQueue::enqueue(int num)
queueArray[rear] = 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++;
}
}
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];

if ( front == rear ) //only one element in the


queue
front = rear = -1;
else
front = (front+1) % queueSize;

return ReturnValue;
}
12-Queues 31
Any Question So Far?

12-Queues 32
9-Queues 33
• Function enqueue

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++;
}
}

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

• Introduction to linked lists


• A pointer-based implementation in C++
• Variations of linked lists

8-Link List Variations 37


Array Implementation – Code (4)
• Function isFull() returns true if the queue is empty and
false otherwise
bool IntQueue::isFull(void)
{
if (numItems < queueSize)
return false;
else
return true;
}

• Function clear() resets the front and rear indices, and


sets numItems to 0
void IntQueue::clear(void)
{
front = - 1;
rear = - 1;
numItems = 0;
}
9-Queues 38

You might also like