Download presentation
Presentation is loading. Please wait.
Published byNickolas Malone Modified over 9 years ago
1
CSC 212 Stacks & Queues
2
Announcement Many programs not compiled before submission Several could not be compiled Several others not tested with javadoc Why? Many students struggling with Java This is a programming class Only way to get better: PRACTICE
3
Announcement Daily quizzes accepted electronically only Submit via one or other Dropbox Cannot force you to compile & test them Can make it much less work Please, please, please use this as an excuse to practice your Java Next homework will be assigned Thursday
4
Abstract Data Types (ADTs) We will discuss ADTs for remainder of class ADTs do NOT specify implementation ADTs do guarantee minimum functionality for data type Only discuss publicly available functions What part(s) of Java could be used to do this?
5
Abstract Data Types (ADTs) ADTs define more than functionality What data is stored How the data will be handled What errors can occur and be handled How?
6
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?
7
Stacks Stack is like a PEZ® dispenser: Objects get “pushed” onto stack Objects pushed on top of stack Objects can be pushed at any time Objects get “popped” from stack Popping object removes it from stack Only top object on stack can be popped
8
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 stack boolean isEmpty() returns if stack is empty Object top() returns top entry from Stack w/o removing it
9
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 element throws StackEmptyException; // if the stack is empty public void push(Object obj); // push obj onto the stack public 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?
10
Stacks Java stores “call frames” using a stack Records parameters References for object parameters, actual values for primitive Maintains storage space for temporary results Also location being executed
11
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. }
12
Array-based Stack Could implement stacks using arrays Requiring 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
13
Array-based Stack Pseudo-code: int size(): return t +1 boolean isEmpty(): return t < 0 Object top(): if isEmpty() then throw StackEmptyException else return s [t ]
14
Array-based Stack Arrays start at 0, so initialize t to -1 Should we throw any new exceptions in addition to our stack interface?
15
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
16
Array-based Stack What is input size for each of these methods? What is running time for each method?
17
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
18
More group work Write code to reverse elements in an array using a stack What is the complexity of this method?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.