Close
Register
Close Window

Show Source |    | About   «  5.7. List Element Implementations   ::   Contents   ::   5.9. Linked Stacks  »

5.8. Stacks

5.8.1. Stack Terminology and Implementation

The stack is a list-like structure in which elements may be inserted or removed from only one end. While this restriction makes stacks less flexible than lists, it also makes stacks both efficient (for those operations they can do) and easy to implement. Many applications require only the limited form of insert and remove operations that stacks provide. In such cases, it is more efficient to use the simpler stack data structure rather than the generic list. For example, the freelist is really a stack.

Despite their restrictions, stacks have many uses. Thus, a special vocabulary for stacks has developed. Accountants used stacks long before the invention of the computer. They called the stack a “LIFO” list, which stands for “Last-In, First-Out.” Note that one implication of the LIFO policy is that stacks remove elements in reverse order of their arrival.

The accessible element of the stack is called the top element. Elements are not said to be inserted, they are pushed onto the stack. When removed, an element is said to be popped from the stack. Here is a simple stack ADT.

As with lists, there are many variations on stack implementation. The two approaches presented here are the array-based stack and the linked stack, which are analogous to array-based and linked lists, respectively.

5.8.1.1. Array-Based Stacks

Here is a complete implementation for the array-based stack class.


Settings

Proficient Saving... Error Saving
Server Error
Resubmit

The array-based stack implementation is essentially a simplified version of the array-based list. The only important design decision to be made is which end of the array should represent the top of the stack.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit


Settings

Proficient Saving... Error Saving
Server Error
Resubmit

5.8.2. Pop

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

   «  5.7. List Element Implementations   ::   Contents   ::   5.9. Linked Stacks  »

nsf
Close Window