Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting queues right … finally (?)

Similar presentations


Presentation on theme: "Getting queues right … finally (?)"— Presentation transcript:

1 Getting queues right … finally (?)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 27: Getting queues right … finally (?)

2 Announcements/reminders
Program 3 due today Program 4 to be posted, due some time before end of semester Today’s lecture: fixing all of the queue stuff Array-based queues Outlining the issues one last time Going through one specific implementation Linked queues 7/5/2019 Data Structures: Lecture 27

3 Data Structures: Lecture 27
Review: Queue ADT Queue is first-in, first-out (FIFO) data structure Definition Ordered collection of data items Items only removed (or read) from the front Items only added to the back Operations Construction (start with empty queue) Check if queue is empty Enqueue: add data to the back of the queue Dequeue: remove data from the front of the queue Read item at front of queue 7/5/2019 Data Structures: Lecture 27

4 Review: Queue implementation
Like stack, very similar to list ADT Same basic idea: sequence of items Restrict access only to front, back element What data do we need in a queue object? Actual queue storage Locations of front and back elements Capacity (if array-based) Implementation options Array-based queue Linked queue 7/5/2019 Data Structures: Lecture 27

5 Array-based queue issues, part 1
Where should front, back of queue be in array? Consider where you add, remove items Add to back  “highest indexed element” Remove from front  “lowest indexed element” One implementation Increment “back” when you enqueue Increment “front” when you dequeue What’s problem? “Walk off” end of array Solution: array as circular buffer Use modulo arithmetic when incrementing index, e.g. front = (front + 1) % capacity 7/5/2019 Data Structures: Lecture 27

6 Array-based queue issues, part 2
What’s with the quotes on the last slide? Add to back  “highest indexed element” Remove from front  “lowest indexed element” Two reasons If array is circular, could have back < front Some flexibility in how you actually treat front/back indexes (and there’s no “right” way) 7/5/2019 Data Structures: Lecture 27

7 Array-based queue issues, part 2 (cont.)
Approach #1: index == position to operate on next So, front = location to read next Refers to spot containing data (unless empty) Initialize to 0 Back = location to write next Refers to spot after data was last written In both cases: operate on position, then inc. index “operate on position” = write (enqueue) / read (dequeue) 3 2 back front Enqueue 7: 7 7/5/2019 Data Structures: Lecture 27

8 Array-based queue issues, part 2 (cont.)
Approach #2: index == position most recently operated on So, front = location that was just read Refers to spot behind data to be read next Initialize to -1 or capacity - 1 Back = location that was just written Refers to spot behind data to be written next Initialize to -1 or capacity – 1 In both cases, inc. index, then operate on position 3 2 front back Enqueue 7: 7 7/5/2019 Data Structures: Lecture 27

9 Array-based queue issues, part 3
How do we check for full/empty conditions? Need something extra Leave one spot in array empty Track full/empty with separate variable Unsigned var with number of slots used (or unused) Boolean for empty condition (or full) Examples that follow assume: Initially, front = back = 0 Therefore, index = position to operate on next 7/5/2019 Data Structures: Lecture 27

10 Array-based queue issues, part 3 (cont.)
Approach #1: Keep one array spot empty Empty condition: front == back Full condition (tested when enqueueing): front == (back + 1) % capacity Empty  Enqueue 7 7 front back Prior state  Enqueue 13, 52, 31, 10 13 52 31 10 Prior state  Dequeue 5 times front / back 7/5/2019 Data Structures: Lecture 27

11 Array-based queue issues, part 3 (cont.)
Approach #2: Track full/empty (let’s use unsigned nVals) Init to 0, increment on enqueue, decrement on dequeue Empty condition: nVals == 0 Full condition: nVals == capacity Empty  Enqueue 7, so nVals = 1 7 front back Prior state  Enqueue 13, 52, 31, 10, 15, so nVals = 6 13 52 31 10 15 front / back Prior state  Dequeue 6 times, so nVals = 0 7/5/2019 Data Structures: Lecture 27

12 Array-based queue examples
Describe, in code or pseudo-code, how to write the following queue member functions Default constructor: Queue(unsigned maxSize = 1024); Destructor: ~Queue(); Check if empty: bool isEmpty(); Add new element: void enqueue(const QueueElement &val); Remove front element: void dequeue(); Retrieve front element: QueueElement getFront(); Assume “QueueElement” defined using typedef to represent data stored in queue Assume queue has following members QueueElement *list int front, back Locations of front & back elements unsigned cap Max size of array This implementation will leave one spot empty I think tracking # of values is easier, which is why we won’t cover it in class! 7/5/2019 Data Structures: Lecture 27

13 Array-based queue example solutions
Queue::Queue(unsigned maxSize) : front(0), back(0), cap(maxSize) { list = new QueueElement[cap]; } Queue::~Queue(unsigned maxSize) { delete [] list; bool Queue::empty() const { return (front == back); void Queue::enqueue(const QueueElement &val) { int newBack = (back + 1) % cap; if (newBack != front) { // Queue isn’t full list[newBack] = val; myBack = newBack; else // handle error appropriately 7/5/2019 Data Structures: Lecture 27

14 Array-based queue example solutions
void Queue::dequeue() { if (!empty()) front = (front + 1) % capacity; else // handle error } QueueElement Queue::front() { return list[front]; else { // return garbage value cout << "Can’t read from empty queue\n"; return list[capacity-1]; 7/5/2019 Data Structures: Lecture 27

15 Data Structures: Lecture 27
Linked queues Implementation similar to linked list Like linked stack, insert/delete functions O(1) since each only accesses one end of list … … as long as you have pointers to each end Queue class Needs two pointers—front and back of queue We’ll cover some details of this next lecture 7/5/2019 Data Structures: Lecture 27

16 Data Structures: Lecture 27
Circular linked queue One other option for linked queue: circular linked list Maintain pointer only to last node: back of queue Where’s front of queue? last->next 7/5/2019 Data Structures: Lecture 27

17 Data Structures: Lecture 27
Final notes Next time Linked queues Linked lists Reminders: Program 3 due today Program 4 to be posted, due TBD 7/5/2019 Data Structures: Lecture 27


Download ppt "Getting queues right … finally (?)"

Similar presentations


Ads by Google