1 / 30

Problem of the Day

Problem of the Day. At what times do the minute and hour hands on an analog clock line up?. Problem of the Day. At what times do the minute and hour hands on an analog clock line up?. CSC 212 – Data Structures. Lecture 21: Stack ADT. Rest of the Year.

esme
Télécharger la présentation

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 • At what times do the minute and hour hands on an analog clock line up?

  2. Problem of the Day • At what times do the minute and hour hands on an analog clock line up?

  3. CSC 212 – Data Structures Lecture 21:Stack ADT

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

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

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

  7. View of an ADT IOU

  8. View of an ADT Other Coder You IOU

  9. View of an ADT Other Coder You IOU ADT

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

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

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

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

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

  15. Is linked list an ADT? NO!

  16. Why is linked list not 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!

  17. Implementation vs. ADT Implementation ADT

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

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

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

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

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

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

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

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

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

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

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

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

  30. For Next Lecture • Read GT5.1.2 – 5.1.3 for Friday’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 in 10 days!

More Related