Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 116/504 – Intro. To Computer Science for Majors II"— Presentation transcript:

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

2 Order Removed from Stack?
first last 1 elementData size needed

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

4 Order Removed from Stack?
first last 1 elementData size needed

5 Order Removed from Stack?
first last elementData size needed

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

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

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

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

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

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

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

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

14 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

15 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

16 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();

17 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();

18 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

19 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

20 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

21 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

22 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

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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

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

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

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

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

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

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

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

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

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

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

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

54 Java's Queue

55 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

56 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>();

57 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>();

58 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>();

59 Prefer Queue to Specific Class?
Yes1 1

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

61 Java's Queue Java declared Queue as an interface

62 Java's Queue Java declared Queue as an interface

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

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

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

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

67 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…

68 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:

69 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

70 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

71 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

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

73 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

74 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

75 Implementing Queue

76 Implementing Queue

77 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

78 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

79 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

80 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

81 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

82 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

83 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

84 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

85 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

86 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

87 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

88 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)

89 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

90 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

91 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

92 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

93 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

94 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

95 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

96 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

97 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

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

99 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…

100 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)

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


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

Similar presentations


Ads by Google