Professional Documents
Culture Documents
02 Stacks Queues
02 Stacks Queues
Teaching Assistants:
Aaron Johnston Ethan Knutson Nathan Lipiarski
Amanda Park Farrell Fileas Sam Long
Anish Velagapudi Howard Xiao Yifan Bai
Brian Chan Jade Watkins Yuma Tou
Elena Spasova Lea Quan
L02: Stacks and Queues CSE373, Winter 2020
Announcements: 1 of 2
❖ If you’re enrolled and don’t have your
Gitlab/Piazza/Gradescope accounts yet, email cse373-staff@cs
2
L02: Stacks and Queues CSE373, Winter 2020
Announcements: 2 of 2
❖ Homework 1 is released!
3
L02: Stacks and Queues CSE373, Winter 2020
Lecture Outline
❖ ADTs and Interfaces; Data Structures and Subtypes
❖ Stack ADT
❖ Queue ADT
4
L02: Stacks and Queues CSE373, Winter 2020
5
L02: Stacks and Queues CSE373, Winter 2020
6
L02: Stacks and Queues CSE373, Winter 2020
7
L02: Stacks and Queues CSE373, Winter 2020
Lecture Outline
❖ ADTs and Interfaces; Data Structures and Subtypes
❖ Stack ADT
❖ Queue ADT
8
L02: Stacks and Queues CSE373, Winter 2020
pollev.com/uwcse373
A. Resizable array
B. Linked nodes
C. Both are about the same
D. I’m not sure …
9
L02: Stacks and Queues CSE373, Winter 2020
10
L02: Stacks and Queues CSE373, Winter 2020
Number of steps
(e.g. 23) is in O(1).
(runtime)
❖ A method that takes a linear
O(N)
number of steps (e.g. 4N + 3) is
in O(N).
O(1)
12
L02: Stacks and Queues CSE373, Winter 2020
Lecture Outline
❖ ADTs and Interfaces; Data Structures and Subtypes
❖ Stack ADT
❖ Queue ADT
13
L02: Stacks and Queues CSE373, Winter 2020
Stack ADT
❖ Stack: an ADT representing an ordered sequence of elements
whose elements can only be added/removed from one end.
▪ Corollary: has “last in, first out” semantics (LIFO)
▪ The end of the stack that we operate on is called the “top”
▪ Two methods:
• void push(Item i)
• Item pop()
• (notably, there is no get() method)
14
L02: Stacks and Queues CSE373, Winter 2020
pollev.com/uwcse373
A. Resizable array
B. Linked nodes
C. Both are about the same
D. I’m not sure …
15
L02: Stacks and Queues CSE373, Winter 2020
ArrayStack
❖ State
Item[] data; push(3);
int size; push(4);
pop();
❖ Behavior push(5);
▪ push()
▪ Resize data array if necessary
push
▪ Assign data[size] = item 0 1 2 3
▪ Increment size 3 4
5
▪ Note: this is ArrayList.addBack()
▪ pop() size 0
2
1 pop
• Return data[size]
• Decrement size
• Note: this is ArrayList.removeBack()
LinkedStack
❖ State
Node top;
push(3);
❖ Behavior push(4);
▪ push() pop();
▪ Create a new node linked to top’s current
value
top
▪ Update top to new node
▪ Increment size push
Lecture Outline
❖ ADTs and Interfaces; Data Structures and Subtypes
❖ Stack ADT
❖ Queue ADT
18
L02: Stacks and Queues CSE373, Winter 2020
19
L02: Stacks and Queues CSE373, Winter 2020
Queue ADT
❖ Queue: an ADT representing an ordered sequence of elements,
whose elements can only be added to one end and removed
from the other end.
▪ Corollary: has “first in, first out” semantics (FIFO)
▪ Two methods:
• void enqueue(Item i)
• Item dequeue()
• (notably, there is no get() method)
20
L02: Stacks and Queues CSE373, Winter 2020
▪ ArrayList.addBack() 4 5 6 7
▪ dequeue()
remove size 0
4
3
2
1
▪ ArrayList.removeFront()
❖ Runtime?
▪ enqueue()
▪ dequeue()
21
L02: Stacks and Queues CSE373, Winter 2020
pollev.com/uwcse373
A. Linear / Linear
B. Linear / Constant
C. Constant / Linear
D. Constant / Constant
E. I’m not sure …
22
L02: Stacks and Queues CSE373, Winter 2020
23
L02: Stacks and Queues CSE373, Winter 2020
ArrayQueue (v2)
24
L02: Stacks and Queues CSE373, Winter 2020
pollev.com/uwcse373
25
L02: Stacks and Queues CSE373, Winter 2020
LinkedQueue (v1)
❖ State
Node qback; // front of list
// is back of
// queue
qback
❖ Behavior
add
▪ enqueue()
▪ LinkedList.addLast() 4 3
▪ dequeue()
remove
▪ LinkedList.removeFront()
❖ Runtime?
▪ enqueue()
▪ dequeue()
Notice how ArrayQueue is a “rebranded ArrayList”!
26
L02: Stacks and Queues CSE373, Winter 2020
LinkedQueue (v2)
4 3
remove
qfront
27
L02: Stacks and Queues CSE373, Winter 2020
Lecture Outline
❖ ADTs and Interfaces; Data Structures and Subtypes
❖ Stack ADT
❖ Queue ADT
28
L02: Stacks and Queues CSE373, Winter 2020
ArrayList LinkedList
addFront linear constant
removeFront linear constant
addBack constant* linear
removeBack constant linear
get(idx) const linear
put(idx) linear linear
* constant for most invocations
29
L02: Stacks and Queues CSE373, Winter 2020
❖ Queue (FIFO):
Array Queue (v2) LinkedQueue (v2)
enqueue constant* constant
dequeue constant constant
* constant for most invocations
30
L02: Stacks and Queues CSE373, Winter 2020
tl;dr
❖ More than one concrete data structure can implement an ADT
▪ Eg: ArrayList and LinkedList both implement List ADT
31