unit cStack; { cStack } { Copyright © 1992 by Michael J. Gibbs, all rights reserved. } { } { This unit provides classes that manage a stack of objects. } { } { iStack - Initializes a new stack object to empty and allocates a list } { object to contain the associated data. } { push - Adds 'anObject' to the top of the stack object. } { pop - Returns the object at the top of the stack as the function } { result and removes the object from the stack. } { tos - Returns the object at the top of the stack as the function } { result without removing it from the stack. } { getNumElements - Returns the number of objects in the stack as the function } { result. } { free - Releases the stack object. NOTE--this does not release the } { individual objects that are contained in the stack, if any. } interface uses objIntf, listObjs; type cStack = object(tObject) itsStackList: cList; procedure iStack; procedure push (anObject: tObject); function pop: tObject; function tos: tObject; function getNumElements: integer; procedure free; override; end; implementation procedure cStack.iStack; { Initialize the stack } begin new(itsStackList); itsStackList.iList; end; { cStack.iStack } procedure cStack.push (anObject: tObject); { Push an object onto the stack } begin itsStackList.prependToList(anObject); end; { cStack.push } function cStack.pop: tObject; { Pop an object off the stack } begin pop := itsStackList.getElement(1); itsStackList.deleteElement(1); end; { cStack.pop } function cStack.tos: tObject; { Return the object on the top of the stack without popping it } begin tos := itsStackList.getElement(1); end; { cStack.tos } function cStack.getNumElements: integer; { Return the size of the stack } begin getNumElements := itsStackList.getNumElements; end; { cStack.getNumElements } procedure cStack.free; { Free the stack } begin itsStackList.free; inherited free; end; { cStack.free } end.