CSE 143
Lecture 5
Stack, Queue
slides created by Alyssa Harding
http://www.cs.washington.edu/143/
Stacks
• Stacks are a Last In First Out (LIFO) structure
// add value to the top of the stack
void push(E value)
// remove and return the value at the top
E pop()
// return the size
int size()
// return whether the stack is empty
boolean isEmpty()
2
Queues
• Queues are a First In First Out (FIFO) structure
// add value to the back of the queue
void enqueue(E value)
// remove and return the value at the front
E dequeue()
// return the size
int size()
// return whether the queue is empty
boolean isEmpty()
3
Stacks and Queues
• We’re using the Stack<E> and Queue<E> interfaces
• We’re using the ArrayStack<E> and LinkedQueue<E>
implementations
• Stacks and Queues use generics, so we can store any type of
Object in them
4
Stack and Queue client
• We might want to fill a Queue with values:
public static Queue<Integer> fillQueue(int n) {
Queue<Integer> q = new LinkedQueue<Integer>();
for (int i = 0; i < n; i++) {
q.enqueue(i);
}
return q;
}
Note that the variable declaration and return value use the
interface type
5
Stack and Queue client
• We might want to move values from a Queue to a Stack:
public static void queueToStack(
Queue<Integer> q, Stack<Integer> s) {
while ( q.isEmpty() == false ) {
int n = q.dequeue();
s.push(n);
}
}
q.isEmpty() is a already a boolean expression, so this test
has bad “boolean zen”
6
Stack and Queue client
• We might want to move values from a Queue to a Stack:
public static void queueToStack(
Queue<Integer> q, Stack<Integer> s) {
while ( !q.isEmpty() ) {
int n = q.dequeue();
s.push(n);
}
}
Better!
7
Stack and Queue client
• We might want to sum the values in a Queue:
public static int sum(Queue<Integer> q) {
int sum = 0;
while ( !q.isEmpty() ) {
sum += q.dequeue();
}
return sum;
}
But this destroys the Queue and leaves it empty!
8
Stack and Queue client
• We might want to sum the values in a Queue:
public static int sum(Queue<Integer> q) {
int sum = 0;
for (int i = 0; i < q.size(); i++) {
int n = q.dequeue();
sum += n;
q.enqueue(n);
}
return sum;
}
So we store values back in the Queue and loop the correct
number of times.
9
Stack and Queue client
• We might also want to fill a Stack:
public static Stack<Integer> fillStack(int n) {
Stack<Integer> s = new ArrayStack<Integer>();
for (int i = 0; i < n; i++)
s.push(i);
return s;
}
We can copy the Queue code and change it to use a Stack.
10
Stack and Queue client
• We might also want to move values from a Stack to a Queue:
public static void stackToQueue(
Stack<Integer> s, Queue<Integer> q) {
while ( !s.isEmpty() ) {
int n = s.pop();
q.enqueue(n);
}
}
We can copy the Queue code and change it to use a Stack.
11
Stack and Queue client
• We might want to sum the values in a Stack:
public static int sum(Stack<Integer> s) {
int sum = 0;
for (int i = 0; i < s.size(); i++) {
int n = s.pop();
sum += n;
s.push(n);
}
return sum;
}
But if we copy the Queue code, we push and pop the same
value over and over.
12
Stack and Queue client
• We might want to sum the values in a Stack:
public static int sum(Stack<Integer> s) {
int sum = 0;
Queue<Integer> q =
new LinkedQueue<Integer>();
for (int i = 0; i < s.size(); i++) {
int n = s.pop();
sum += n;
Even though we store
q.enqueue(n);
values in a Queue, i
}
increases and s.size()
queueToStack(q,s);
decreases, so we only
return sum;
examine half the values
}
13
Stack and Queue client
• We might want to sum the values in a Stack:
public static int sum(Stack<Integer> s) {
int sum = 0;
Queue<Integer> q =
new LinkedQueue<Integer>();
while ( !s.isEmpty() ) {
int n = s.pop();
Now the sum is correct,
sum += n;
but our resulting Stack
q.enqueue(n);
is reversed! This
}
happens when we
queueToStack(q,s);
transfer from a Stack to
return sum;
Queue and back.
}
14
Stack and Queue client
• We might want to sum the values in a Stack:
public static int sum(Stack<Integer> s) {
int sum = 0;
Queue<Integer> q =
new LinkedQueue<Integer>();
while ( !s.isEmpty() ) {
int n = s.pop();
sum += n;
q.enqueue(n);
}
queueToStack(q,s);
Now both the sum and
the Stack are correct.
stackToQueue(s,q);
queueToStack(q,s);
return sum;
}
15
Stack and Queue client
• We might want to find the minimum value in a Stack:
public static int findMin(Stack<Integer> s) {
int min = 0;
Queue<Integer> q = new LinkedQueue<Integer>();
while ( !s.isEmpty() ) {
int n = s.pop();
if ( n < min )
min = n;
q.enqueue(n);
What if the values in the
}
Stack are all greater than 0?
queueToStack(q,s);
This would still return 0 as
stackToQueue(s,q);
the minimum.
queueToStack(q,s);
return min;
16
Stack and Queue client
• We might want to find the minimum value in a Stack:
public static int findMin(Stack<Integer> s) {
Queue<Integer> q = new LinkedQueue<Integer>();
int min = s.pop();
s.push(min);
while ( !s.isEmpty() ) {
int n = s.pop();
if ( n < min )
min = n;
Instead, we start our
q.enqueue(n);
minimum at a value that is in
}
the Stack.
queueToStack(q,s);
stackToQueue(s,q);
queueToStack(q,s);
17
return min;
}
Preconditions
• Remember the precondition of Stack’s pop method?
• The Stack cannot be empty when we call pop
• Since we call pop in our method without checking that the
Stack isn’t empty, our method has this precondition too
18
Preconditions
• How are we going to deal with this?
• We need to return a value, but what would we return if we
were passed an empty Stack?
• We can comment this precondition, but that doesn’t stop
clients from using our method incorrectly
19
Exceptions
• Java lets us throw exceptions in our methods:
throw new <exception type here>();
• In this case, we want to throw an exception if the method was
given an illegal argument, so we throw an
IllegalArgumentException:
if ( s.isEmpty() )
throw new IllegalArgumentException();
20
Exceptions
• Always comment the type and cause of any exceptions you
throw:
// throws IllegalArgumentException if the
// stack is empty
21
© Copyright 2026 Paperzz