Basic Data Types Stacks Richard Newman Based on
Basic Data Types Stacks Richard Newman Based on Sedgewick and Wayne
Stacks and Queues Stacks Dynamic Resizing Queues Generics Iterators Applications
Fundamental data types • Values: sets of objects • Operations: insert, remove, test if empty. • Intent is clear when we insert. • Which item do we remove?
Fundamental data types Stack: Remove the item most recently added. (*LIFO – “last in first out”) Analogy: Cafeteria trays. Web surfing. Queue: Remove the item least recently added. (*FIFO – “first in first out”) Analogy: Grocery store checkout line.
Client, Implementation & Interface Separate Interface & Implementation: Ex: stack, queue, priority queue, symbol table, union-find, . … Client: program using operations defined in interface. Implementation: actual code implementing operations. Interface: description of data type, basic operations.
Client, Implementation & Interface Benefits of separation: Client can't know details of implementation Client has many implementations from which to choose. Implementation can't know details of client needs Many clients can re-use the same implementation.
Client, Implementation & Interface Design: creates modular, reusable libraries. Performance: use optimized implementation where it matters.
Stack API Public class stack: Can be any type (stay tuned) Stack ( ) Create an empty stack Void Push (String S) Insert a new item onto stack String pop ( ) Remove and return the item most recently added Boolean is. Empty ( ) Is the stack empty? PUSH POP public static void main(String[ ] args) { Stack. Of. Strings stack = new Stack. Of. Strings(); while (!Std. In. is. Empty()) { String item = Std. In. read. String(); if (item. equals("-")) Std. Out. print(stack. pop()); else stack. push(item) { More tobe. txt To be or not to - be - - that - - - is Java Stack. Of. Strings < tobe. txt To be not that or be
Stack Pop: linked-list implementation First of best the was it String item = first. item; “of” First best the was it First = first. next; best the was it Return item; First “of”
Stack Pop: linked-list implementation first best of it the was it Node oldfirst = first; oldfirst best first was oldfirst best first the first = new Node (); oldfirst best the was it first. item = “of”; first. next = oldfirst;
Stacks: array implementation Use array s[ ] to store N items on stack push() add new item to s at s[N], ++N pop() s[] remove item at s[N-1], --N it was the best of 0 1 2 3 4 5 N
Dynamic Resizing Stacks: array implementation Arrays are instantiated with a specific size Q: How to grow stack when capacity reached? Q: When to shrink array when stack shrinks? s[] it was the best of 0 1 2 3 4 5 N
Dynamic Resizing Stacks: array implementation First try for increase: push() increase size of s by one Too expensive! Need to copy elements to new array on increase Inserting N elements causes … … 1+2+3+4+. . . +N ~ N 2/2 copy operations NOT FEASIBLE for large N!!! Want to insure increases are infrequent. . .
Dynamic Resizing Stacks: array implementation How to grow array? push() double size of s if capacity reached How many times doubling occur for N items? Ans: lg N times Inserting N elements causes … … 1+2+4+8+16+. . . +N ~ 2 N copy operations Linear!!! (Yay!)
Dynamic Resizing Stacks: array implementation Shrink array: How? pop() decrease size of s by half When? (First try) When array is half full. . . … nope – causes thrashing pop-push-pop-push – time N per operation!
Dynamic Resizing Stacks: array implementation Shrink array: When? Solution: Decrease when ¼ full New array starts ½ full – Need N/2 operations to have to resize again Result: Any N operations takes O(N) copies Array is always between ¼ and 100% full
Comparing Stack Implementations Array Implementation: Most operations constant time and fast Now and then expensive doubling Amortized bound: O(N) for any N operations
Comparing Stack Implementations Linked List Implementation: All operations constant time Gracefully grows and shrinks Extra overhead for objects, references
Comparing Stack Implementations Which better? Toss-up for stacks. . Bigger differences when other ops added
Nonstandard Stack Operations Which implementation is best for … Return size of stack Remove 7 th most recently added element Sample a random element
Stack Uses Parsing Arithmetic Operations – Reverse Polish Notation – Evaluation of expressions Recursive Method Calls Many, many more!
Stack Uses Parsing Arithmetic Operations – Reverse Polish Notation Recursive Method Calls Many, many more!
Next: Queues
- Slides: 23