Download presentation
Presentation is loading. Please wait.
Published byPaul Barnett Modified over 9 years ago
1
1ADS Lecture 11 Stacks contd.
3
ADS Lecture 113 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 e1 e2 e3 e4 null head e2 e3 e4 null head e5 e2 e3 e4 null head pop() push(e5)
4
A Generic NodeStack Class ADS Lecture 11 4 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
5
NodeStack
6
Node
7
NodeStack
14
recursive
15
NodeStack recursive
16
Test
18
Another example of using a stack … a (simple integer) Reverse Polish Calculator
24
Random fact #5
25
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)
26
end of random fact #5
27
RPN Calculator
29
Use java Stack
30
RPN Calculator Use Stack methods as advertised
31
RPN Calculator for each
32
RPN Calculator User entered some data as a String. If input String is an integer push it onto the stack S Cool code
33
RPN Calculator
34
NOTE: no exception handling in event of stack overflow or underflow What is “overflow” and what is “underflow”?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.