LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.

Slides:



Advertisements
Similar presentations
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.
Advertisements

© 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.
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 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
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Implementing and Using Stacks
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.
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
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.
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.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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?
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.
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.
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.
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.,
Parasol Lab, Dept. CSE, Texas A&M University
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
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 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
Lecture 9: Stack and Queue
Stacks and Linked Lists
Presentation transcript:

LECTURE 26: QUEUES CSC 212 – Data Structures

Using Stack

 Great for Pez dispensers, calculators,& methods  All of these use only the last item added  Do not complain when later additions served first  Many situations use 1 item, but in different order  Take reservations in order they are received  Cards (usually) dealt from top of the deck Stack Limitations

 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 element at front of structure  Also includes method to peek in at first element  front() returns element at front without removing Queue ADT

public interface Queue extends Collection { public E front() throws EmptyQueueException; public E dequeue() throws EmptyQueueException; public void dequeue(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 the start of a QUEUE…  …while STACK removes element at the end Queue Interface

Stacks vs. Queues  Access data with Stack in LIFO order  LIFO  L ast I n- F irst O ut  Completely unfair (unless you are always late) ‏  Data accessed in Queue using FIFO order  FIFO  F irst I n- F irst O ut  Better resembles lines at bank, Sabres game, airports

 Among the most fundamental data types  Selecting application to run in modern OS  Handling and processing network traffic  Message checking in IM chat servers  Accepting ticket orders for Sabres game  Other data structures also rely upon a Queue  Key feature in many algorithms Queue Applications

 “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  More straightforward, 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

 STACKS are easy for arrays: only 1 end “moves”  Can always find Stack’s bottom at index 0  QUEUES are harder, because both ends move  dequeue removes element from the front  Add element to back with calls to enqueue  Ends of a array-based QUEUE like clock time Circular Access q r f r rrr r r r

f r Array-based Queue  Two fields track front and rear of QUEUE f equals index of front element r holds index immediately after rear element  Add & remove elements at opposite ends of array  Uses circular access to the array  Like a clock –when end (12) reached we loop to start  Array must be empty at index in r q r r f r r r

Array-based Queue Operations  Based on clock math  Uses mod (remainder) ‏  Java expressed as %  How mod works: 4 % 3 = 1 3 % 4 = 3 6 % 6 = 0 12 % 6 = 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

 Finish week #9 assignment  Due by 5PM tomorrow  Continue programming assignment #3  Messages are not always sent to everyone!  Read section 5.3 in book before class  Discusses merger of Stack & Queue ADT  Implement this Deque ADT with array  Could instead implement it with a linked-list  Make also sorts of bad jokes about porches Before Next Lecture…