Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
The Accept method of the StackAsLinkedList class is defined in Program . The Accept method takes a visitor and calls its Visit method one-by-one for all of the objects on the stack.
Program: StackAsLinkedList class Accept method.
The implementation of the Accept method for the StackAsLinkedList class mirrors that of the StackAsArray class shown in Program . 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 is the running time of the Visit method, the total running time for Accept is , where is the number of objects in the container. If we assume that , the total running time is O(n).