Skip to main content

Section 6.2 Linked Stacks

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.
Both the array-based and linked implementations of stacks offer constant-time operations, resulting in similar time efficiency. Therefore, neither approach has a significant advantage in terms of time efficiency. However, a comparison can be made based on the total space required by each implementation, similar to the analysis performed for list implementations.
In the case of the array-based stack, a fixed-size array needs to be declared initially, and some space is wasted whenever the stack is not full. On the other hand, the linked stack has the flexibility to shrink and grow dynamically but incurs the overhead of a link field for every element.
When implementing multiple stacks, there is a possibility to leverage the one-way growth characteristic of the array-based stack by using a single array to store two stacks. Each stack grows inward from opposite ends, as illustrated in the figure below, which can help minimize wasted space. However, this approach works best when the space requirements of the two stacks are inversely correlated. Ideally, when one stack grows, the other should shrink. This technique proves particularly effective when elements are transferred between the two stacks. In contrast, if both stacks grow simultaneously, the free space in the middle of the array will be depleted rapidly.
You have attempted of activities on this page.