Download presentation
Presentation is loading. Please wait.
1
slides created by Alyssa Harding
CSE 143 Lecture 5 Stack, Queue slides created by Alyssa Harding
2
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()
3
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()
4
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
5
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
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() == false ) { int n = q.dequeue(); s.push(n); } } q.isEmpty() is a already a boolean expression, so this test has bad “boolean zen”
7
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!
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; while ( !q.isEmpty() ) { sum += q.dequeue(); } return sum; } But this destroys the Queue and leaves it empty!
9
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.
10
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.
11
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.
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; 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.
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>(); for (int i = 0; i < s.size(); i++) { int n = s.pop(); sum += n; q.enqueue(n); } queueToStack(q,s); return sum; } Even though we store values in a Queue, i increases and s.size() decreases, so we only examine half the values
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); return sum; } Now the sum is correct, but our resulting Stack is reversed! This happens when we transfer from a Stack to Queue and back.
15
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); stackToQueue(s,q); queueToStack(q,s); return sum; } Now both the sum and the Stack are correct.
16
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); } queueToStack(q,s); stackToQueue(s,q); queueToStack(q,s); return min; } What if the values in the Stack are all greater than 0? This would still return 0 as the minimum.
17
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; q.enqueue(n); } queueToStack(q,s); stackToQueue(s,q); queueToStack(q,s); return min; } Instead, we start our minimum at a value that is in the Stack.
18
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
19
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
20
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();
21
Exceptions Always comment the type and cause of any exceptions you throw: // throws IllegalArgumentException if the // stack is empty
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.