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.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Chapter 24 Lists, Stacks, and Queues
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.
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.
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 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Problem of the Day  What do you get when you cross a mountain climber and a grape?
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
CSE 373 Data Structures and Algorithms Lecture 2: 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?
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Stacks And Queues Chapter 18.
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.
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.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
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
Welcome to CSCE 221 – Data Structures and Algorithms
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.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Elementary Data Structures
Week 4 - Monday CS221.
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.
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Csc 2720 Data structure Instructor: Zhuojun Duan
Priority Queue.
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:19 AM Queues 11/16/2018 4:19 AM Queues.
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.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Stacks and Linked Lists
Data Structures & Programming
Queues, Deques, and Priority Queues
Presentation transcript:

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 Aid

public interface Stack { public E top() throws EmptyStackException; public E pop() throws EmptyStackException; public void push(E elem); public int size(); public boolean isEmpty(); } Stack Interface

Queue Memory Aid It’s hard writing rhymes with enqueue, dequeue, and front

public interface Queue { public E front() throws EmptyQueueException; public E dequeue() throws EmptyQueueException; public void enqueue(E elem); public int size(); public boolean isEmpty(); } Queue ADT

Stack vs. Queue  Access data with Stack in LIFO order  LIFO  L ast I n- F irst O ut is totally unfair (unless always late) ‏  Data accessed in Queue using FIFO order  FIFO  F irst I n- F irst O ut ensures early bird gets the worm Order read if Stack

 Cannot access both sides of Collection  Transplant waiting lists  Help center phone banks  Grandpa dealing cards for money  Stack only works on one end  All insertions & removals from the Stack ’s top  Queue limits how each side used  Elements can only be added to end  Front allows accessing & removal of elements Still Have Limits

 Pronounced “deck” (like on a house) ‏ DEQUE  Mnemonic for Double Ended QUEue  dequeue ≠ deque and do not sound alike  Structure that provides access to both ends  Combines Stack & Queue concepts  Is also able to add elements to start Deque ADT

public interface Deque { public E getFirst() /* front() */ throws EmptyDequeException; public E removeFirst() /* dequeue() */ throws EmptyDequeException; public E getLast() /* top() */ throws EmptyDequeException; public E removeLast() /* pop() */ throws EmptyDequeException; public void addFirst(E elem); /* Brand new! */ public void addLast(E elem); /* push() & enqueue() */ public int size(); public boolean isEmpty(); } Deque Interface

VIP Lines & Lucky Losers

 Class defines fields aliased to first & last nodes  Doubly-linked list enables O(1) time for all methods  Add elements by adding new Node at end  Update sentinels next/previous to remove element Linked-list based Deque head rear Ø retVal Ø

 DEQUES, like QUEUES, have both ends move  addFirst & removeFirst moves its front  Structure’s end moved by addLast & removeLast  Ends of a array-based DEQUE like clock time  Identical to Queue and how it works, except…  …occasionally need to subtract from index, also Circular Access q r f r rrr r r r f

Array-based DEQUE Operations  Uses property of clock math  Remainder of result is all that matters  But the values sign is also important  -1 % 4 == -1, for example  To get this to work, use a cheap trick  Adding size of the array does not change result (-1 + 6) % 6 (3 + 6) % 6 = 5 % 6= 9 % 6 = 5= 3

 Start week #10 assignment  Due by 5PM next Tuesday  Continue programming assignment #3  Messages are not always sent to everyone!  Read section 6.1 in book before class  What if we wanted to access all the items?  Could we get something like a resizable array?  How do ranks differ from indices? Before Next Lecture…