1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head...
-
Upload
paul-barnett -
Category
Documents
-
view
224 -
download
0
description
Transcript of 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head...
![Page 1: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/1.jpg)
1ADS Lecture 11
Stacks contd.
![Page 2: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/2.jpg)
![Page 3: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/3.jpg)
ADS Lecture 11 3
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 nullhead
e2 e3 e4 nullhead
e5 e2 e3 e4 nullhead
pop()
push(e5)
![Page 4: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/4.jpg)
A Generic NodeStack Class
ADS Lecture 114
• 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
![Page 5: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/5.jpg)
NodeStack<E>
![Page 6: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/6.jpg)
Node<E>
![Page 7: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/7.jpg)
NodeStack<E>
![Page 8: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/8.jpg)
NodeStack<E>
![Page 9: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/9.jpg)
NodeStack<E>
![Page 10: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/10.jpg)
NodeStack<E>
![Page 11: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/11.jpg)
NodeStack<E>
![Page 12: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/12.jpg)
NodeStack<E>
![Page 13: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/13.jpg)
NodeStack<E>
![Page 14: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/14.jpg)
NodeStack<E>
recursive
![Page 15: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/15.jpg)
NodeStack<E>
recursive
![Page 16: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/16.jpg)
Test
![Page 17: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/17.jpg)
Test
![Page 18: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/18.jpg)
Another example of using a stack… a (simple integer) Reverse Polish Calculator
![Page 19: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/19.jpg)
![Page 20: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/20.jpg)
![Page 21: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/21.jpg)
![Page 22: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/22.jpg)
![Page 23: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/23.jpg)
![Page 24: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/24.jpg)
Random fact #5
![Page 25: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/25.jpg)
Random fact #5
In 1973, amongst physicist, computer scientists, …, the HP35was 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)
![Page 26: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/26.jpg)
end of random fact #5
![Page 27: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/27.jpg)
RPN Calculator
![Page 28: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/28.jpg)
RPN Calculator
![Page 29: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/29.jpg)
RPN Calculator
Use java Stack<String>
![Page 30: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/30.jpg)
RPN Calculator
Use Stack methods as advertised
![Page 31: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/31.jpg)
RPN Calculator
for each
![Page 32: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/32.jpg)
RPN Calculator
User entered some data as a String. If input String is an integer push it onto the stack S
Cool code
![Page 33: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/33.jpg)
RPN Calculator
![Page 34: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/34.jpg)
RPN Calculator
NOTE: no exception handling in event of stack overflow or underflow
What is “overflow” and what is “underflow”?
![Page 35: 1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.](https://reader036.fdocuments.us/reader036/viewer/2022062412/5a4d1b087f8b9ab0599891e3/html5/thumbnails/35.jpg)