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

Lecture 8 2024-Queues

Uploaded by

jiawenf1009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Lecture 8 2024-Queues

Uploaded by

jiawenf1009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Queues

Chung-Ming Chen
Department of Biomedical Engineering

Textbook:
1. C++ Programming: From Problem Analysis to Program Design, 8th Edition, D.S. Malik
2. Data Structures Using C++, 2nd Edition, D.S. Malik Page 1
Queues

l Queue: A data structure in which the elements are added at one end,
called the rear (or back), and deleted from the other end, called the front;
a First In First Out (FIFO) data structure..

rear front
E

D C B A

Spring 2024 Data Structure 2


Queues
l As in the case of a stack, a queue can be stored in an array or in a linked
structure. Two pointers are used to keep track of the front and rear of the
queue, called queueFront and queueRear.
l Some of the queue operations are as follows::
– initializeQueue—Initializes the queue to an empty state.
– isEmptyQueue—Determines whether the queue is empty. If the queue is
empty, it returns the value true; otherwise, it returns the value false.
– isFullQueue—Determines whether the queue is full. If the queue is full, it
returns the value true; otherwise, it returns the value false.
– front—Returns the front, that is, the first element of the queue. Prior to this
operation, the queue must exist and must not be empty.
– back—Returns the last element of the queue. Prior to this operation, the
queue must exist and must not be empty.
– addQueue—Adds a new element to the rear of the queue. Prior to this
operation, the queue must exist and must not be full.
– deleteQueue—Removes the front element from the queue. Prior to this
operation, the queue must exist and must not be empty.
Spring 2024 Data Structure 3
class queueADT

Spring 2024 Data Structure 4


Implementation of Queues as Arrays

l What are the variables required to implement a queue as an ADT


using an array ?
– an array to store the queue elements,
– the variables queueFront and queueRear to keep track of the first and
last elements of the queue,
– the variable maxQueueSize to specify the maximum size of the queue.
l To add an element to the queue, first we advance queueRear to the
next array position and then add the element to the position that
queueRear is pointing to.
l To delete an element from the queue, first we retrieve the element
that queueFront is pointing to and then advance queueFront to the
next element of the queue.
l Thus, queueFront changes after each deleteQueue operation and
queueRear changes after each addQueue operation (changes before
adding the element).

Spring 2024 Data Structure 5


Implementation of Queues as Arrays

l Assume that the array to hold the queue elements is of size 100.
l Initially, the queue is empty. After the operation:
addQueue(Queue,'A');
l the array is as shown

l After two more addQueue operations:


addQueue(Queue,'B');
addQueue(Queue,'C');
l Now consider the deleteQueue operation:
deleteQueue();
l After this operation, the array containing the queue is as shown

Spring 2024 Data Structure 6


Problems with Linear Queues using Arrays
l Suppose A stands for adding (that is, addQueue) an element to the
queue, and D stands for deleting (that is, deleteQueue) an element
from the queue. Consider the following sequence of operations:
AAADADADADADADADA...
l This sequence of operations would eventually set the index
queueRear to point to the last array position, giving the impression
that the queue is full. However, the queue has only two or three
elements and the front of the array is empty.

l One solution to this problem is that when the queue overflows to the
rear, we can check the value of the index queueFront. If it indicates
that there is room in the front of the array, then when queueRear gets
to the last array position, we can slide all of the queue elements
toward the first array position. This solution is good if the queue size
is very small; otherwise, the program might execute more slowly.
Spring 2024 Data Structure 7
Circular Queues using Arrays
l A better solution to this problem is to assume that the array is
circular—that is, the first array position immediately follows the last
array position.
l We will consider the array containing the queue to be circular,
although we will draw the figures of the array holding the queue
elements as before.

Spring 2024 Data Structure 8


Circular Queues using Arrays
l Suppose that we have the queue as shown below.

l After the operation addQueue(Queue,'Z');, the queue is as shown in (b).


l Because the array containing the queue is circular, we can use the
statement to advance queueRear (queueFront) to the next array position:
queueRear = (queueRear + 1) % maxQueueSize;
l If queueRear < maxQueueSize - 1, then queueRear + 1 <= maxQueueSize - 1,
so (queueRear + 1) % maxQueueSize = queueRear + 1. If queueRear ==
maxQueueSize - 1 (that is, queueRear points to the last array position),
queueRear + 1 == maxQueueSize, so (queueRear + 1) % maxQueueSize = 0.
In this case, queueRear will be set to 0, which is the first array position.

Spring 2024 Data Structure 9


Problems with Circular Queues using Arrays
l Consider the following two cases:
l Case 1:

Empty Queue

l Case 2:

Full Queue

l The arrays in both Figures (b) have identical values for queueFront and
queueRear. However, the one represents an empty queue, whereas the
other represents a full queue.

Spring 2024 Data Structure 10


Problems with Circular Queues using Arrays
l Two possible solutions:
l One solution is to keep a count. In addition to the member variables
queueFront and queueRear, we need another variable, count, to implement
the queue. The value of count is incremented whenever a new element is
added to the queue, and it is decremented whenever an element is
removed from the queue. In this case, the function initializeQueue
initializes count to 0. This solution is very useful if the user of the queue
frequently needs to know the number of elements in the queue.
l Another solution is to let queueFront indicate the index of the array
position preceding the first element of the queue. The queue is empty if
queueFront == queueRear. In this solution, the slot indicated by the index
queueFront is reserved. The queue will be full if the next available space is
the special reserved slot indicated by queueFront.

Spring 2024 Data Structure 11


class queueType

Spring 2024 Data Structure 12


class queueType

Spring 2024 Data Structure 13


Empty Queue and Full Queue

Spring 2024 Data Structure 14


Initialize Queue

l The first element is added at the first array position. Therefore, we


initialize queueFront to 0, queueRear to maxQueueSize - 1, and count
to 0.

Spring 2024 Data Structure 15


Front and Back

l Front: This operation returns the first element of the queue. If the
queue is nonempty, the element of the queue indicated by the index
queueFront is returned; otherwise, the program terminates.

l Back: This operation returns the last element of the queue. If the
queue is nonempty, the element of the queue indicated by the index
queueRear is returned; otherwise, the program terminates.

Spring 2024 Data Structure 16


Add Queue

l Because queueRear points to the last element of the queue, to add a


new element to the queue, we first advance queueRear to the next
array position, and then add the new element to the array position
indicated by queueRear. We also increment count by 1.

Spring 2024 Data Structure 17


Delete Queue

l Because queueFront points to the array position containing the first


element of the queue, to remove the first queue element, we
decrement count by 1 and advance queueFront to the next queue
element.

Spring 2024 Data Structure 18


Constructors and Destructors

Constructor

Destructor

Spring 2024 Data Structure 19


Linked Implementation of Queues

l Because the size of the array to store the queue elements is fixed,
only a finite number of queue elements can be stored in the array.
l Also, the array implementation of the queue requires the array to be
treated in a special way together with the values of the indices
queueFront and queueRear.

l The linked implementation of a queue simplifies many of the special


cases of the array implementation and, because the memory to
store a queue element is allocated dynamically, the queue is
never full.
l Because elements are added at one end, queueRear, and removed
from the other end, queueFront, we need to know the front of the
queue and the rear of the queue. Thus, we need two pointers,
queueFront and queueRear, to maintain the queue.

Spring 2024 Data Structure 20


Linked Implementation of Queues

Spring 2024 Data Structure 21


Linked Implementation of Queues

Spring 2024 Data Structure 22


Empty and Full Queue

l The queue is empty if queueFront is NULL. Memory to store the


queue elements is allocated dynamically. Therefore, the queue is
never full and so the function to implement the isFullQueue operation
returns the value false.

Spring 2024 Data Structure 23


Initialize Queue

l The operation initializeQueue initializes the queue to an empty state.


The queue is empty if there are no elements in the queue Therefore,
this operation traverses the list containing the queue starting at the
first node, and it deallocates the memory occupied by the queue
elements.

Spring 2024 Data Structure 24


addQueue
l The addQueue operation adds a new element at the end of the queue.
To implement this operation, we access the pointer queueRear.

Spring 2024 Data Structure 25


front
l If the queue is nonempty, the operation front returns the first element
of the queue and so the element of the queue indicated by the pointer
queueFront is returned. If the queue is empty, the function front
terminates the program.

Spring 2024 Data Structure 26


back
l If the queue is nonempty, the operation back returns the last element
of the queue and so the element of the queue indicated by the pointer
queueRear is returned. If the queue is empty, the function back
terminates the program.

Spring 2024 Data Structure 27


deleteQueue

l if the queue is nonempty, the operation deleteQueue removes the first


element of the queue, and so we access the pointer queueFront.

Spring 2024 Data Structure 28


Constructor and Others

l When the queue object goes out of scope, the destructor destroys the
queue; that is, it deallocates the memory occupied by the elements of
the queue. The definition of the function to implement the destructor
is similar to the definition of the function initializeQueue. Also, the
functions to implement the copy constructor and overload the
assignment operators are similar to the corresponding functions for
stacks.

Spring 2024 Data Structure 29


STL class queue (Queue Container Adapter)
l The Standard Template Library (STL) provides a class to implement queues
in a program. The name of the class defining the queue is queue, and the
name of the header file containing the definition of the class queue is queue.
l In addition to the operations size, empty, push, front, back, and pop, the
queue container class provides relational operators to compare two queues.
For example, the relational operator == can be used to determine whether
two queues are identical.

Spring 2024 Data Structure 30


Priority Queues

l The use of a queue structure ensures that the items are processed in
the order they are received. For example, in a banking environment,
the customers who arrive first are served first.
l However, there are certain situations when this First In First Out rule
needs to be relaxed somewhat.
l In a hospital environment, patients are, usually, seen in the order they
arrive. Therefore, you could use a queue to ensure that the patients
are seen in the order they arrive.
l However, if a patient arrives with severe or life-threatening symptoms,
they are treated first. In other words, these patients take priority over
the patients who can wait to be seen, such as those awaiting their
routine annual checkup.
l To implement such a data structure in a program, we use a special
type of queue, called priority queues. In a priority queue, customers or
jobs with higher priority are pushed to the front of the queue.

Spring 2024 Data Structure 31


Queuing System

l Scenario: The manager of a local movie theater is hearing


complaints from customers about the time they have to wait in line to
buy tickets. The theater currently has only one cashier. Another
theater is preparing to open in the neighborhood and the manager is
afraid of losing customers. The manager wants to hire enough
cashiers so that a customer does not have to wait too long to buy a
ticket, but does not want to hire extra cashiers on a trial basis and
potentially waste time and money. One thing that the manager would
like to know is the average time a customer has to wait for service.
The manager wants someone to write a program to simulate the
behavior of the theater.

Spring 2024 Data Structure 32


Queuing System

l In the theater problem, when the cashier is serving a customer, the


other customers must wait. Because customers are served on a first-
come, first-served basis and queues are an effective way to
implement a First In First Out system, queues are important data
structures for use in computer simulations.
l The following examines computer simulations in which queues are
the basic data structure. These simulations model the behavior of
systems, called queuing systems, in which queues of objects are
waiting to be served by various servers. In other words, a queuing
system consists of servers and queues of objects waiting to be
served.
l Examples: a grocery store, a banking system, a printing process.

Spring 2024 Data Structure 33


Queuing System

Spring 2024 Data Structure 34


Time-Driven Simulation
To estimate the average waiting time, we need to simulate:
1. The arrival model
2. Waiting time of each customer: starting from entering the queue
3. Service time: the time being served

Waiting time
Arrival model, e.g.,
Poisson distribution

Service time

Spring 2024 Data Structure 35


Arrival Model

l For example, Poisson distribution


l The probability of y events occurring at a given time:
!! " "#
𝑃 𝑦 = , 𝑦 = 0,1,2, … ,
#!
l is the expected value that y events occur at that time.
l Suppose that, on average, a customer arrives every four minutes.
During this four-minute period, the customer can arrive at any one of
the four minutes. Assuming an equal likelihood of each of the four
minutes, the expected value that a customer arrives in each of the
four minutes is, therefore, 1/4=0.25.

Spring 2024 Data Structure 36


Arrival Model

Spring 2024 Data Structure 37


Simulation Algorithm of a Queuing System

1. Declare and initialize the variables such as the simulation parameters, customer
number, clock, total and average waiting times, number of customers arrived,
number of customers served, number of customers left in the waiting queue,
number of customers left with the servers, waitingCustomersQueue, and a list of
servers.
2. The main loop is as follows:
for (clock = 1; clock <= simulationTime; clock++)
{
2.1. Update the server list to decrement the transaction time of each busy
server by one time unit.
2.2. If the customer’s queue is nonempty, increment the waiting time of each
customer by one time unit.
2.3. If a customer arrives, increment the number of customers by 1 and add the
new customer to the queue.
2.4. If a server is free and the customer’s queue is nonempty, remove a
customer from the front of the queue and send the customer to the free server.
}

Spring 2024 Data Structure 38

You might also like