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

Inserting and Removing Items

Program gif gives the code for inserting and removing items from a ChainedHashTable.

   program11952
Program: ChainedHashTable Class Insert and Withdraw Member Function Definitions

The implementations of the Insert and Withdraw functions are remarkably simple. For example, the Insert function first calls the hash function H to compute an array index which is used to select one of the linked lists. The Append member function provided by the LinkedList<T> class is used to add a pointer to the object to the selected linked list. The total running time for the Insert operation is tex2html_wrap_inline62834, where tex2html_wrap_inline62836 is the running time of the Object::Hash function. Notice that if the hash function runs in constant time, then so too does hash table insertion operation!

The Withdraw function is almost identical to the Insert function. Instead of calling the Append, it calls the linked list Extract function to remove the specified object from the appropriate linked list. The running time of Withdraw is determined by the time of the Extract operation. In Chapter gif this was shown to be O(n) where n is the number of items in the linked list. In the worst case, all of the items in the ChainedHashTable have collided with each other and ended up in the same list. I.e., in the worst case if there are n items in the container, all n of them are in a single linked list. In this case, the running time of the Withdraw operation is tex2html_wrap_inline62846.


next up previous contents index

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