The Stack
This chapter covers the Stack ADT, a collection where a client can work only with the most recently added item. We implement it twice, as ArrayStack and LinkedStack, and compare the two.
After reading this chapter, you should be able to:
- Describe the Stack ADT: the
push,pop,top, andisEmptyoperations, the last-in, first-out rule, and the documented exception behavior - Implement and trace
ArrayStack, with the end of the array as the top, andLinkedStack, with the head of the list as the top - Compare the two implementations, and explain why a stack’s limited interface lets both run every operation in , with
pushonArrayStackamortized - Recognize last-in, first-out behavior in a problem as the cue to use a stack
You can download the accompanying code for this chapter: starter and solution.
Sections
- Implementing Undo
- The Stack ADT
- Stack ADT Specification
- Using an Array to Implement a Stack
- The ArrayStack Class
- Implementing isEmpty, top, and pop
- Implementing push
- Tracing the ArrayStack
- Using a Linked List to Implement a Stack
- The LinkedStack Class
- Implementing pop, top, and push
- Tracing the LinkedStack
- Two Stacks, Side by Side