210 likes | 283 Views
Question of the Day. How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?. Question of the Day.
E N D
Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
CSC 212 – Data Structures Lecture 20:Implementing Stack
Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed
Stack Interface public interface Stack<E> extends Collection {public Epeek()throws EmptyCollectionException;public Epop() throws EmptyCollectionException;public void push(E element); } • Any type of data stored within a Stack • Generics enable us to avoid rewriting this code • Minimum set of exceptions defined by interface • Classes could throw more unchecked exceptions
Array-based Implementation … 0 1 2 • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top]
Array-based Implementation Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top] … 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index
Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top]
Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top]
… X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi
… X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi
… 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi
Oops… My Bad … 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi
Why It Rocks Why It Sucks Simple Array-based Stack • Easy to write & read • Simple to find bugs • Quick running times • Methods take O(1) time • Array must be huge • Max. possible elements • Problems occur when too many exist at once • When full,throws specific exception
Better Approach (Maybe?) • Implement with array, but allow resizing array • Grow as needed rather that stating full • (In Java) Requires allocating new, larger array • Copy data into new array from current array • Reassign field so that it now aliases larger array • Arrays.copyOf()does everything in single call
Better Approach (Maybe?) • Implement with array, but allow resizing array • Grow as needed rather that stating full • (In Java) Requires allocating new, larger array • Copy data into new array from current array • Reassign field so that it now aliases larger array • Arrays.copyOf()does everything in single call • Method call is O(1), but method takes O(n) time • push() now O(n) in worst-case, but how bad is that?
Ways to Grow Array • Two ways to increase array size • Constant value (e.g., 2, 4, 6, 8, 10…) • Constant factor (e.g., 2, 4, 8, 16, 32…) • Both approaches requires O(n) time… • Instantiating & copying array are slow steps • …average (amortized) costs differ, however • Difference in how often slow step needed
Increase Array Size by c • To hold n elements, must grow k = n/c times • Copy array each growth, so total copies is:1+ (c+1) + • • • + (((k-1)*c)+1) + (k*c+1)= 1 + (k*c+1) + (c+1) + (((k-1)*c)+1) + • • •= ((k * c)+ 2)+ ((k * c)+ 2) + • • •= k/2 * ((k * c) + 2)= O(c*k2)= O(c * (n/c)2) = O(n2 * 1/c)= O(n2) • Averaged over adding n elements = O(n) each!
Grow by Constant Factor • To hold n elements, need to grow k = logn times • Still copy array each time, so will make this many copies: 1 + 2 + 4 + • • • + 2k-1 + 2k= (2k - 1) + 2k= 2k+1 - 1 = (2 * 2k) - 1 = (2 * 2logn) - 1= O(2n - 1) = O(n) • Average cost is O(1)(O(n) copies ÷ n elements)
Your Turn • Get into your groups and complete activity
For Next Lecture • Read 4.1 – 4.3 before Wednesday's class • Can we do better than resizable arrays? • Didn’t some GENIUSfind too much memory expensive? • How could we grow & shrink memory demands? • Is linked list an ADT? Why or why not? • Week #7 weekly assignment due Wednesday • Programming Assignment #1 due in 2 weeks • Start before it gets too late! Planning saves time!