1 / 35

Stacks contd.

Stacks contd. Singly Linked list-based Stack. Top of stack is head of list (can insert elements at head in constant time, at tail in linear time) To perform operation size in constant time, keep track of size via instance variable Implement generic stack using generic linked list.

wynn
Download Presentation

Stacks contd.

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. Stacks contd. ADS Lecture 11

  2. Singly Linked list-based Stack • Top of stack is head of list (can insert elements at head in constant time, at tail in linear time) • To perform operation size in constant time, keep track of size via instance variable • Implement generic stack using generic linked list pop() head head e3 e1 e2 e3 e4 e2 e4 e5 e2 e3 e4 null null head null push(e5) ADS Lecture 11

  3. A Generic NodeStack Class • Code given in next few slides • All methods are executed in constant time (except toString) • In addition, space requirement is O(n), where n is current number of elements in stack • Do not need a new exception to be created to handle overflow problems • Use instance variable top to refer to head of list (null if list empty) • To push new element e onto stack simply add to head of list, to pop simply remove head and return its element 4 ADS Lecture 11

  4. NodeStack<E>

  5. Node<E>

  6. NodeStack<E>

  7. NodeStack<E>

  8. NodeStack<E>

  9. NodeStack<E>

  10. NodeStack<E>

  11. NodeStack<E>

  12. NodeStack<E>

  13. NodeStack<E> recursive

  14. NodeStack<E> recursive

  15. Test

  16. Test

  17. Another example of using a stack … a (simple integer) Reverse Polish Calculator

  18. Random fact #5

  19. Random fact #5 In 1973, amongst physicist, computer scientists, …, the HP35 was the “must have” item. It was cool, exotic (first commercial rpn calculator), and most importantly … it felt good! “Felt good”? … the keys had a “click”, i.e. a push and then “click” like a micro switch. Really beautiful. It cost me 1 month’s pay (about £70 as a student in 1973)

  20. end of random fact #5

  21. RPN Calculator

  22. RPN Calculator

  23. RPN Calculator Use java Stack<String>

  24. RPN Calculator Use Stack methods as advertised

  25. RPN Calculator for each

  26. RPN Calculator User entered some data as a String. If input String is an integer push it onto the stack S Cool code

  27. RPN Calculator

  28. RPN Calculator NOTE: no exception handling in event of stack overflow or underflow What is “overflow” and what is “underflow”?

More Related