11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 4.2 Stacks 121top1top2Figure 4.21 Two stacks implemented within in a single array, both growingtoward the middle.4.2.3 Comparison of Array-Based <strong>and</strong> Linked StacksAll oper<strong>at</strong>ions for the array-based <strong>and</strong> linked stack implement<strong>at</strong>ions take constanttime, so from a time efficiency perspective, neither has a significant advantage.Another basis for comparison is the total space required. The analysis is similar toth<strong>at</strong> done for list implement<strong>at</strong>ions. The array-based stack must declare a fixed-sizearray initially, <strong>and</strong> some of th<strong>at</strong> space is wasted whenever the stack is not full. Thelinked stack can shrink <strong>and</strong> grow but requires the overhead of a link field for everyelement.When multiple stacks are to be implemented, it is possible to take advantage ofthe one-way growth of the array-based stack. This can be done by using a singlearray to store two stacks. One stack grows inward from each end as illustr<strong>at</strong>ed byFigure 4.21, hopefully leading to less wasted space. However, this only works wellwhen the space requirements of the two stacks are inversely correl<strong>at</strong>ed. In otherwords, ideally when one stack grows, the other will shrink. This is particularlyeffective when elements are taken from one stack <strong>and</strong> given to the other. If insteadboth stacks grow <strong>at</strong> the same time, then the free space in the middle of the arraywill be exhausted quickly.4.2.4 Implementing RecursionPerhaps the most common computer applic<strong>at</strong>ion th<strong>at</strong> uses stacks is not even visibleto its users. This is the implement<strong>at</strong>ion of subroutine calls in most programminglanguage runtime environments. A subroutine call is normally implemented byplacing necessary inform<strong>at</strong>ion about the subroutine (including the return address,parameters, <strong>and</strong> local variables) onto a stack. This inform<strong>at</strong>ion is called an activ<strong>at</strong>ionrecord. Further subroutine calls add to the stack. Each return from asubroutine pops the top activ<strong>at</strong>ion record off the stack. Figure 4.22 illustr<strong>at</strong>es theimplement<strong>at</strong>ion of the recursive factorial function of Section 2.5 from the runtimeenvironment’s point of view.Consider wh<strong>at</strong> happens when we call fact with the value 4. We use β toindic<strong>at</strong>e the address of the program instruction where the call to fact is made.Thus, the stack must first store the address β, <strong>and</strong> the value 4 is passed to fact.Next, a recursive call to fact is made, this time with value 3. We will name theprogram address from which the call is made β 1 . The address β 1 , along with the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!