Queue ADT
Queue ADT
5
The Queue ADT
▪ UML diagram for the class Queue
In many cases it needs to return ItemType as follows
+dequeue(newEntry: ItemType&) : boolean
6
Queue Operations: Enqueue
Queue Operations: Dequeue
Queue Operations: Peek Front
Peek
Front
Queue Example
Queue Example
Linked List Implementation
Linked List / Chain Implementation
13
Linked List / Chain Implementation
▪ How will the queue be declared?
▪ How will the queue be initialised? → Write a constructor
▪ How will the member functions be implemented? → note special
cases!
▪ Can the queue be implemented using a single pointer? What should
that pointer point to? Check out!
14
Linked List Implementation
Linked List Implementation: Structure
▪ Constructor?
▪ Enqueue?
▪ Dequeue?
▪ Special cases?
Linked List Implementation: Enqueue
Note: this case has to be
explicitly handled!
Linked List Implementation: Dequeue
Linked List Implementation: Dequeue
▪ A priority queue is an ADT with the property that only the highest-
priority element can be accessed at any time, even if they are
enqued late.
No modification is required!
Applications using Queues ADT
Using queue as ADT!
Enqueue
→ Dequeue
Peek
Queue Applications
→ • Categorizing Data
• Queue Simulation
Data Categorization
▪ Goal: categorize data within a queue while maintaining relative
order.
▪ Examples:
– Students according to their scores
– Employees according to their age group
▪ Integers are categorized as : 0-10, 11-19, 20-29.
Data Categorization: Required Output
Data Categorization: Core Code
The general algorithm would be:
q3.enqueue.(data);
}
return;
Data Categorization: Structures
Data Categorization: Structures
Queue Simulator
slidetemplates.com
icon-icons.com/
Queue Simulator
▪ Queuing theory is a field of applied mathematics that is used to
predict the performance of queues.
▪ To simulate a queue, we need the following information for each
individual:
42
Queue Simulator: Arrival Rate
Problem description:
• A customer arrives every m mins (arrival rate)
→ probability of a customer arriving at a certain instance? (1/m)
→ determine if a customer arrived at a certain instance or not
… use rand num generator
43
Queue Simulator: Arrival Time
▪ Now we want to answer the following question:
▪ At each instance: Did a new customer arrive?
▪ A common function used in these cases is the rand() function.
The most common form is
float rand()
that returns a value between 0 and 1.
→ So, assuming that probability of arrival of a customer is 25% if
rand() returns a value between 0 and 0.25 then we assume a
customer arrived, otherwise no customer arrived at this instance.
45
Queue Simulator: Service Time
▪ Another thing to determine is the customer service time
The rand function can be used to generate random values within any
required range. So a customer service time should be some random
value between min_servtime and max_servtime
Hence
Cust_servtime = min_servtime + rand()*(max_servtime – min_servtime)
46
Queue Simulator
To wrap-up:
Input:
▪ Probability of a new customer arriving,
▪ Max and min service time of a new customer
Output: Average wait time of customer is calculated
Algorithm: At each instance one or more of the following events may
occur:
▪ A new customer arrives
▪ The customer in front is served (finished)
In all cases time is incremented till simulation period finishes.
47
Queue Simulator: Example
Custom Arrival Service Start Finish Wait
er ID time time Time Time time
required
1 5 9 5 14 0
2 7 6 14 20 7
3 14 4 20 24 6
4 30 5 30 35 0
5 32 6 35 41 3
6 34 5 41 46 7
2. Two ways:
2.1. Use an additional count variable to follow enqueue and
dequeue and hence determine when the queue is full or empty.
59
Array Implementation: Circular Array
2. 2. Another solution is to get rid of count and watch
the relative position of front and back.
▪ However, the condition back == front may cause a
dilemma:
▪ Queue is becoming empty, as front has catched up with
back
▪ OR queue is becoming full, as back has catched up with
front
Array Implementation: Circular Array
▪ One option is to use the condition back == front
to indicate that queue is empty
▪ Then, queue full is indicated by:
(back+1) % max == front
i.e. back has catched up with front
The operations will be as follows ….
Array Implementation: Circular Array
▪ Initialize: front = back = 0;
▪ Hence, queue is EMPTY if front == back
▪ Operation enqueue:
arr [back] = it;
back = (back+1) % max;
▪ Operation dequeue: (provided that front != back)
it = arr [front]
front = (front+1) % max;
▪ Queue is FULL if (back+1) % max == front
i.e. back catched up with front
62
Array Implementation: Circular Array
▪ At this final case, FULL is true as
(back + 1) % max == front
▪ Then, enqueue will not succeed and this final location will NOT
be filled. i.e. only (max-1) locations out of max locations are
useful: back front