Close
Register
Close Window

BM40A1500 Data Structures and Algorithms

Chapter 2 Linear Data Structures

Show Source |    | About   «  2.5. Stacks   ::   Contents   ::   2.7. Queues  »

2.6. Linked Stacks

2.6.1. Linked Stack Implementation

The linked stack implementation is quite simple. Elements are inserted and removed only from the head of the list. A header node is not used because no special-case code is required for lists of zero or one elements.

Here is a visual representation for the linked stack.

2.6.2. Linked Stack Push

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

2.6.3. Linked Stack Pop

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

2.6.4. Comparison of Array-Based and Linked Stacks

All operations for the array-based and linked stack implementations take constant time, so from a time efficiency perspective, neither has a significant advantage. Another basis for comparison is the total space required. The analysis is similar to that done for list implementations. The array-based stack must declare a fixed-size array initially, and some of that space is wasted whenever the stack is not full. The linked stack can shrink and grow but requires the overhead of a link field for every element.

When implementing multiple stacks, sometimes you can take advantage of the one-way growth of the array-based stack by using a single array to store two stacks. One stack grows inward from each end as illustrated by the figure below, hopefully leading to less wasted space. However, this only works well when the space requirements of the two stacks are inversely correlated. In other words, ideally when one stack grows, the other will shrink. This is particularly effective when elements are taken from one stack and given to the other. If instead both stacks grow at the same time, then the free space in the middle of the array will be exhausted quickly.

   «  2.5. Stacks   ::   Contents   ::   2.7. Queues  »

nsf
Close Window