CSE 116/504 – Intro. To Computer Science for Majors II

Slides:



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

CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Stacks, Queues, and Deques
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
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.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
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.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
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.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs
Stacks II David Lillis School of Computer Science and Informatics
Week 4 - Monday CS221.
QueueStack CS1020.
Stacks and Queues.
Queues Rem Collier Room A1.02
CSE 116/504 – Intro. To Computer Science for Majors II
Heaps And Priority Queues
Stacks.
Lecture 23: Doubly Linked List
CSE 373: Data Structures and Algorithms
Queues Queues Queues.
Stacks and Queues.
Top Ten Words that Almost Rhyme with “Peas”
HW-6 Deadline Extended to April 27th
Topic 16 Queues "FISH queue: n.
Stacks and Queues.
Topic 16 Queues Adapted from Mike Scott’s materials.
Building Java Programs
structures and their relationships." - Linus Torvalds
THURSDAY, OCTOBER 17 IN LAB
Topic 16 Queues "FISH queue: n.
Stacks and Queues.
Building Java Programs
Building Java Programs
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CSE 214 – Computer Science II Stacks
Stacks and Queues CLRS, Section 10.1.
slides created by Alyssa Harding
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
Chapter 4 Queues.
More Data Structures (Part 1)
Lecture 2: Stacks and Queues
slides created by Alyssa Harding
Generics, Stack, Queue Based on slides by Alyssa Harding
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Lecture 9: Stack and Queue
Presentation transcript:

CSE 116/504 – Intro. To Computer Science for Majors II Lecture 26: Queues

Order Removed from Stack? first last 1 elementData size needed

Order Removed from Stack? first last Convince your neighbor that your answer is correct elementData size needed

Order Removed from Stack? first last 1 elementData size needed

Order Removed from Stack? first last elementData size needed

Order Removed from Stack? first last Stack uses Last-In, First-Out order: elementData size needed

Order Removed from Stack? first last Stack uses Last-In, First-Out order: elementData size needed

Order Removed from Stack? first last Stack uses Last-In, First-Out order: elementData size needed

Order Removed from Stack? first last Stack uses Last-In, First-Out order: elementData size needed

Order Removed from Stack? first/last Stack uses Last-In, First-Out order: elementData size needed

Order Removed from Stack? Stack uses Last-In, First-Out order: elementData size needed

Order Removed from Stack? Stack uses Last-In, First-Out order: elementData size needed

Stack Key Concept Last-In, First-Out Only top available Uses O(1) Time

Stack Method Definition Defines two vital methods… push(obj) add obj onto top of stack pop() remove & return item on top of stack … an accessor method… peek() return top item (but do not remove it) … informational methods… isEmpty() returns if instance is empty size() returns obj's distance from stack top And List's methods

Java's Stack Class part of Java's history – defined since Java 1.0 Predates Collection (interface added in Java 1.2) Started from HORRIBLE choice to extend Vector DUMB decisions made with Collection & List

Java's Stack Methods Methods That Should Exist They That Shall Not Be Named E push(E e); E pop(); E peek(); int size(); boolean isEmpty(); Iterator<E> iterator();

Java's Stack Methods Methods That Should Exist They That Shall Not Be Named E push(E e); E pop(); E peek(); int size(); boolean isEmpty(); Iterator<E> iterator();

Stack Limitations Great for Pez dispensers, DJs,& undo menu All of these track our memory – favor most recent item Do not complain when ignored for newer elements

Stack Limitations Great for Pez dispensers, DJs,& undo menu All of these track our memory – favor most recent item Do not complain when ignored for newer elements

Stack Limitations Great for web browsers, DJs,& undo menu All of these track our memory – favor most recent item Do not complain when ignored for newer elements

Stack Limitations Great for web browsers, DJs,& undo menu All of these track our memory – favor most recent item Do not complain when ignored for newer elements Latest and greatest not always best ordering Last-come, first-served unfair and violates decency Whenever people queue up, complaints WILL happen

Single Point Access ADTs Stack Queue Adds to top Access element at top Removes from top Last-In, First-Out order uses most-recently added Adds to back Access element at front Removes from front First-In, First-Out order uses longest waiting

First-In, First-Out Only front usable Fairness Ensues Queue Key Concept #1 First-In, First-Out Only front usable Fairness Ensues

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile Stack of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile Stack of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile Stack of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile Stack of books on a table Collection of Crows, Owls, or Bats

Stack, Queue, or Other Checkout line at the grocery store Undo actions in Eclipse Waiting emergency room patients Web page requests for a webserver Equation calculator Mouse movements processed by driver Pile Stack of books on a table Collection of Crows, Owls, or Bats

Order Removed from Queue? first last 1 Need implementation to know

Order Removed from Queue? first last Convince your neighbor that your answer is correct Need implementation to know

Order Removed from Queue? first last 1 Need implementation to know

Order Removed from Queue? first last Need implementation to know Queue uses First-In, First-Out order

Order Removed from Queue? first last Queue uses First-In, First-Out order: Need implementation to know

Order Removed from Queue? first last Queue uses First-In, First-Out order: Need implementation to know

Order Removed from Queue? first last Queue uses First-In, First-Out order: Need implementation to know

Order Removed from Queue? first last Queue uses First-In, First-Out order: Need implementation to know

Order Removed from Queue? first/last Queue uses First-In, First-Out order: Need implementation to know

Order Removed from Queue? Queue uses First-In, First-Out order: Need implementation to know

Order Removed from Queue? Queue uses First-In, First-Out order: Need implementation to know

Java's Queue

Java's Queue Interface Like List (but not Stack), Queue an interface Declares methods which will be implemented For methods, specifies outputs & exceptions possible Needs implementing classes to actually be useful

Queue Implementations Java includes many Queue implementations Strengths & goals differ with each of these classes Still Java: cannot assign one Queue type to another ArrayDeque<Long> ad = new LinkedList<Long>(); LinkedList<Long> link = new ArrayDeque<Long>(); ArrayBlockingQueue<Long> abq = new ArrayDeque(); AbstractQueue<Long> aq = new ArrayBlockingQueue(); ConcurrentLinkedQueue<Long> clq=new LinkedList<Long>();

Queue Implementations Java includes many Queue implementations Strengths & goals differ with each of these classes But all of the implementations usable as Queue ArrayDeque<Long> ad = new LinkedList<Long>(); LinkedList<Long> link = new ArrayDeque<Long>(); ArrayBlockingQueue<Long> abq = new ArrayDeque(); AbstractQueue<Long> aq = new ArrayBlockingQueue(); ConcurrentLinkedQueue<Long> clq=new LinkedList<Long>();

Queue Implementations Java includes many Queue implementations Strengths & goals differ with each of these classes But all of the implementations usable as Queue Queue<Long> ad = new LinkedList<Long>(); Queue<Long> link = new ArrayDeque<Long>(); Queue<Long> abq = new ArrayDeque(); Queue<Long> aq = new ArrayBlockingQueue(); Queue<Long> clq=new LinkedList<Long>();

Prefer Queue to Specific Class? Yes1 1

Programming Key Concept #1 Declare variables, fields, params as interface type* * When possible

Java's Queue Java declared Queue as an interface

Java's Queue Java declared Queue as an interface

Java's Queue Java declared Queue as an interface Traditional method names not used in declarations

Java's Queue Java declared Queue as an interface Traditional method names not used in declarations

Java's Queue Java declared Queue as an interface Traditional method names not used in declarations Instead uses Java method names, but results different

Java's Queue Java declared Queue as an interface Traditional method names not used in declarations Instead uses Java method names, but results different

int size() boolean isEmpty() Queue Methods Action Traditional Java Queue Add element enqueue(E e) boolean add(E e) Remove element E dequeue() E remove() Access (but not remove) element E peek() E element() # elements & if it has elements int size() boolean isEmpty() Matches up all the methods so far…

Java's Queue Definition boolean add(E e); boolean offer(E e); E remove(); E poll(); E element(); E peek(); // size(),isEmpty(),iterator() & other boring ones Picture used by permission of Alex E. Proimos under a Creative Commons 2.0 license: https://commons.wikimedia.org/wiki/File:Paris_Tuileries_Garden_Facepalm_statue.jpg

Java's Queue Definition boolean add(E e); boolean offer(E e); E remove(); E poll(); E element(); E peek(); // size(),isEmpty(),iterator() & other boring ones

Java's Queue Definition boolean push(E e); boolean offer(E e); E remove(); E poll(); E element(); E peek(); // size(),isEmpty(),iterator() & other boring ones

peek() Method Stack Queue Top element returned Access only, no removal When Stack is empty, Exception thrown First element returned Access only, no removal When Queue is empty, null returned

When in doubt, ask: What would Java do? Programming Pro Tip #2 When in doubt, ask: What would Java do?

DON'T do that When in doubt, ask: What would Java do? Programming Pro Tip #2 When in doubt, ask: What would Java do? DON'T do that

Java's Queue Definition boolean add(E e); boolean offer(E e); E remove(); E poll(); E element(); E peek(); // size(),isEmpty(),iterator() & other boring ones

Implementing Queue

Implementing Queue

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 1 1 2 3 4 5 6 7 LinkedList Queue head tail size = 1

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 2 1 2 3 4 5 6 7 LinkedList Queue head tail size = 2

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 3 1 2 3 4 5 6 7 LinkedList Queue head tail size = 3

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 4 1 2 3 4 5 6 7 LinkedList Queue head tail size = 4

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 3 1 2 3 4 5 6 7 LinkedList Queue head tail size = 3

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 3 1 2 3 4 5 6 7 LinkedList Queue head tail size = 3

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 3 1 2 3 4 5 6 7 LinkedList Queue head tail size = 3

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 3 1 2 3 4 5 6 7 LinkedList Queue head tail size = 3

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 2 1 2 3 4 5 6 7 LinkedList Queue head tail size = 2

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 2 1 2 3 4 5 6 7 LinkedList Queue head tail size = 2

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions ArrayList Queue Backing store size = 2 1 2 3 4 5 6 7 LinkedList Queue head tail size = 2

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions Queue's add at size() & remove from index 0 Shifting inevitable when using an ArrayList Method ArrayList LinkedList w/ tail remove(i) O(1) (if i=size-1) O(n) (generally) O(1) (at either end) get(i) O(1) add(i,e) O(1) (at either end)

Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 1 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array

Convince your neighbor that your answer is correct Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } Convince your neighbor that your answer is correct 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array

Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 1 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array

Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array

Queue uses First-In, First-Out ordering Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array Queue uses First-In, First-Out ordering

Queue uses First-In, First-Out ordering Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array Queue uses First-In, First-Out ordering

Queue uses First-In, First-Out ordering Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array Queue uses First-In, First-Out ordering

element() returns and does not remove Fill In the Blanks @Test public void testLinkedListStart(){ Queue<Integer> lst=new LinkedList<Integer>(); lst.add(17); lst.add(12); assertEquals(_____,lst.element()); assertEquals(_____,lst.remove()); assertEquals(_____,lst.size()); } 12, 17, 1 17, 17, 1 12, 12, 1 Cannot have size without an array element() returns and does not remove first element in Queue

DON'T do that When in doubt, ask: What would Java do? Programming Pro Tip #2 When in doubt, ask: What would Java do? DON'T do that

First-In, First-Out Only front usable Fairness Ensues Queue Key Concept #1 First-In, First-Out Only front usable Fairness Ensues

int size() boolean isEmpty() Queue Methods Action Traditional Java Queue Add element enqueue(E e) boolean add(E e) Remove element E dequeue() E remove() Access (but not remove) element E peek() E element() # elements & if it has elements int size() boolean isEmpty() Matches up all the methods so far…

Implementing Queue Why include LinkedList & not ArrayList? Performance issues tied to Queue method definitions Queue's add at size() & remove from index 0 Shifting inevitable when using an ArrayList Method ArrayList LinkedList w/ tail remove(i) O(1) (if i=size-1) O(n) (generally) O(1) (at either end) get(i) O(1) add(i,e) O(1) (at either end)

For Next Lecture Week #9 homework problems available Continue to be due at 12:45PM on Monday