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

Constructor and Destructor

The constructor and destructor for the ChainedHashTable class are defined in Program gif. The constructor takes a single argument of type unsigned int which specifies the size of hash table desired. The constructor simply initializes the HashTable base class and the array member variable accordingly. Initializing the array variable involves constructing the required number of empty linked lists. Consequently, the running time for the ChainedHashTable constructor is O(M) where M is the size of the hash table.

   program11924
Program: ChainedHashTable Class Constructor, Destructor and Purge Member Function Definitions

The ChainedHashTable destructor simply calls the Purge member function. Since the ChainedHashTable is a container, the Purge function must delete any contained objects if it is the owner of those objects. Therefore, the Purge function is required to traverse each of the linked lists in the array.

To determine the running time of the Purge function, we will assume that there are n contained objects and the length of the array is M. Furthermore, let tex2html_wrap_inline62818 be the number of items in the tex2html_wrap_inline58387 linked list for tex2html_wrap_inline62822. Note that tex2html_wrap_inline62824. The running time of the inner loop for the tex2html_wrap_inline58387 iteration of the outer loop is tex2html_wrap_inline62828. The constant overhead arises from the loop termination test which must be done at least once. The total running time of the destructor is given by

displaymath62808

If we assume that tex2html_wrap_inline62830, the running time simplifies to O(M+n).


next up previous contents index

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