0% found this document useful (0 votes)
44 views33 pages

Queues: Erin Keith

This document discusses different implementations of queues, including linked and array-based approaches. It begins by covering linked queue implementation, showing how to track the front and back pointers and implement enqueue and dequeue. It then discusses array-based queues and how to treat the array as circular to avoid overflow issues. Finally, it briefly introduces priority queues as another queue variation where items are dequeued based on priority rather than order. The document assigns reading material and homework for students to design queue implementations.

Uploaded by

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

Queues: Erin Keith

This document discusses different implementations of queues, including linked and array-based approaches. It begins by covering linked queue implementation, showing how to track the front and back pointers and implement enqueue and dequeue. It then discusses array-based queues and how to treat the array as circular to avoid overflow issues. Finally, it briefly introduces priority queues as another queue variation where items are dequeued based on priority rather than order. The document assigns reading material and homework for students to design queue implementations.

Uploaded by

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

Queues

ERIN KEITH

11_QUEUES_LINKED 1
Topics
1. Linked Based
2. Priority Queue
3. Array Based
4. Homework 3

11_QUEUES_LINKED 2
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 3
Linked Implementation
Now we must keep track of “front” and “back”.

Most easily done with a pointer for each!


/ /
front ptr back ptr

11_QUEUES_LINKED 4
Linked Implementation
#ifndef LINKED_QUEUE
#define LINKED_QUEUE

#include "QueueInterface.h"
#include "Node.h"

template<class ItemType>
class LinkedQueue : public QueueInterface<ItemType> {
private:
Node<ItemType>* frontPtr;
Node<ItemType>* backPtr;

11_QUEUES_LINKED 5
Linked Implementation
public:
LinkedQueue();

bool isEmpty() const;


bool enqueue(const ItemType& newEntry);
bool dequeue();
ItemType peekFront() const;

~LinkedQueue();
};
#include "LinkedQueue.cpp"
#endif

11_QUEUES_LINKED 6
Linked Implementation
#include "LinkedQueue.h"

template<class ItemType>
LinkedQueue<ItemType>::LinkedQueue(){
frontPtr = nullptr;
backPtr = nullptr;
}

11_QUEUES_LINKED 7
Linked Implementation
template<class ItemType>
bool LinkedQueue<ItemType>::isEmpty() const{
return frontPtr == nullptr;
}

11_QUEUES_LINKED 8
Queue Use
LinkedQueue<string> groceryQueue;

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

Items
milk
cereal
eggs

10_QUEUES 9
Linked Implementation
Enqueue:

front ptr back ptr

data data data /

11_QUEUES_LINKED 10
Linked Implementation
Enqueue:
◦ Create new node
◦ Point back next to new node
◦ Point back to new node

front ptr back ptr

data data data /

11_QUEUES_LINKED 11
Linked Implementation
Enqueue:
◦ Create new node
◦ Point back next to new node
◦ Point back to new node

front ptr back ptr new node ptr

data data data / data /

11_QUEUES_LINKED 12
Linked Implementation
Enqueue:
◦ Create new node
◦ Point back next to new node
◦ Point back to new node

front ptr back ptr new node ptr

data data data data /

11_QUEUES_LINKED 13
Linked Implementation
Enqueue:
◦ Create new node
◦ Point back next to new node
◦ Point back to new node

front ptr back ptr new node ptr

data data data data /

11_QUEUES_LINKED 14
Linked Implementation
Enqueue (empty queue):

front ptr back ptr


/ /

11_QUEUES_LINKED 15
Linked Implementation
Enqueue (empty queue):
◦ Create new node
◦ Point back to new node
◦ Point front to new node

front ptr back ptr


/ /

11_QUEUES_LINKED 16
Linked Implementation
Enqueue (empty queue):
◦ Create new node
◦ Point back to new node
◦ Point front to new node

front ptr back ptr


/ /

new node ptr


data /

11_QUEUES_LINKED 17
Linked Implementation
Enqueue (empty queue):
◦ Create new node
◦ Point back to new node
◦ Point front to new node

front ptr back ptr

new node ptr


data /

11_QUEUES_LINKED 18
Linked Implementation
template<class ItemType>
bool LinkedQueue<ItemType>::enqueue(const ItemType& newEntry){
Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry);

if(isEmpty()){
frontPtr = newNodePtr;
}
else{
backPtr->setNext(newNodePtr);
}
backPtr = newNodePtr;
newNodePtr = nullptr;

return true;
}

11_QUEUES_LINKED 19
Linked Implementation
template<class ItemType>
bool LinkedQueue<ItemType>::Dequeue(){
bool canDq = !isEmpty();

if(canDq)){
Node<ItemType>* nodeToDeletePtr = frontPtr;

frontPtr = frontPtr->getNext();

nodeToDeletePtr->setNext(nullptr);
delete nodeToDeletePtr;

nodeToDeletePtr = nullptr;
}

return canDq;
}

11_QUEUES_LINKED 20
Linked Implementation
template<class ItemType>
ItemType LinkedQueue<ItemType>::peekFront(){
bool canPeek = !isEmpty();

if(canPeek){
return frontPtr->getItem();
}
else{
throw "empty queue";
}
}

11_QUEUES_LINKED 21
Linked Implementation
template<class ItemType>
ItemType LinkedQueue<ItemType>::~LinkedQueue(){
while(dequeue());
}

11_QUEUES_LINKED 22
List Implementation
https://www.autonomousrobotslab.com/uploads/5/8/4/4/58449511/cs302
-82-implementation-of-queues.pdf

11_QUEUES_LINKED 23
Array Implementations
◦ Keep track of front and back
◦ Add?

◦ Remove?

11_QUEUES_LINKED 24
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?

11_QUEUES_LINKED 25
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front

11_QUEUES_LINKED 26
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front
◦ Problem: the queue is full when back equals
DEFAULT_CAPACITY-1 BUT this may happen without
the array being “full”.
Index 0 1 2 3
Item Apples Cheese

11_QUEUES_LINKED 27
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front
◦ Problem: the queue is full when back equals
DEFAULT_CAPACITY-1 BUT this may happen without
the array being “full”.
◦ Treat array as circular!

11_QUEUES_LINKED 28
Array Implementations
◦ Treat array as circular!
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front

◦ When either front or back advances past


DEFAULT_CAPACITY-1, then wrap around to 0.

11_QUEUES_LINKED 29
Priority Queue ADT
Includes qualities of a Queue ADT:
• Items are dequeued following a priority order
• Data includes an additional value: priority
• Same interface!
• same functions, different behavior

11_QUEUES_LINKED 30
Priority Queue ADT
Operations
• isEmpty(): boolean
• enqueue(newEntry: ItemType): boolean
• inserts item based on priority
• dequeue(): boolean
• removes highest priority item
• peekFront(): ItemType
• returns copy of highest priority item

10_QUEUES 31
Homework 3
◦ Update the pairs worksheet TONIGHT!
◦ Link is on WebCampus on Queues Page
◦ Read book from section 13.3 - 13.5
◦ This weekend with your partner, produce a design plan
◦ Included classes
◦ Main driver design
◦ Task allocation
◦ DUE SUNDAY AT MIDNIGHT

11_QUEUES_LINKED 32
Next Class
Complexity
Textbook:
• Chapters 10
Internet:
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-51-measuring-algorithm-effic
iency-v2.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-52-understanding-big-o-notat
ion.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-53-formal-definitions.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
11_QUEUES_LINKED 33

You might also like