Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Queues

ERIN KEITH

10_QUEUES 1
Topics
1. Covid Quiz
2. Queue ADT
3. Using Queues
4. Interface
5. Homework 3

10_QUEUES 2
Covid Quiz
Due Sunday, 3/7 at midnight!

10_QUEUES 3
Queue ADT
Qualities of an ADT Queue:
• Finite number of objects
• Not necessarily distinct
• Same data type
• Ordered by First In, First Out

10_QUEUES 4
Queue ADT
Queue-like things
• Buffers (cout / cin)
• Simulations (because time and events are FIFO)
Queues contain items of the same type
Operations
• Empty?
• Add, remove items
• Retrieve

10_QUEUES 5
Queue ADT
Qualities of an ADT Queue:
• Items are referenced by
• the “front” of the Queue
• the “back” of the Queue
• if you want an item “in the middle”, you need to remove all the
front items to get to it

10_QUEUES 6
Queue ADT
Operations
• isEmpty(): boolean
• enqueue(newEntry: ItemType): boolean
• dequeue(): boolean
• peekFront(): ItemType

Note* peekFront is the only function that returns an ItemType!

10_QUEUES 7
Queue Use
LinkedQueue<string> groceryQueue;

Items

10_QUEUES 8
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.engueue("milk");

Items
milk

10_QUEUES 9
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal");

Items
milk
cereal

10_QUEUES 10
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal");
groceryQueue.enqueue("eggs");

Items
milk
cereal
eggs

10_QUEUES 11
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal");
groceryQueue.enqueue("eggs");

string frontItem = groceryQueue.peekFrontFront();


// frontItem stores "milk"

Items
milk
cereal
eggs

10_QUEUES 12
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal"); Items
groceryQueue.enqueue("eggs");
milk
string frontItem = groceryQueue.peekFront(); cereal
//frontItem stores "milk"
eggs
do {
if(!groceryQueue.isEmpty()){
cout << groceryQueue.peekFront() << endl;
}
}while(groceryQueue.dequeue());

10_QUEUES 13
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal"); Items
groceryQueue.enqueue("eggs");
cereal
string frontItem = groceryQueue.peekFront(); eggs
//frontItem stores "milk"

do {
if(!groceryQueue.isEmpty()){
cout << groceryQueue.peekFront() << endl;
}
}while(groceryQueue.dequeue());

10_QUEUES 14
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal"); Items
groceryQueue.enqueue("eggs");
eggs
string frontItem = groceryQueue.peekFront();
//frontItem stores "milk"

do {
if(!groceryQueue.isEmpty()){
cout << groceryQueue.peekFront() << endl;
}
}while(groceryQueue.dequeue());

10_QUEUES 15
Queue Use
LinkedQueue<string> groceryQueue;

groceryQueue.enqueue("milk");
groceryQueue.enqueue("cereal"); Items
groceryQueue.enqueue("eggs");

string frontItem = groceryQueue.peekFront();


//frontItem stores "milk"

do {
if(!groceryQueue.isEmpty()){
cout << groceryQueue.peekFront() << endl;
}
}while(groceryQueue.dequeue());

10_QUEUES 16
Queue ADT
Axioms
1. (Queue()).isEmpty = true
2. (Queue()).peekFront() => error
3. (aQueue.enqueue(item)).isEmpty() = false
4. (aQueue.enqueue(item)).peekFront() = item
5. (aQueue.enqueue(item)).dequeue() = true
6. (aQueue.enqueue(item)).dequeue() = aQueue
7. (Queue()).dequeue() = false

10_QUEUES 17
Queue Implementation
#ifndef QUEUE_INTERFACE
#define QUEUE_INTERFACE

template<class ItemType>
class QueueInterface {
public:
virtual bool isEmpty() const = 0;
virtual bool enqueue(const ItemType& newEntry) = 0;
virtual bool dequeue() = 0;
virtual ItemType peekFront() const = 0;
virtual ~QueueInterface() { }
};
#endif

10_QUEUES 18
Homework 3
Due Sunday, 3/14 at midnight!

Pairs:
By Wednesday 3/3 at midnight

https://docs.google.com/spreadsheets/d/16ajAFsPQr6da47BlOUcH
WKpzJ0nNMQTmmxE0EHSNipw/edit?usp=sharing

10_QUEUES 19
Next Class
Queues
Textbook:
• Chapters 6
Internet:
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-31-queue-adt.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-32-using-the-adt-queue.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-33-implementation-adt-queue
.pdf

10_QUEUES 20

You might also like