Handling Complexity: Since many computer applications address complex problems, problem-solving with computers must include a consideration of the handling of complexity. Already in this course, we have seen several such strategies and use a variety of language capabilities.
For example, association lists allow a natural matching of keyword and data, so we can focus upon the storage and retrieval of these information pairs. The assoc procedure retrieves relevant data, and we need not be bothered about the details of this retrieval.
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 piles for bills that need paying, magazines that we plan to read, and notes we have taken. These piles of materials have several properties. Each pile contains some papers (information). In addition, for each pile, we can do several tasks:
When discussing these operations it is customary to call the addition of an item to the top of the pile a Push operation and the deletion of an item from the top a Pop operation.
More formally, a stack is defined as an abstract data type that can store data and that has the following operations:
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.
Classes, Objects, and Messages: One approach to problem solving involves an initial focus on data elements, operations on those elements, and relationships among data. Within computer science, such an approach motivates the viewpoint of object-oriented programming or OOP. When working within a OOP framework, it is useful to distinguish between the general notion of an abstract data type and specific uses of the ADT with definite data values. (For example, in Scheme we have found it useful to distinguish between an integer type and specific integer values.) Within OOP, the definition of an ADT (involving data variables and operations) is called a class, and specific instances of an ADT (with specific data values) are called objects.
Utilizing the notion of abstraction, we think of objects as self-contained entities, just as we might consider each pile on a desk as an separate, independent collection of material. To support this image, processing involving objects consists of sending messages to the objects, letting the object react to the object in an appropriate way, and receiving responses back. Within an OOP context, a mechanism for interpreting messages is called a method. For example, if our desk had two piles -- one for magazines and one for bills, we might have the following interaction:
> (define mag-pile ... ) ; make stack for magazines
; (details of call dependent on implementation)
> (define bill-pile ... ) ; make stack for bills
; (details of call dependent on implementation)
> (bill-pile 'push! "mortgage") ; send push message to bill-pile
> (bill-pile 'push! "doctor's bill")
> (bill-pile 'push! "credit card")
> (bill-pile 'empty?) ; send empty? message to bill-pile
#f ; response from empty? message
> (mag-pile 'empty?)
#t
> (mag-pile 'push! "Communications of the ACM - April 1997")
> (mag-pile 'push! "CS Education Bulletin - Spring 1998")
> (bill-pile 'top) ; send top message to bill-pile
"credit card" ; data returned as message response
> (mag-pile 'top)
"CS Education Bulletin - Spring 1988"
> (bill-pile 'pop!)
"credit card"
> (bill-pile 'top)
"doctor's bill"
Stacks in Scheme: To implement an abstract data type within Scheme,
we may proceed either following the recent lab on
Abstract Data Types or the text's
define-class macro. In either case, the simpest approach is to
maintain an internal list within the abstract data type or class. With
this approach, push and pop operations insert or delete an
item from the front of the list, the empty operation tests if the
list is empty, and the top operation returns the head of the list.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153/lab-stacks.html