stacks outline and reading
play

Stacks Outline and Reading The Stack ADT (4.2.1) Applications of - PowerPoint PPT Presentation

Stacks Outline and Reading The Stack ADT (4.2.1) Applications of Stacks (4.2.3) Array-based implementation (4.2.2) Growable array-based stack Stacks 2 Abstract Data Types (ADTs) An abstract data Example: ADT modeling a type (ADT) is


  1. Stacks

  2. Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack Stacks 2

  3. Abstract Data Types (ADTs) An abstract data Example: ADT modeling a type (ADT) is an simple stock trading system abstraction of a � The data stored are buy/sell data structure orders An ADT specifies: � The operations supported are � Data stored � order buy(stock, shares, price) � Operations on the � order sell(stock, shares, price) data � void cancel(order) � Error conditions � Error conditions: associated with � Buy/sell a nonexistent stock operations � Cancel a nonexistent order Stacks 3

  4. The Stack ADT The Stack ADT stores Auxiliary stack arbitrary objects operations: � top(): returns a reference Insertions and deletions to the last inserted follow the last-in first-out element without removing scheme it Think of a spring-loaded � size(): returns the number plate dispenser of elements stored � isEmpty(): returns a Main stack operations: Boolean value indicating � push(object o): inserts whether no elements are element o stored � pop(): removes and returns the last inserted element Stacks 4

  5. Exceptions Attempting the In the Stack ADT, execution of an operations pop and operation of ADT may top cannot be sometimes cause an performed if the error condition, called stack is empty an exception Attempting the Exceptions are said to execution of pop or be “thrown” by an top on an empty operation that cannot stack throws an be executed EmptyStackException Stacks 5

  6. Applications of Stacks Direct applications � Page-visited history in a Web browser � Undo sequence in a text editor � Saving local variables when one function calls another, and this one calls another, and so on. Indirect applications � Auxiliary data structure for algorithms � Component of other data structures Stacks 6

  7. C++ Run-time Stack main() { The C++ run-time system int i = 5; keeps track of the chain of bar foo(i); active functions with a stack PC = 1 } m = 6 When a function is called, the run-time system pushes on the foo(int j) { stack a frame containing foo int k; PC = 3 � Local variables and return value k = j+1; j = 5 � Program counter, keeping track of bar(k); k = 6 the statement being executed } When a function returns, its main frame is popped from the stack bar(int m) { PC = 2 and control is passed to the … i = 5 method on top of the stack } Stacks 7

  8. Array-based Stack Algorithm size () A simple way of implementing the return t + 1 Stack ADT uses an array Algorithm pop () We add elements if isEmpty () then from left to right throw EmptyStackException A variable keeps else track of the index of t ← t − 1 the top element return S [ t + 1] … S 0 1 2 t Stacks 8

  9. Array-based Stack (cont.) The array storing the stack elements may Algorithm push ( o ) become full if t = S.length − 1 then A push operation will throw FullStackException then throw a else FullStackException t ← t + 1 � Limitation of the array- S [ t ] ← o based implementation � Not intrinsic to the Stack ADT … S 0 1 2 t Stacks 9

  10. Performance and Limitations Performance � Let n be the number of elements in the stack � The space used is O ( n ) � Each operation runs in time O (1) Limitations � The maximum size of the stack must be defined a priori , and cannot be changed � Trying to push a new element into a full stack causes an implementation-specific exception Stacks 10

  11. Computing Spans 7 We show how to use a stack 6 as an auxiliary data structure 5 in an algorithm 4 Given an an array X , the span 3 S [ i ] of X [ i ] is the maximum 2 number of consecutive elements X [ j ] immediately 1 preceding X [ i ] and such that 0 X [ j ] ≤ X [ i ] 0 1 2 3 4 Spans have applications to financial analysis X 6 3 4 5 2 � E.g., stock at 52-week high S 1 1 2 3 1 Stacks 11

  12. Quadratic Algorithm Algorithm spans1 ( X, n ) Input array X of n integers Output array S of spans of X # S ← new array of n integers n for i ← 0 to n − 1 do n s ← 1 n while s ≤ i ∧ X [ i − s ] ≤ X [ i ] 1 + 2 + … + ( n − 1) s ← s + 1 1 + 2 + … + ( n − 1) S [ i ] ← s n return S 1 Algorithm spans1 runs in O ( n 2 ) time Stacks 12

  13. Computing Spans with a Stack We keep in a stack the 7 indices of the elements 6 visible when “looking 5 back” 4 We scan the array from 3 left to right 2 � Let i be the current index 1 � We pop indices from the stack until we find index j 0 such that X [ i ] < X [ j ] 0 1 2 3 4 5 6 7 � We set S [ i ] ← i − j � We push x onto the stack Stacks 13

  14. Linear Algorithm Each index of the # Algorithm spans2 ( X, n ) array S ← new array of n integers n A ← new empty stack � Is pushed into the 1 stack exactly one for i ← 0 to n − 1 do n � Is popped from while ( ¬ A . isEmpty () ∧ the stack at most X [ top ()] ≤ X [ i ] ) do n once j ← A.pop () n The statements in if A . isEmpty () then n the while-loop are S [ i ] ← i + 1 n executed at most else n times S [ i ] ← i − j n Algorithm spans2 A . push ( i ) n runs in O ( n ) time return S 1 Stacks 14

  15. Growable Array-based Stack In a push operation, when Algorithm push ( o ) the array is full, instead of if t = S.length − 1 then throwing an exception, we A ← new array of can replace the array with size … a larger one for i ← 0 to t do How large should the new A [ i ] ← S [ i ] array be? S ← A t ← t + 1 � incremental strategy: S [ t ] ← o increase the size by a constant c � doubling strategy: double the size Stacks 15

  16. 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 Stacks 16

  17. Incremental Strategy Analysis 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 ) Stacks 17

  18. Doubling Strategy Analysis We replace the array k = log 2 n times geometric series The total time T ( n ) of a series of n push operations is 2 proportional to 4 n + 1 + 2 + 4 + 8 + …+ 2 k = 1 1 n + 2 k + 1 − 1 = 2 n − 1 8 T ( n ) is O ( n ) The amortized time of a push operation is O (1) Stacks 18

  19. Stack Interface in C++ template <typename Object> Interface class Stack { corresponding to public: our Stack ADT int size(); bool isEmpty(); Requires the Object& top() definition of class throw(EmptyStackException); EmptyStackException void push(Object o); Most similar STL Object pop() construct is vector throw(EmptyStackException); }; Stacks 19

  20. Array-based Stack in C++ template <typename Object> bool isEmpty() class ArrayStack { { return (t < 0); } private: Object pop() int capacity; // stack capacity throw(EmptyStackException) { Object *S; // stack array if(isEmpty()) int top; // top of stack throw EmptyStackException public: (“Access to empty stack”); ArrayStack(int c) { return S[t--]; capacity = c; } S = new Object[capacity]; // … (other functions omitted) t = –1; } Stacks 20

Recommend


More recommend