1 / 27

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. CSC 212 – Data Structures. Lecture 22: Implementing Stack. Using Stack.

cargan
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. CSC 212 – Data Structures Lecture 22:Implementing Stack

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

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

  7. Array-based Implementation … 0 1 2 • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top + 1]

  8. Array-based Implementation … 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top+ 1]

  9. Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top + 1]

  10. Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top + 1]

  11. X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi

  12. X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi

  13. 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi

  14. Oops… My Bad … 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi

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

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

  17. Once You pop()…  • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal

  18. Once You pop()… Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal  retVal • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node

  19. Once You pop()…  retVal • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal

  20. Once You pop()…  retVal • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal

  21. Linked list-based Stack  • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null

  22. Linked list-based Stack  e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null

  23. Linked list-based Stack  newN e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null

  24. Linked list-based Stack  newN e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null

  25. Linked list-based Stack  • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null

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

  27. For Next Lecture • Read GT section 5.2 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

More Related