Skip to main content

Section 29.1 Stack ADT

Much list a “list”, a stack does not describe a concrete data structure, but rather an abstract data type (ADT). A stack is a collection of items where we expect to mostly work with the item at the top of the stack.
Common operations on a stack include push(item), which adds an item to the top of the stack, and pop(), which removes and returns the item at the top of the stack. We can also have a peek() operation that returns the item at the top of the stack without removing it. But we do not typically have operations to access or modify items that are not at the top of the stack.

Insight 29.1.1.

Picture a stack of plates while doing dishes. When you go to wash a plate, you take the top plate off the stack. You don’t go digging through the stack to find a plate in the middle. You only ever add or remove plates from the top of the stack. People might come up and add plates to the top of the stack while you are working.
A stack is a linear structure, like a list, but it has more restricted access. So a natural way to implement a stack is to use a list as the underlying data structure. We can use either an array based list or a linked list to implement a stack. In either case, we will only ever add or remove items from one end of the list, which we will consider to be the top of the stack.
First let’s consider a linked list based implementation. Even in the simplest linked list, we can efficiently add and remove items from the front of the list. So that is the logical end of the list to consider as the top of the stack.

Activity 29.1.1. Stack Implemented with a Linked List.

A stack of integers has been implemented using a singly linked list. Use the interactive below to experiment with pushing and popping items from the stack.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
We could also use an array based list to implement a stack. In that case, to make push() and pop() efficient, we would add and remove items from the end of the array.

Activity 29.1.2. Stack Implemented with an Array.

A stack of integers has been implemented using an array based list. Use the interactive below to experiment with pushing and popping items from the stack.
This particular implementation uses a fixed size array and thus has a limited capacity. We would typically implement a dynamic array to allow the stack to grow as needed.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
Because the stack is so simple, we might choose to implement it directly using an array or list nodes. But, if we already have access to a list implementation, it is often easier to just use that as the underlying data structure for the stack. In that case, we might just write a thin wrapper around the list to provide the stack interface. Something like:
Listing 29.1.1.
This IntStack class just provides a limited interface to the underlying vector, forcing us to only interact with the stack through push, pop, peek, and isEmpty operations. The highlighted lines show all the places where the stack is handing off work to the underlying vector.
Also note that we do not need a destructor, copy constructor, or assignment operator because the vector is taking care of the memory management. When the IntStack object is destroyed, the vector that is a member of it is also destroyed. That executes the vector’s destructor, which cleans up the memory.

Insight 29.1.2.

Our code never called new to allocate memory, so we do not need to call delete to free memory.

Checkpoint 29.1.1.

The logical end to consider as the top of the stack when using a singly linked list implementation is the:
  • Front of the list
  • We can always add and remove items from the front of the list efficiently.
  • End of the list
  • Either end

Checkpoint 29.1.2.

The logical index to consider as the top of the stack when using an array-based implementation is:
  • The largest index
  • That is where we can insert items without shifting anything.
  • Index 0
  • Either 0 or the largest index

Checkpoint 29.1.3.

If we implemented a stack using a doubly linked list, what end would be a reasonable choice for the top of the stack:
  • Either end
  • With a doubly linked list, we can efficiently add and remove items from either end of the list, so either end would be a reasonable choice for the top of the stack.
  • End of the list
  • Front of the list
You have attempted of activities on this page.