1 / 38

Problem of the Day

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….

saima
Download Presentation

Problem of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem of the Day • What do you get when you cross a mountain climber and a grape?

  2. Problem of the Day • What do you get when you cross a mountain climber and a grape? • Nothing, you cannot cross a scalar.

  3. Google Easter Eggs

  4. Google Easter Eggs

  5. Google Easter Eggs

  6. When Base Case Is Missing…

  7. CSC 212 – Data Structures Lecture 22:Stack ADT

  8. 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

  9. 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

  10. 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

  11. View of an ADT IOU

  12. View of an ADT Other Coder You IOU

  13. View of an ADT Other Coder You IOU ADT

  14. Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with:

  15. Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array

  16. Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array • Linked list

  17. Implementing ADT • ADTs must remain abstract • Any implementation okay, if it completes methods • Could implement an ADT with: • Array • Linked list • Trained monkeys

  18. 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

  19. Is linked list an ADT?

  20. Is Linked List an ADT? NO!

  21. 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!

  22. Implementation vs. ADT Implementation ADT

  23. Collection Classes • Superinterfacefor all our ADTs • Define methods common to all data structures • Access & usages patterns differ with each ADT

  24. 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(); }

  25. 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(); }

  26. 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(); }

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed

  36. 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

  37. Your Turn • Get into your groups and complete activity

  38. 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!

More Related