380 likes | 458 Views
Problem of the Day. What do you get when you cross a mountain climber and a grape?. Problem of the Day. What do you get when you cross a mountain climber and a grape? Nothing, you cannot cross a scalar. Google Easter Eggs. Google Easter Eggs. Google Easter Eggs. When Base Case Is Missing….
E N D
Problem of the Day • What do you get when you cross a mountain climber and a grape?
Problem of the Day • What do you get when you cross a mountain climber and a grape? • Nothing, you cannot cross a scalar.
CSC 212 – Data Structures Lecture 22:Stack ADT
Rest of the Year Abstract–List what is done, not how it is done Data– Access & use Collections of data Type– Will use in fields, parameters, & locals
Rest of the Year Abstract–List what is done, not how it is done Data–Access & use Collectionsof data Type– Will use in fields, parameters, & locals
ADTs Mean Interfaces • Each ADT is defined by single Interface • Guarantees methods exist & what they should do • But classes are free to implement however they want • Programmer knows from the interface: • Each of the method signatures • Value returned by the method • The effects of the method’s actions • Why Exceptions thrown by method
View of an ADT IOU
View of an ADT Other Coder You IOU
View of an ADT Other Coder You IOU ADT
Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with:
Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array
Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array • Linked list
Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array • Linked list • Trained monkeys
Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array • Linked list • Trained monkeys • College students
Why Not an ADT? • Linked lists have very specific implementation • Singly-, & doubly-linked versions exist… • … but implementation impossible using an array • No trained monkeys could do same work • Linked lists also do not specify functionality • No standard way to access or use data • In fact, there is no interface serving as guarantee!
Implementation vs. ADT Implementation ADT
Collection Classes • Superinterfacefor all our ADTs • Define methods common to all data structures • Access & usages patterns differ with each ADT
Collection Classes • Superinterfacefor all our ADTs • Define methods common to all data structures • Access & usages patterns differ with each ADT public interface Collection {public int size();public booleanisEmpty(); }
Collection Classes • Superinterfacefor all our ADTs • Define methods common to all data structures • Access & usages patterns differ with each ADT public interface Collection {public int size();public booleanisEmpty(); }
Collection Classes • Superinterfacefor all our ADTs • Define methods common to all data structures • Access & usages patterns differ with each ADT public interface Collection {public int size();public booleanisEmpty(); }
Stacks • Awwww… our first collection class • Works like PEZ dispenser: • Add by pushing data onto top • Pop top item off to remove • Accessing other values impossible • Top item only is available • Cheap plastic/private fields get in way
Stacks • Awwww… our first collection class • Works like PEZ dispenser: • Add by pushing data onto top • Pop top item off to remove • Accessing other values impossible • Top item only is available • Cheap plastic/private fields get in way
Stacks • Awwww… our first collection class • Works like stackof books (or coins, rocks, …) • Add by pushing data onto top • Pop top item off to remove • Accessing other values impossible • Top item only is available • Gravity’s pull/private fields get in way
Stacks • Awwww… our first collection class • Works like stackof chips (or coins, rocks, …) • Add by pushing data onto top • Pop top item off to remove • Accessing other values impossible • Top item only is available • Cardboard tube/private fields get in way
Applications of Stacks • Stacks are used everywhere • Back & Forward buttons in web browser • Powerpoint’sUndo & Redo commands • Methods’ stackframesused during execution • Java uses stacks to execute operations in program
Stack ADT • Defines two vital methods… • push(obj) add obj onto top of stack • pop() remove & return item on top of stack • … an accessormethod… • top() return top item (but do not remove it) • … and Collection’s methods… • size() returns number of items in stack • isEmpty() states if stack contains items
More Stack ADT • ADT also defines own exception public class EmptyStackException extends RuntimeException{public EmptyStackException(String err) {super(err);} } • EmptyStackExceptionis unchecked • Need not be listed in throws, but could be • Unchecked since there is little you can do to fix it • try-catchnot required, but can crash program
Stack Interface public interface Stack<E> extends Collection {public Etop()throws EmptyStackException;public Epop() throws EmptyStackException;public void push(E element); } • Any type of data stored within a Stack • Generics enable us to avoid rewriting this code • Minimum set of exceptions defined by interface • Classes could throw more unchecked exceptions
Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed
Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed • Pulling out tablecloth trick does not work
Your Turn • Get into your groups and complete activity
For Next Lecture • Read GT5.1.2 – 5.1.3 for Monday’s class • How can we use an array to implement a Stack? • Linked-list based approaches possible, too? • Why would we prefer one over the other? • Week #8 weekly assignment due on Tuesday • Programming assignment #1 also on Angel • Pulls everything together and shows off your stuff • Better get moving on it, since due end of today!