Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

accept Method

The accept method of the StackAsLinkedList class is defined in Program gif. The accept method takes a visitor and calls its visit method one-by-one for all of the objects on the stack.

   program5546
Program: StackAsLinkedList class accept method.

The implementation of the accept method for the StackAsLinkedList class mirrors that of the StackAsArray class shown in Program gif. In this case, the linked list is traversed from front to back, i.e., from the top of the stack to the bottom. As each element of the linked list is encountered, the visit method is called. If tex2html_wrap_inline60206 is the running time of the visit method, the total running time for accept is tex2html_wrap_inline60208, where tex2html_wrap_inline60196 is the number of objects in the container. If we assume that tex2html_wrap_inline60212, the total running time is O(n).


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.