csc 212 stacks & queues. announcement many programs not compiled before submission several...
TRANSCRIPT
CSC 212
Stacks & Queues
Announcement
Many programs not compiled before submissionSeveral could not be compiledSeveral others not tested with javadocWhy?
Many students struggling with JavaThis is a programming classOnly way to get better: PRACTICE
Announcement
Daily quizzes accepted electronically onlySubmit via one or other DropboxCannot force you to compile & test themCan make it much less workPlease, please, please use this as an excuse
to practice your Java Next homework will be assigned Thursday
Abstract Data Types (ADTs)
We will discuss ADTs for remainder of class
ADTs do NOT specify implementation ADTs do guarantee minimum functionality
for data typeOnly discuss publicly available functionsWhat part(s) of Java could be used to do this?
Abstract Data Types (ADTs)
ADTs define more than functionalityWhat data is storedHow the data will be handledWhat errors can occur and be handled
How?
Stock Trading ADT
Suppose we want to design a stock trading ADT
What functionality should be defined?
What errors could occur? How should they be handled?
Stacks
Stack is like a PEZ® dispenser: Objects get “pushed” onto stack
Objects pushed on top of stackObjects can be pushed at any time
Objects get “popped” from stackPopping object removes it from stackOnly top object on stack can be popped
Stacks
So, Stack ADT defines two vital methods:void push(Object obj)Object pop()
Also defines other useful functionality: int size() returns number of elements on stackboolean isEmpty() returns if stack is emptyObject top() returns top entry from Stack w/o
removing it
Stacks
We can define our own stack interface like this:public interface Stack {
public int size(); // returns elements in the stack
public boolean isEmpty(); // return if the stack is empty
public Object top() // return the top elementthrows StackEmptyException; // if the stack is
emptypublic void push(Object obj); // push obj onto the stackpublic Object pop() // return and remove top stack element
throws StackEmptyException; // if the stack is empty
}
Why do push(), pop(), and top() return objects of type Object?
Stacks
Java stores “call frames” using a stackRecords parameters
References for object parameters, actual values for primitive
Maintains storage space for temporary resultsAlso location being executed
findMin
1. public static int findMin(int[] a, int n) {
2. if (n == (a.length - 1))
3. return a[n];
4. else {
5. int recurMin = findMin(a, n+1);
6. if (a[n] < recurMin)
7. return a[n];
8. else
9. return recurMin;
10. }
11. }
Array-based Stack
Could implement stacks using arraysRequiring a maximum size N for our stack
(e.g., N = 1000) Stack then includes
N-element array, s,Array index of the top element, t
Array-based Stack
Pseudo-code:
int size(): return t +1
boolean isEmpty(): return t < 0
Object top():
if isEmpty() then
throw StackEmptyExceptionelse
return s [t ]
Array-based Stack
Arrays start at 0, so initialize t to -1 Should we throw any new exceptions in
addition to our stack interface?
Array-based Stack
More pseudo-code:push(obj):
if size() = N then throw StackFullException
t t + 1
s[t] obj
Object pop():
if isEmpty() then throw StackEmptyException
e s[t ]
s[t ] null
t t -1
return e
Array-based Stack
What is input size for each of these methods?
What is running time for each method?
Array-based Stack
Arrays make simple and efficient implementation
Note: N is upper bound on stack size If N is too small, cannot hold all items
Makes stack class unusable If N is too large, will waste memory
Can lead to significant performance penalty
More group work
Write code to reverse elements in an array using a stack
What is the complexity of this method?