Stacks

Course links

Abstract data types

An abstract data type is a set of values and operations on those values, considered independently of the ways in which those values might be represented and the operations implemented in actual programs. Separating the definition of an abstract data type from its implementation is a technique that has been found to be especially useful in the development of large software systems.

Objects produced by a constructor procedure, such as the switches in the reading on object-oriented programming, can often be developed efficiently from the definition of an abstract data type, with the advantage that other procedures that take such objects as arguments cannot operate on them or modify them in any way not considered by the definition of the abstract data type. As a result, the methods of such an object can often be implemented in such a way as to preserve certain simplifying invariants -- conditions that are known to be true at the beginning and end of the execution of each method. Relying on such invariants often allows the programmer to dispense with some precondition tests in the methods, because the invariants imply that the preconditions will be met whenever the method is called.

As illustrations of the use of abstract data types in the development of programs, we shall consider two frequently encountered data structures that Scheme happens not to supply as built-ins: stacks and queues.

Stacks as an abstract data type

Conceptually, the stack abstract data type mimics the information kept in a pile on a desk. Informally, we first consider materials on a desk, where we may keep separate stacks for bills that need paying, magazines that we plan to read, and notes we have taken. We can perform several operations that involve a stack:

These operations allow us to do all the normal processing of data at a desk. For example, when we receive bills in the mail, we add them to the pile of bills until payday comes. We then take the bills, one at a time, from the top of the pile and pay them until the money runs out.

When discussing these operations, it is conventional to call the addition of an item to the top of the stack a push operation and the deletion of an item from the top a pop operation. (These terms are derived from the workings of a spring-loaded rack containing a stack of cafeteria trays. Such a rack is loaded by pushing the trays down onto the springs; as each diner removes a tray, the lessened weight on the springs causes the stack to pop up slightly.)

Here is a more formal definition of the stack ADT: A stack is a data structure containing zero or more elements, on which the following operations can be performed:

This abstract data type definition says nothing about how we will program the various stack operations; rather, it tells us how stacks can be used. We can infer some limitations on how we can use the data. For example, stack operations allow us to work with only the top item on the stack. We cannot look at elements farther down in the stack without first using pop operations to clear away items above the desired one.

A push operation always puts the new item on top of the stack, and this is the first item returned by a pop operation. Thus, the last piece of data added to the stack will be the first item removed.

Stacks in Scheme

We can implement stacks in Scheme as objects that respond to the messages ':empty?, ':push!, ':pop!, and ':top. The create operation will correspond to the constructor procedure make-stack, which takes no arguments and returns an empty stack. The object will protect access to a static variable, stk, which will contain all of the elements that are currently in the stack, assembled into a list. Here is the code:

;;; make-stack: construct and return a stack

;; Givens:
;;   None

;; Result:
;;   STACK, a procedure

;; Preconditions:
;;   None.

;; Postconditions:
;;   (1) STACK is initially empty.
;;   (2) When STACK is invoked with the argument :EMPTY?, it reports
;;       whether it is empty.
;;   (3) When STACK has been invoked with the first argument :PUSH! and a
;;       second argument, say NEW-VALUE, NEW-VALUE is at the top (available
;;       for popping) and all other values on the stack are below it, in
;;       order by recency of having been pushed on.
;;   (4) When STACK has been invoked with the argument :POP!, it returns
;;       the value that was formerly at the top and retains all other
;;       values, in order by recency of having been pushed on.
;;   (5) When STACK is invoked with the argument :TOP, it returns the value
;;       at the top (available for popping).
;;   (6) It is an error to give STACK any other argument when invoking it.
;;   (7) When STACK is invoked with the first argument :EMPTY?, :POP!, or
;;       :TOP, it is an error to give it two or more arguments.
;;   (8) When STACK is invoked with the first argument :PUSH!, it is an
;;       error to give it only one argument or three or more.

(define make-stack
  (lambda ()
    (let ((stk '()))
      (lambda (message . arguments)
        (cond ((eq? message ':empty?)
               (if (null? arguments)
                   (null? stk)
                   (error "stack:empty?: no arguments are required")))

              ((eq? message ':push!)
               (cond ((null? arguments)
                      (error "stack:push!: an argument is required"))
                     ((not (null? (cdr arguments)))
                      (error "stack:push!: only one argument is required"))
                     (else
                      (set! stk (cons (car arguments) stk)))))

              ((eq? message ':pop!)
               (cond ((not (null? arguments))
                      (error "stack:pop!: no arguments are required"))
                     ((null? stk)
                      (error "stack:pop!: the stack is empty"))
                     (else
                      (let ((removed (car stk)))
                        (set! stk (cdr stk))
                        removed))))

              ((eq? message ':top)
               (cond ((not (null? arguments))
                      (error "stack:top: no arguments are required"))
                     ((null? stk)
                      (error "stack:top: the stack is empty"))
                     (else
                      (car stk))))

              (else
               (error "stack: unrecognized message")))))))

Since the field stk is allocated during the definition process, outside of the lambda-expression for the procedure being returned, it will persist as part of the object between operations on that object. Further, note that a different static variable is created each time make-stack is invoked. Thus, a program can arrange for the construction of any number of stacks, which can be pushed and popped independently.

Stacks are useful when it is necessary to interrupt or postpone part of a computation until some simpler or more urgent computation has been completed -- some description of the unfinished computation can be pushed onto a stack to make room for the simpler or more urgent one. When the latter is finished, we pop the stack to recover and resume the unfinished computation.

For example, suppose that we have a list structure containing numbers at many different levels of nesting (as described in the reading on deep recursion), and we want to find the sum of all the numbers in the tree. The approach we described there was to issue a recursive call every time we identified a non-empty subtree:

;;; sum-all-numbers: find the sum of all of the numbers in a given list
;;; structure

;; Given:
;;   TR, a list.

;; Result:
;;   SUM, a number.

;; Precondition:
;;   Every element of TR is either a number or a list constituted as TR
;;   is.

;; Postcondition:
;;   SUM is the sum of all of the numbers on all levels of TR.

(define sum-all-numbers
  (lambda (tr)
    (cond ((null? tr) 0)
          ((pair? (car tr)) (+ (sum-all-numbers (car tr))
                               (sum-all-numbers (cdr tr))))
          ((number? (car tr)) (+ (car tr) (sum-all-numbers (cdr tr))))
          (else
           (error "sum-all-numbers: non-numeric constituent in argument")))))

This is an elegant solution, but the fact that computing a result may require two recursive calls, or one, or none can be confusing. We could avoid this confusion by building up the sum as a running total, adding in the numbers one by one and considering only one subtree at any given time.

We can manage this by maintaining a stack that will at all times contain the subtrees whose contents we have not yet added to the running total. Initially, we'll set up the stack so that it contains the whole tree as its only element. Subsequently, at each step, we pop one subtree off this stack. If it is null, we discard it without changing the running total. Otherwise, it must have a car and a cdr. The cdr will be another subtree in either case, but the car might be either a number or another tree of numbers, so we check to see whether it is a pair. If so, we push both it and the cdr back onto the stack and proceed to the next step, without changing the running total. On the other hand, if the car of the current subtree is a number, we push just the cdr back onto the stack, add the car to the running total, and continue to the next step. When the stack becomes empty, everything has been added to the running total, so we stop and return the accumulated value. In Scheme:

;;; sum-all-numbers: find the sum of all of the numbers in a given list
;;; structure

;; Given:
;;   TR, a list.

;; Result:
;;   SUM, a number.

;; Precondition:
;;   Every element of TR is either a number or a list constituted as TR
;;   is.

;; Postcondition:
;;   SUM is the sum of all of the numbers on all levels of TR.

(define sum-all-numbers
  (lambda (tr)
    (let ((to-do (make-stack)))
      (to-do ':push! tr)
      (let kernel ((so-far 0))
        (if (to-do ':empty?)
            so-far
            (let ((current (to-do ':pop!)))
              (cond ((null? current)
                     (kernel so-far))
                    ((pair? (car current))
                     (to-do ':push! (cdr current))
                     (to-do ':push! (car current))
                     (kernel so-far))
                    ((number? (car current))
                     (to-do ':push! (cdr current))
                     (kernel (+ so-far (car current))))
                    (else
                     (error "sum-all-numbers: non-numeric constituent in argument")))))))))

The idea of storing subproblems that we can't address immediately and recovering them later, when we're in a better position to solve them, is a useful programming strategy -- keep an eye out for appropriate occasions to use it.

Acknowledgements

The principal author of this reading is Professor Henry Walker.

We are indebted to Sanchit Chokhani 2005 for calling our attention to an editing error in an earlier version of this reading.