1 / 18

Question of the Day

Question of the Day. How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?. Question of the Day.

zaide
Download Presentation

Question 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. Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?

  2. Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?

  3. CSC 212 – Data Structures Lecture 22:Linked List-BasedStack

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

  5. Stack Interface public interface Stack<E> extends Collection {public Epeek()throws EmptyCollectionException;public Epop() throws EmptyCollectionException;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

  6. Why It Rocks Why It Sucks Array-based Stack • Easy to write & read • Simple to find bugs • Quick running times • Methods (usually) take O(1) time • Array must be huge • Max. possible elements • Problems occur when too many exist at once • When full,takes O(n) time to reload

  7. Better Approach (Maybe?) • Implement Stack using linked list • Grows & shrinks as elements added & removed • Add element in push() by allocating LinearNode • pop()removes LinearNodefrom linked list • Concerns about size limits are forgotten

  8. Once You pop()…  • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal

  9. Once You pop()…  retVal • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal

  10. Once You pop()… Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal  retVal • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node

  11. Once You pop()… Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal  retVal • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node

  12. Linked list-based Stack  • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null

  13. Linked list-based Stack  e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null

  14. Linked list-based Stack  newN e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null

  15. Linked list-based Stack  newN e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null

  16. Linked list-based Stack  • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null

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

  18. For Next Lecture • Read sections 5.1 – 5.7 before Monday's class • Discusses design of the Queue ADT • Array-based implementation of Queue presented • Queue implementation of linked-list also shown • Week #8 weekly assignment due Tuesday • Programming Assignment #1 due next week • Not too late now; but should really have started!

More Related