12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

128 Chap. 4 Lists, Stacks, <strong>and</strong> Queues<strong>to</strong>p1<strong>to</strong>p2Figure 4.20 Two stacks implemented within in a single array, both growing<strong>to</strong>ward the middle.is then returned <strong>to</strong> free s<strong>to</strong>re (or the freelist), <strong>and</strong> the element value is returned asthe value of the pop method.4.2.3 Comparison of Array-Based <strong>and</strong> Linked StacksAll operations for the array-based <strong>and</strong> linked stack implementations take constanttime, so from a time efficiency perspective, neither has a significant advantage.Another basis for comparison is the <strong>to</strong>tal space required. The analysis is similar <strong>to</strong>that done for list implementations. The array-based stack must declare a fixed-sizearray initially, <strong>and</strong> some of that 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 <strong>to</strong> be implemented, it is possible <strong>to</strong> take advantage ofthe one-way growth of the array-based stack. This can be done by using a singlearray <strong>to</strong> s<strong>to</strong>re two stacks. One stack grows inward from each end as illustrated byFigure 4.20, hopefully leading <strong>to</strong> less wasted space. However, this only works wellwhen the space requirements of the two stacks are inversely correlated. 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 <strong>to</strong> the other. If insteadboth stacks grow at 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 application that uses stacks is not even visible<strong>to</strong> its users. This is the implementation of subroutine calls in most programminglanguage runtime environments. A subroutine call is normally implemented byplacing necessary information about the subroutine (including the return address,parameters, <strong>and</strong> local variables) on<strong>to</strong> a stack. This information is called an activationrecord. Further subroutine calls add <strong>to</strong> the stack. Each return from asubroutine pops the <strong>to</strong>p activation record off the stack. Figure 4.21 illustrates theimplementation of the recursive fac<strong>to</strong>rial function of Section 2.5 from the runtimeenvironment’s point of view.

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

Saved successfully!

Ooh no, something went wrong!