Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
The definitions of the constructor and the Purge methods of the StackAsLinkedList class are shown in Program . With a linked-list implementation, it is not necessary to preallocate storage space for the objects in the stack. Space is allocated dynamically and incrementally on the basis of demand.
Program: StackAsLinkedList class constructor and Purge methods.
The constructor simply creates an empty LinkedList and assigns it to the list field. Since an empty list can be created in constant time, the running time of the StackAsLinkedList constructor is O(1).
The Purge method of the StackAsLinkedList class simply calls the Purge method of the LinkedList class. The Purge method of the LinkedList class discards all the elements of the list in constant time. Consequently, the running time of the Purge method is also O(1).