Getting queues right … finally (?)

Slides:



Advertisements
Similar presentations
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
DATA STRUCTURE & ALGORITHMS
Queues.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Chapter 16 Stack and Queues part2
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ Queues.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Review Array Array Elements Accessing array elements
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
CS505 Data Structures and Algorithms
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
CC 215 Data Structures Queue ADT
Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues.
Function and class templates
Queue, Deque, and Priority Queue Implementations
Stacks, Queues, and Deques
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
Revised based on textbook author’s notes.
CSC 143 Queues [Chapter 7].
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
ADT Queue (Array Implementation)
CE 221 Data Structures and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Dynamic allocation (continued)
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Data Structures & Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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