Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS Data Structures II Review COSC 2006 April 14, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Queue, Deque, and Priority Queue Implementations.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Stacks And Queues Chapter 18.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
CS 367 Introduction to Data Structures Lecture 5.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
April 27, 2017 COSC Data Structures I Review & Final Exam
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
CSE 116/504 – Intro. To Computer Science for Majors II
CSE 373: Data Structures and Algorithms
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
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.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
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 (張智星)
Copyright © Aiman Hanna All rights reserved
Stacks and Linked Lists
Presentation transcript:

Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy to return. To make it easier, the bellboy pockets $2 and gives $1 back to each person. Each person paid $10 and got back $1. So they paid $9 each, totaling $27. The bellboy has $2, totaling $29. Where is the remaining dollar?

Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy to return. To make it easier, the bellboy pockets $2 and gives $1 back to each person. Each person paid $10 and got back $1. So they paid $9 each, totaling $27. The bellboy has $2, totaling $29. Where is the remaining dollar?  $25 (manager) + $2 (bellboy) + $3 (customers) = $30

CSC 212 – Data Structures

Using Stack

Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only  Do not complain when later additions served first  Many situations use items in order added  Checker at Wegmans & others prevent cutting in line  Use first-come, first-served getting food at dining hall

Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only  Do not complain when later additions served first  Many situations use items in order added  Checker at Wegmans & others prevent cutting in line  Use first-come, first-served getting food at dining hall

Stack Limitations  Great for web browsers, JVMs,& methods  All of these use most recent item added only  Do not complain when later additions served first  Many situations use items in order added  Checker at Wegmans & others prevent cutting in line  Use first-come, first-served getting food at dining hall

 Collection ’s operations are part of Queue  As in Stack, declares size() & isEmpty()  Add & remove elements using 2 methods  Element gets added to end with enqueue(elem)  dequeue() removes front element in structure  Also includes method to peek in at first element  front() returns element at front without removing Queue ADT

Queue Interface public interface Queue extends Collection { public E front() throws EmptyQueueException; public E dequeue() throws EmptyQueueException; public void enqueue(E element); }  Very similar to Stack interface  Defines specific methods to add, remove, & view data  Holds many elements, but can access only one  Stack & Queue always add to the end  Remove element at start of this Q UEUE …  …while S TACK removes element at the end

Stacks vs. Queues

 “Obvious” implementation uses an array  Must consume a constant amount of space  enqueue() throws exception when it lacks space  Instead write linked list-based implementation  Singly-, doubly-, or circular-linked list could work  Size of the Queue grows & shrinks as needed  No additional exceptions needed, but is it slower? Queue Implementation

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear elem

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear elem

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear elem

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear elem

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear retVal

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear retVal

 Class defines fields aliased to first & last nodes  head & rear often used as fields’ names (creative!)  enqueue element by adding new Node after rear  Set head to next Node in list to dequeue element Linked-list based Queue head rear retVal

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 S TACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  Q UEUES are harder, because both ends move  dequeue calls will remove element at front  Add element to back with calls to enqueue  Ends of a array-based Q UEUE like clock time Circular Access q r f

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f Array-based Queue q r

 Two fields track front and rear of Q UEUE f equals index of front element r holds index immediately after rear element  Add & remove elements from opposite ends  Uses circular access to the array  Works like clock: when end (12) reached, loop to start Array must be empty at index in r f f Array-based Queue q r q r

Array-based Queue Operations  Based on clock math  Uses mod (remainder)  Java expressed mod as %  How mod works: 0 % 3 = 0 1 % 3 = 1 2 % 3 = 2 3 % 3 = 0 Algorithm size() N  q.length return (N  f + r) mod N

Array-based Queue Operations Algorithm enqueue(e) if size() = q.length  1 then throw FullQueueException else q[r]  e r  (r + 1) mod q.length q rf Algorithm dequeue() if isEmpty() then throw EmptyQueueException else retVal  q[f] f  (f + 1) mod q.length return retVal

Your Turn  Get into your groups and complete activity

For Next Lecture  No weekly assignment this week. Why, you ask? Stacks & Queues  Friday’s class is quiz covering Stacks & Queues  Midterm #2  Midterm #2 will be in class next Wednesday