Stack - if the first item is removed in the AL, then rest...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Stack Stack: last-in, first-out Top of stack d c b a push - add to the stack pop - remove from the stack ie. web browser keeps a stack of pages you’ve visited; when you hit the back button, it goes to the last page and not go to the first page M1 to M2 to M3, then goes back R2 to R1 “Call stack” Generic stack - the stack takes all kinds of types of data; “one size that fits all” API (Application Programming Interface) - public methods/constructors together make up the API Parentheses Matching y = (a*b)+A[x*a] -> correct y = (a+A[(b*c)]+d )/2 -> correct )a+b( -> incorrect How to implement stack in efficient parentheses matching? y = (a+A[(b*c)]+d )/2 ( [ ( the next ) --> is the open parenthesis there? ArrayList
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
no-arg constructor sets up ArrayList of default initial capacity - capacity will change if more items are pushed after an item is pushed that exceeds the initial capacity, the array automatically doubles, copies all of the information from the old array to the new array, and the old array is deleted
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if the first item is removed in the AL, then rest shifts over basic op=write (n-1) writes O(n) Big O Worst Case running times for Stack Operations, stack of length n 1. Constructor = O(1) -- assuming array list creation is O(1) 2. Push = O(n) -- every time the doubling happens, the big O is order n 3. Pop = O(1) 4. size = O(1) 5. empty = O(1) 6. clear = O(1) ArrayList is not a good choice because it keeps growing so the empty space in the front is wasted ArrayList running time best case: O(1) worst case: O(n) when auto-expanding - all n items are copied to new array redistribute/amortize (conceptually only!) the array writes. take away each write - except the lowest two - in a spike and give it to one of the adds in the subsequent group (until the next spike) amortized running time is O(1), adding n elements require O(n) time average = how the algorithm behaves over different inputs...
View Full Document

Page1 / 2

Stack - if the first item is removed in the AL, then rest...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online