Data Structures & Algorithms Lecture Notes

6 October 2010 • Stacks


Outline

  • Motivation
  • Stack properties.
  • The stack ADT.
  • ADT implementations.
  • Extension and details.
acrobat stack

Lists

The Trade-Off

ADT Properties

The Stack ADT

  • The maintenance operations are create and delete.
  • The manipulation operations are add and remove.
    • “Add” is traditionally called push.
    • “Remove” is traditionally called pop.
  • Value references are unnecessary.
    • Although they can be provided.
a stack o' books

Adding and Removing Values

Query Operations

Stack ADT Implementations

Array Representation

Stack Push and Pop

List-Stack Comparison

Dynamic Stacks

List Implementation

Push And Pop

Stack Comparison

List Overkill

Dynamic-Array Stacks

Dynamic Example

example stack expansion

  • Pushing 4 causes a stack overflow.
  • Create a new array, twice as big.
  • Copy values from the old to the new array.
  • Replace old with the new array.
  • Do the push.

Dynamic-Array Costs

Stack Comparison

Value Ordering

Stacks Everywhere

Activation Records

Activation Stack

Array-Reverse Example

void reverse(T a[], int n, int i)
  if i < n
    T c = a[i]
    reverse(a, n, i + 1)
    a[n - (i + 1)] = c

an example reverse call sequence

Summary

References

Credits


This page last modified on 6 October 2010.

Creative
    Commons License