Queues

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

CS214 – Data Structures

Lecture 11: Queues

Slides by
Mohamed El-Ramly, PhD
Basheer Youssef, PhD
Agenda
1. Queues and their operations
2. Queues’ implementations
3. Queues’ applications

2
Queues
1. A queue is a linear data structure that
resembles a waiting line that grows by adding
elements to its end and shrinks by taking
elements from its front.

2. It is FIFO structure.
Queue Operations
1. clear () empties the queue

2. isEmpty() returns true if the queue is empty

3. enqueue (item) puts item at the end of the


queue

4. dequeue () removes and returns item at the


front of the queue

5. first () returns the first item in the queue without


removing it
Queue Example
1. Try this sequence of operations
• enqueue (10)
• enqueue (5)
• dequeue
• enqueue (15)
• enqueue (7)
• dequeue
1.
Queue Applications
Queues have numerous applications in life,
system modeling and computing.
2. An entire science is dedicated to studying
queues; which is called Queuing Theory.
3. We are not interested in the dynamics of the
queue but we take it as a data structure.
Queuing theory is a branch of mathematics that
studies how lines form, how they function, and
why they malfunction. Queuing theory examines
every component of waiting in line, including the
arrival process, service process, number of
servers, number of system places, and the
number of customers—which might be people,
data packets, cars, or anything else.
Queue Applications
Real-life applications of queuing theory cover a
wide range of businesses. Its findings may be
used to provide faster customer service,
increase traffic flow, improve order shipments
from a warehouse, or design data networks and
call centers.
As a branch of operations research, queuing theory
can help inform business decisions on how to
build more efficient and cost-effective workflow
systems.
Queue Applications
Queuing theory is the study of the movement of
people, objects, or information through a line.
Studying congestion and its causes in a process is
used to help create more efficient and cost-
effective services and systems.
Often used as an operations management tool,
queuing theory can address staffing, scheduling,
and customer service shortfalls.
Some queuing is acceptable in business. If there's
never a queue, it's a sign of overcapacity.
Queuing theory aims to achieve a balance that is
efficient and affordable.
Queue Applications Examples
• In operating systems, for controlling access to
shared system resources such as printers, files,
communication lines, disks and tapes.
• Simulation of real-world situations. E.g., a new
bank wants to know how many tellers to install.
The goal is to service each customer within a
"reasonable" wait time, but not have too many
tellers for the number of customers. To find out
a good number of tellers, they can run a
computer simulation of typical customer
transactions using queues to represent the
waiting customers.
Queue Applications Examples
• When placed on hold for telephone operators,
you may get a recording that says, "Thank you
for calling A-1 Company. Your call will be
answered by the next available operator. Please
wait." This is a queuing system.
Queue Implementations
• What are the possible options?
• Arrays?
• Circular Arrays?
• SLL?
• DLL?
• …….

• What are the pros and cons of each option?


Array-based Q
Circular Array-
based Q
Chapter 4: Stacks and Queues 14
Queue
Implementation -
DLL
Chapter 4: Stacks and Queues 16
Chapter 4: Stacks and Queues 17
Priority Queues
1. A priority queue is a queue whose elements
has priority and are dequeued according to their
priority and their queue position relative to other
elements with the same priority.

2. It is used when a overruling FIFO rule is needed


for some applications like: giving the service to
physically-challenged people first or scheduling
the patients in an emergency department.
Priority Queue Example
1. Try this sequence of operations
• enqueue (10,3) // (item, priority) 1 is top priority
• enqueue (5,2)
• dequeue
• enqueue (15,1)
• enqueue (7,3)
• dequeue

10 5 10 10 15 10 15 10 7 10 7
Implementing Priority Queue
1. One problem with priority queues is finding an
efficient implementation.

2. This is because arriving elements do not


necessary join the queue at the end and leaving
elements are not necessarily the elements at the
front of the queue.

3. We need also to define and implement a priority


criterion.
Implementing Priority Queue
1. What are possible implementations?

• Array?
• Linked List?
• What kind?
• Sorted or unsorted?

• Heap?
Queue in STL
Dequeue in STL
Priority Queue in STL
• Read the book and check cplusplus.com to
know what support is available for these data
structures in STL.
FIFO Queue in STL
• Queues is a FIFO container adaptor, where
elements are inserted into one end of the container
and extracted from the other.

• A containers adaptors is a class that uses an


encapsulated object of a specific container class as
its underlying container, providing a specific set of
member functions to access its elements.

• Elements are pushed into the "back" of the specific


container and popped from its "front".
FIFO Queue in STL
• The underlying container may be one of the
standard container class template or some other
specifically designed container class.
• The only requirement is that it supports the
following operations:

front()
back()
push_back()
pop_front()
FIFO Queue in STL
• The standard container class templates deque
and list can be used.

• By default, if no container class is specified for a


particular queue class, the standard container
class template deque is used.

• In STL, queues are defined as follows:


• template < class T,
class Container = deque<T> >
class queue;
Queue in STL
Creating
#include <iostream>
a STL Queue
#include <queue>
#include <list>
using namespace std;

int main() {
queue<int> q1;
queue<int,list<int> > q2; //leave space > >
q1.push(1); q1.push(2); q1.push(3);
q2.push(4); q2.push(5); q2.push(6);
q1.push(q2.back());
while (!q1.empty()) {
cout << q1.front() << ' '; // 1 2 3 6
q1.pop();
}
while (!q2.empty()) {
cout << q2.front() << ' '; // 4 5 6
q2.pop();
}
return 0;
}

27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy