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

Constructor and Purge Methods

The definitions of the constructor and the Purge methods of the StackAsLinkedList class are shown in Program gif. 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.

   program5519
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).


next up previous contents index

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