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