{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

DataStructures

# DataStructures - The Stack ADT(4.2.1 Elementary Data...

This preview shows pages 1–3. Sign up to view the full content.

Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees Elementary Data Structures 2 The Stack ADT (§4.2.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations: push (Object o): inserts element o pop (): removes and returns the last inserted element Auxiliary stack operations: top (): returns the last inserted element without removing it size (): returns the number of elements stored isEmpty (): a Boolean value indicating whether no elements are stored Elementary Data Structures 3 Applications of Stacks Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine or C++ runtime environment Indirect applications Auxiliary data structure for algorithms Component of other data structures Elementary Data Structures 4 Array-based Stack (§4.2.2) A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable t keeps track of the index of the top element (size is t+1) S 0 1 2 t Algorithm pop (): if isEmpty () then throw EmptyStackException else t t 1 return S [ t + 1] Algorithm push ( o ) if t = S.length 1 then throw FullStackException else t t + 1 S [ t ] o Elementary Data Structures 5 Growable Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push ( o ) if t = S.length 1 then A new array of size … for i 0 to t do A [ i ] S [ i ] S A t t + 1 S [ t ] o Elementary Data Structures 6 Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T ( n ) needed to perform a series of n push operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of a push operation the average time taken by a push over the series of operations, i.e., T ( n )/ n

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

View Full Document
Elementary Data Structures 7 Analysis of the Incremental Strategy We replace the array k = n / c times The total time T ( n ) of a series of n push operations is proportional to n + c + 2 c + 3 c + 4 c + + kc = n + c (1 + 2 + 3 + … + k ) = n + ck ( k + 1)/2 Since c is a constant, T ( n ) is O ( n + k 2 ) , i.e., O ( n 2 ) The amortized time of a push operation is O ( n ) Elementary Data Structures 8 Direct Analysis of the Doubling Strategy We replace the array k = log 2 n times The total time T ( n ) of a series of n push operations is proportional to n + 1 + 2 + 4 + 8 + …+ 2 k = n + 2 k + 1 1 = 2 n 1 T ( n ) is O ( n ) The amortized time of a push operation is O (1) geometric series 1 2 1 4 8 Elementary Data Structures 9 The accounting method determines the amortized running time with a system of credits and debits
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

DataStructures - The Stack ADT(4.2.1 Elementary Data...

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

View Full Document
Ask a homework question - tutors are online