Lecture 8 2024-Queues
Lecture 8 2024-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
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 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.
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.
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.
Constructor
Destructor
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 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.
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.
Waiting time
Arrival model, e.g.,
Poisson distribution
Service time
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.
}