238
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

Embed Size (px)

Citation preview

Page 1: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

1

C++ Classes and Data StructuresJeffrey S. Childs

Chapter 11

Hash Tables

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Page 2: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

2

Hash Table ADT

• The hash table is a table of elements that have keys

• A hash function is used for locating a position in the table

• The table of elements is the set of data acted upon by the hash table operations

Page 3: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

3

Hash Table ADT Operations

• insert, to insert an element into a table

• retrieve, to retrieve an element from the table

• remove, to remove an element from the table

• update, to update an element in the table

• an operation to empty out the hash table

Page 4: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

4

Fast Search

• A hash table uses a function of the key value of an element to identify its location in an array.

• A search for an element can be done in ( 1 ) time.

• The function of the key value is called a hash function.

Page 5: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

5

Hash Functions

• The input into a hash function is a key value

• The output from a hash function is an index of an array (hash table) where the object containing the key is located

• Example of a hash function:h( k ) = k % 100

Page 6: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

6

Example Using a Hash Function

• Suppose our hash function is:h( k ) = k % 100

• We wish to search for the object containing key value 214

• k is set to 214 in the hash function• The result is 14• The object containing key value 214 is stored at

index 14 of the array (hash table)• The search is done in ( 1 ) time

Page 7: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

7

Inserting an Element

• An element is inserted into a hash table using the same hash functionh( k ) = k % 100

• To find where an element is to be inserted, use the hash function on its key

• If the key value is 214, the object is to be stored in index 14 of the array

• Insertion is done in ( 1 ) time

Page 8: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

8

Consider the Big Picture …

• If we have millions of key values, it may take a long time to search a regular array or a linked list for a specific part number (on average, we might compare 500,000 key values)

• Using a hash table, we simply have a function which provides us with the index of the array where the object containing the key is located

Page 9: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

9

Collisions

• Consider the hash function– h( k ) = k % 100

• A key value of 393 is used for an object, and the object is stored at index 93

• Then a key value of 193 is used for a second object; the result of the hash function is 93, but index 93 is already occupied

• This is called a collision

Page 10: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

10

How are Collisions Resolved?

• The most popular way to resolve collisions is by chaining

• Instead of having an array of objects, we have an array of linked lists, each node of which contains an object

• An element is still inserted by using the hash function -- the hash function provides an index of a linked list, and the element is inserted at the front of that (usually short) linked list

• When searching for an element, the hash function is used to get the correct linked list, then the linked list is searched for the key (still much faster than comparing 500,000 keys)

Page 11: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

11

0

1

2

3

4

5

6

A hash table which is initially empty.

Every element is a LinkedList object. Only the start pointer of the LinkedList object is shown, which is set to NULL.

The hash function is:

h( k ) = k % 7

Example Using Chaining

Page 12: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

12

0

1

2

3

4

5

6

INSERT object with key 31

Example Using Chaining(cont.)

The hash function is:

h( k ) = k % 7

Page 13: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

13

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 31

31 % 7 is 3

Example Using Chaining(cont.)

Page 14: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

14

0

1

2

3

4

5

6

31

The hash function is:

h( k ) = k % 7

INSERT object with key 31

31 % 7 is 3

Example Using Chaining(cont.)

Page 15: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

15

0

1

2

3

4

5

6

Note: The whole object is stored but only the key value is shown

The hash function is:

h( k ) = k % 7

INSERT object with key 31

31 % 7 is 3

Example Using Chaining(cont.)

31

Page 16: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

16

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

31

0

1

2

3

4

5

6

Page 17: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

17

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 9

Example Using Chaining(cont.)

31

Page 18: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

18

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

31

Page 19: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

19

0

1

2

3

4

5

6

9

The hash function is:

h( k ) = k % 7

INSERT object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

31

Page 20: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

20

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

9

31

Page 21: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

21

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 36

Example Using Chaining(cont.)

9

31

Page 22: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

22

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 36

36 % 7 is 1

Example Using Chaining(cont.)

9

31

Page 23: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

23

0

1

2

3

4

5

6

36

The hash function is:

h( k ) = k % 7

INSERT object with key 36

36 % 7 is 1

Example Using Chaining(cont.)

9

31

Page 24: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

24

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

36

9

31

Page 25: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

25

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 42

Example Using Chaining(cont.)

36

9

31

Page 26: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

26

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 42

42 % 7 is 0

Example Using Chaining(cont.)

36

9

31

Page 27: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

27

Example Using Chaining(cont.)

0

1

2

3

4

5

6

42

The hash function is:

h( k ) = k % 7

INSERT object with key 42

42 % 7 is 0

36

9

31

Page 28: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

28

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

42

36

9

31

Page 29: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

29

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 46

Example Using Chaining(cont.)

42

36

9

31

Page 30: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

30

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 46

46 % 7 is 4

Example Using Chaining(cont.)

42

36

9

31

Page 31: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

31

0

1

2

3

4

5

6

46

The hash function is:

h( k ) = k % 7

INSERT object with key 46

46 % 7 is 4

Example Using Chaining(cont.)

42

36

9

31

Page 32: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

32

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

46

42

36

9

31

Page 33: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

33

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 20

Example Using Chaining(cont.)

46

42

36

9

31

Page 34: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

34

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 20

20 % 7 is 6

Example Using Chaining(cont.)

46

42

36

9

31

Page 35: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

35

0

1

2

3

4

5

6 20

The hash function is:

h( k ) = k % 7

INSERT object with key 20

20 % 7 is 6

Example Using Chaining(cont.)

46

42

36

9

31

Page 36: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

36

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 37: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

37

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 38: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

38

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 39: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

39

0

1

2

3

4

5

6

COLLISION occurs…

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 40: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

40

0

1

2

3

4

5

6

But key 2 is just inserted in the linked list

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 41: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

41

0

1

2

3

4

5

6

The insert function of LinkedList inserts a new element at the BEGINNING of the list

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 42: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

42

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 43: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

43

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 44: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

44

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 45: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

45

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 46: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

46

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

9

31

Page 47: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

47

0

1

2

3

4

5

6

9

The hash function is:

h( k ) = k % 7

INSERT object with key 2

2 % 7 is 2

Example Using Chaining(cont.)

20

46

42

36

2

31

Page 48: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

48

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 49: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

49

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 24

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 50: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

50

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 24

24 % 7 is 3

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 51: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

51

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 24

24 % 7 is 3

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 52: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

52

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 24

24 % 7 is 3

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 53: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

53

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 24

24 % 7 is 3

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 54: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

54

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

INSERT object with key 24

24 % 7 is 3

Example Using Chaining(cont.)

9

20

46

42

36

2

31

Page 55: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

55

0

1

2

3

4

5

6

31

The hash function is:

h( k ) = k % 7

INSERT object with key 24

24 % 7 is 3

Example Using Chaining(cont.)

9

20

46

42

36

2

24

Page 56: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

56

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

Example Using Chaining(cont.)

31

9

20

46

42

36

2

24

Page 57: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

57

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

Example Using Chaining(cont.)

31

9

20

46

42

36

2

24

Page 58: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

58

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

31

9

20

46

42

36

2

24

Page 59: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

59

0

1

2

3

4

5

6

We search this linked list for the object with key 9

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

31

9

20

46

42

36

2

24

Page 60: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

60

0

1

2

3

4

5

6

Remember…the whole object is stored, only the key is shown

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

31

9

20

46

42

36

2

24

Page 61: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

61

0

1

2

3

4

5

6

Does this object contain key 9?

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

31

9

20

46

42

36

2

24

Page 62: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

62

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

Does this object contain key 9? No, so go on to the next object.

31

9

20

46

42

36

2

24

Page 63: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

63

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

Does this object contain key 9?

31

9

20

46

42

36

2

24

0

1

2

3

4

5

6

Page 64: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

64

0

1

2

3

4

5

6

The hash function is:

h( k ) = k % 7

**FIND** the object with key 9

9 % 7 is 2

Example Using Chaining(cont.)

Does this object contain key 9? YES, found it! Return the object.

31

9

20

46

42

36

2

24

Page 65: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

65

Uniform Hashing

• When the elements are spread evenly (or near evenly) among the indexes of a hash table, it is called uniform hashing

• If elements are spread evenly, such that the number of elements at an index is less than some small constant, uniform hashing allows a search to be done in ( 1 ) time

• The hash function largely determines whether or not we will have uniform hashing

Page 66: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

66

Bad Hash Functions

• h( k ) = 5 is obviously a bad hash function

• h( k ) = k % 100 could be a bad hash function if there is meaning attached to parts of a key– Consider that the key might be an employee

id– The last two digits may give the state of birth

Page 67: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

67

Ideal Hash Function for Uniform Hashing

• The hash table size should be a prime number that is not too close to a power of 2

• 31 is a prime number but is too close to a power of 2

• 97 is a prime number not too close to a power of 2

• A good hash function might be:h( k ) = k % 97

Page 68: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

68

Hash Functions Can be Made for Keys that are Strings

1 int sum = 0;2 for ( int i = 0; i < int( str.length( ) ); i++ ) 3 sum += str[ i ];4 hash_index = sum % 97;

Page 69: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

69

Speed vs. Memory Conservation

• Speed comes from reducing the number of collisions

• In a search, if there are no collisions, the first element in the linked list in the one we want to find (fast)

• Therefore, the greatest speed comes about by making a hash table much larger than the number of keys (but there will still be an occasional collision)

Page 70: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

70

Speed vs. Memory Conservation

(cont.)

• Each empty LinkedList object in a hash table wastes 8 bytes of memory (4 bytes for the start pointer and 4 bytes for the current pointer)

• The best memory conservation comes from trying to reduce the number of empty LinkedList objects

• The hash table size would be made much smaller than the number of keys (there would still be an occasional empty linked list)

Page 71: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

71

Hash Table Design

• Decide whether speed or memory conservation is more important (and how much more important) for the application

• Come up with a good table size which– Allows for the use of a good hash function– Strikes the appropriate balance between

speed and memory conservation

Page 72: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

72

Ideal Hash Tables

• Can we have a hash function which guarantees that there will be no collisions?

• Yes:h( k ) = k

• Each key k is unique; therefore, each index produced from h( k ) is unique

• Consider 300 employees that have a 4 digit id• A hash table size of 10000 with the hash

function above guarantees the best possible speed

Page 73: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

73

Ideal Hash Tables (cont.)

• Should we use LinkedList objects if there are no collisions?

• Suppose each Employee object takes up 100 bytes• An array size of 10000 Employee objects with only 300

used indexes will have 9700 unused indexes, each taking up 100 bytes

• Best to use LinkedList objects (in this case) – the 9700 unused indexes will only use 8 bytes each

• Additional space can be saved by not storing the employee id in the object (if no collisions)

Page 74: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

74

Ideal Hash Tables (cont.)

• Can we have a hash table without any collisions and without any empty linked lists?

• Sometimes. Consider 300 employees with id’s from 0 to 299. We can make a hash table size of 300, and use h( k ) = k

• LinkedList objects wouldn’t be necessary and in fact, would waste space

• It would also not be necessary to store the employee id in the object

Page 75: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

75

Implementing aHash Table

• We’ll implement a HashTable with linked lists (chaining)– without chaining, a hash table can become full

• If the client has the ideal hash table mentioned on the previous slide, he/she would be better off to just use an Array for the hash table

Page 76: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

76

Implementing a Hash Function

• We shouldn’t write the hash function

• The client should write the hash function that he/she would like to use

• Then, the client should pass the hash function that he/she wrote as a parameter into the constructor of the HashTable class

• This can be implemented with function pointers

Page 77: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

77

Function Pointers

• A function pointer is a pointer that holds the address of a function

• The function can be called using the function pointer instead of the function name

Page 78: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

78

Function Pointers (cont.)

• Example of a function pointer declaration:

float (*funcptr) (string);

Page 79: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

79

Function Pointers (cont.)

• Example of a function pointer declaration:

float (*funcptr) (string);

funcptr is the name of the pointer; the name can be chosen like any other pointer name

Page 80: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

80

Function Pointers (cont.)

• Example of a function pointer declaration:

float (*funcptr) (string);

The parentheses are necessary.

Page 81: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

81

Function Pointers (cont.)

• Example of a function pointer declaration:

float (*funcptr) (string);

The return type of the function that funcptr can point to is given here (in this case, the return type is a float)

Page 82: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

82

Function Pointers (cont.)

• Example of a function pointer declaration:

float (*funcptr) (string);

The parameter list of a function that funcptr can point to is given here – in this case, there is only one parameter of string type.

Page 83: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

83

Function Pointers (cont.)

• Example of a function pointer declaration:

float (*funcptr) (string);

• What would a function pointer declaration look like if the function it can point to has a void return type and accepts two integer parameters?

Page 84: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

84

Function Pointers (cont.)

void (*fp) (int, int);

Page 85: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

85

Function Pointers (cont.)

void (*fp) (int, int);

void foo( int a, int b ){

cout << “a is: “ << a << endl;cout << “b is: “ << b << endl;

}

A function that fp can point to

Page 86: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

86

Assigning the Address of a Function

to a Function Pointer void (*fp) (int, int);

void foo( int a, int b ){

cout << “a is: “ << a << endl;cout << “b is: “ << b << endl;

}

fp = foo;The address of foo is assigned to fp like this

Page 87: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

87

Calling a Function by Using a

Function Pointer

Once the address of foo has been assigned to fp, the foo function can be called using fp like this

void (*fp) (int, int);

void foo( int a, int b ){

cout << “a is: “ << a << endl;cout << “b is: “ << b << endl;

}

fp( 5, 10 );

Page 88: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

88

Design of theHashTable Constructor

• Once the client designs the hash function, the client passes the name of the hash function, as a parameter into the HashTable constructor

• The HashTable constructor accepts the parameter using a function pointer in this parameter location

• The address of the function is saved to a function pointer in the private section

• Then, the hash table can call the hash function that the client made by using the function pointer

Page 89: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

89

HashTable.h1 #include "LinkedList.h"2 #include "Array.h“34 template <class DataType>5 class HashTable 6 {7 public:8 HashTable( int (*hf)(const DataType &), int s );9 bool insert( const DataType & newObject ); 10 bool retrieve( DataType & retrieved ); 11 bool remove( DataType & removed ); 12 bool update( DataType & updateObject ); 13 void makeEmpty( ); HashTable.h continued…

Page 90: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

90

HashTable.h

14 private:15 Array< LinkedList<DataType> > table;16 int (*hashfunc)(const DataType &); 17 };1819 #include "HashTable.cpp"

Space is necessary here

Page 91: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

91

Clientele

• The LinkedList class is being used in the HashTable class, along with the Array class

• Note that when one writes a class the clientele extends beyond the main programmers who might use the class

• The clientele extends to people who write other classes

Page 92: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

92

HashTable Constructor

1 template <class DataType>2 HashTable<DataType>::HashTable( 3 int (*hf)(const DataType &), int s )4 : table( s )5 {6 hashfunc = hf;7 }

This call to the Array constructor creates an Array of LinkedList’s of type DataType

Page 93: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

93

HashTable Constructor(cont.)

1 template <class DataType>2 HashTable<DataType>::HashTable( 3 int (*hf)(const DataType &), int s )4 : table( s )5 {6 hashfunc = hf;7 }

The DataType for Array is LinkedList<DataType> (DataType in Array is different than DataType in HashTable)

Page 94: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

94

HashTable Constructor(cont.)

1 template <class DataType>2 HashTable<DataType>::HashTable( 3 int (*hf)(const DataType &), int s )4 : table( s )5 {6 hashfunc = hf;7 }

In the Array constructor, an Array of size s is made, having LinkedList elements – when this array is created, the LinkedList constructor is called for each element.

Page 95: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

95

HashTable Constructor(cont.)

1 template <class DataType>2 HashTable<DataType>::HashTable( 3 int (*hf)(const DataType &), int s )4 : table( s )5 {6 hashfunc = hf;7 }

Page 96: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

96

insert

8 template <class DataType>89 bool HashTable<DataType>::insert( 10 const DataType & newObject )11 {12 int location = hashfunc( newObject );13 if ( location < 0 || location >= table.length( ) )14 return false;15 table[ location ].insert( newObject ); 16 return true;17 } Keep in mind that this is a

LinkedList object.

Page 97: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

97

retrieve

18 template <class DataType>19 bool HashTable<DataType>::retrieve( 20 DataType & retrieved )21 {22 int location = hashfunc( retrieved );23 if ( location < 0 || location >= table.length( ) )24 return false;25 if ( !table[ location ].retrieve( retrieved ) )26 return false;27 return true;28 }

Page 98: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

98

remove

29 template <class DataType>30 bool HashTable<DataType>::remove( 31 DataType & removed )32 {33 int location = hashfunc( removed );34 if ( location < 0 || location >= table.length( ) )35 return false;36 if ( !table[ location ].remove( removed ) )37 return false;38 return true;39 }

Page 99: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

99

update40 template <class DataType>41 bool HashTable<DataType>::update( 42 DataType & updateObject )43 {44 int location = hashfunc( updateObject );45 if ( location < 0 || location >= table.length( ) )46 return false;47 if ( !table[location].find( updateObject ) )48 return false;49 table[location].replace( updateObject );50 return true;51 }

Page 100: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

100

makeEmpty

50 template <class DataType>51 void HashTable<DataType>::makeEmpty( )52 {53 for ( int i = 0; i < table.length( ); i++ )54 table[ i ].makeEmpty( );55 }

Page 101: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

101

Using HashTable

1 #include <iostream>2 #include <string>3 #include "HashTable.h"45 using namespace std;67 struct MyStruct {8 string str;9 int num;10 bool operator ==( const MyStruct & r ) { return str == r.str; }11 };

str will be the key

Page 102: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

102

Using HashTable(cont.)

1 #include <iostream>2 #include <string>3 #include "HashTable.h"45 using namespace std;67 struct MyStruct {8 string str;9 int num;10 bool operator ==( const MyStruct & r ) { return str == r.str; }11 };

It is necessary to overload the == operator for the LinkedList functions

Page 103: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

103

Using HashTable(cont.)

1 #include <iostream>2 #include <string>3 #include "HashTable.h"45 using namespace std;67 struct MyStruct {8 string str;9 int num;10 bool operator ==( const MyStruct & r ) { return str == r.str; }11 };

In the actual code, a comment is placed above HashTable, telling the client that this is needed and what is required.

Page 104: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

104

Using HashTable(cont.)

12 const int SIZE1 = 97, SIZE2 = 199;1314 int hash1( const MyStruct & obj );15 int hash2( const MyStruct & obj );1617 int main( )18 {19 HashTable<MyStruct> ht1( hash1, SIZE1 ), 20 ht2( hash2, SIZE2);

Page 105: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

105

Using HashTable(cont.)

21 MyStruct myobj;2223 myobj.str = "elephant";24 myobj.num = 25;25 ht1.insert( myobj );2627 myobj.str = "giraffe";28 myobj.num = 50;29 ht2.insert( myobj );

…// other code using the hash tables…

Page 106: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

106

Using HashTable(cont.)

30 return 0;31 }3233 int hash1( const MyStruct & obj )34 {35 int sum = 0;36 for ( int i = 0; i < 3 && i < int( obj.str.length( ) ); i++ )37 sum += obj.str[ i ];38 return sum % SIZE1;39 }

Page 107: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

107

A Hash Table is Like a List

• The hash table ADT description is very close to the list ADT description

• The only items missing from the hash table ADT description are:– an iterator– a function to determine when the “list” is empty– find, to determine whether an element is in the “list”– a current position

• If we had all of these, we would have a fast list (or an enhanced hash table)

Page 108: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

108

Iterator

• Everything would be easy to implement for the hash table, except for the iterator

• The iterator is an important part of a “list”, so we would like it to be as fast as possible

• We can iterate through a collision list, but finding the next collision list to iterate through might not be so fast…

Page 109: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

109

Iterator (cont.)

table

Large gap with empty linked lists

.

.

.

Page 110: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

110

Iterator (cont.)

• Instead, we can have a linked list run through the collision lists, so that the linked list contains every element

• Then, iterating through the linked list is simple and fast

Page 111: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

111

Time Complexitiesfor List ADT

• insert – we’ll insert at the head of the linked list – ( 1 )

• iterator – each step will be ( 1 ) • find – element is found by hashing, so it is ( 1 )

for uniform hashing (the hash function and hash table are designed so that the length of the collision list is bounded by some small constant)

• retrieve – is ( 1 ) for uniform hashing• more…

Page 112: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

112

Time Complexitiesfor List ADT (cont.)

• replace – is ( 1 ) using the current position• an operation to determine whether or not the list

is empty – is ( 1 ), because we just test the linked list to see if it is empty

• an operation to empty out the list – is ( n ), the best we can do, since each node must be freed

• remove – to make this last operation as fast as possible, consider using a doubly-linked list for the linked list…

Page 113: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

113

Remove

• In Chapter 10, we’ve seen that a node in a doubly-linked list can be removed in ( 1 ) time, given a pointer to it

• Using hashing, we obtain a collision list, which will have a pointer to the node we wish to remove

Page 114: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

114

Doubly-Linked List ADT

• The description for the doubly-linked list ADT is the same as that for the list ADT

• We don’t consider implementation in the ADT description, and double links have to do with implementation

• The implementation of the doubly-linked list using the HashTable is also not a part of the ADT description

Page 115: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

115

Avoiding SpecialCases

• To avoid special cases, we’ll have a header node and a trailer node in the doubly-linked list

• Few data structures use arrays of doubly-linked lists – if such a use arises, we could create a doubly-linked list without header and trailer nodes to avoid wasting memory

Page 116: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

116

An Example

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

The red line is the doubly-linked list

Page 117: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

117

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

Each node has three pointers (not just one).

Page 118: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

118

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

The elements were inserted in this order: 33, 65, 63, 31, 53, 22, 47, 99, 36, 70

Page 119: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

119

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

33, 65, 63, 31, 53, 22, 47, 99, 36, 70 – since each node is inserted at the beginning …

Page 120: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

120

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

33, 65, 63, 31, 53, 22, 47, 99, 36, 70 – you’ll see these nodes from trailer to header.

Page 121: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

121

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52

Page 122: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

122

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 The hash function gives us index 8.

Page 123: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

123

An Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there…

Page 124: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

124

An Example (cont.)

4722

99

33

36 7063

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there…

Page 125: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

125

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there.

Page 126: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

126

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there.

52

Page 127: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

127

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

The new node, 52, must also be inserted at the beginning of the doubly-linked list…

52

Page 128: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

128

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

The new node, 52, must also be inserted at the beginning of the doubly-linked list…

52

Page 129: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

129

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

The new node, 52, must also be inserted at the beginning of the doubly-linked list

52

Page 130: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

130

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31

52

Page 131: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

131

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 The hash function gives us index 9, where we’ll find 31

52

Page 132: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

132

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 Node 53 contains the pointer to it…

52

Page 133: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

133

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

so 31 can be removed from the doubly-linked list in ( 1 ) time…

52

Page 134: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

134

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

so 31 can be removed from the doubly-linked list in ( 1 ) time

52

Page 135: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

135

An Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

31 is also removed from the collision list using LinkedList remove…

52

Page 136: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

136

An Example (cont.)

4722

99

33

36 70

63

53 65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

31 is also removed from the collision list using LinkedList remove

52

Page 137: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

137

Doubly-LinkedList Order

• The order of the doubly-linked list can be maintained independently of the singly-linked list

• If we wanted a sorted doubly-linked list using the same insertion order of the elements, it would look like this…

Page 138: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

138

Sorted Example

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

Page 139: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

139

Sorted Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52

Page 140: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

140

Sorted Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 The hash function gives us index 8.

Page 141: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

141

Sorted Example (cont.)

4722

99

33

36 70 63 53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there…

Page 142: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

142

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there…

Page 143: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

143

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there.

Page 144: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

144

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

INSERT: 52 So 52 is inserted at the beginning of the collision list there.

52

Page 145: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

145

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

52 must also be inserted in the doubly-linked list…

52

Page 146: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

146

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

52

52 must also be inserted in the doubly-linked list.

Page 147: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

147

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

52

52 must also be inserted in the doubly-linked list. However…

Page 148: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

148

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

52

it would take ( n ) time since its position in the doubly-linked list must be found

Page 149: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

149

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31

52

Page 150: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

150

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 The hash function gives us index 9, where we’ll find 31

52

Page 151: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

151

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 In ( 1 ) time, it is removed from the doubly-linked list…

52

Page 152: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

152

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 In ( 1 ) time, it is removed from the doubly-linked list

52

Page 153: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

153

Sorted Example (cont.)

4722

99

33

36 70

63

53

31

65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 In ( 1 ) time, it is also removed from the collision list…

52

Page 154: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

154

Sorted Example (cont.)

4722

99

33

36 70

63

53 65

0 1 2 3 4 5 6 7 8 9 10

trailer

header

hash function:

h( k ) = k % 11

REMOVE: 31 In ( 1 ) time, it is also removed from the collision list

52

Page 155: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

155

Memory Considerations

• Each node has three pointers now:

template <class DataType>struct Node {

DataType info;Node<DataType> *next;Node<DataType> *dlnext;Node<DataType> *dlback;

};

For the collision list

Page 156: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

156

Memory Considerations(cont.)

• Each node has three pointers now:

template <class DataType>struct Node {

DataType info;Node<DataType> *next;Node<DataType> *dlnext;Node<DataType> *dlback;

};

For the doubly-linked list

Page 157: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

157

Memory Considerations(cont.)

• If there is only one node in the collision list, on average, then the LinkedList used for each element also has two pointers: start and current

• This gives us a total of 20 bytes of wasted memory per node (housekeeping)

Page 158: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

158

Memory Considerations(cont.)

• If each element is 20 bytes, 50% of memory is wasted

• However, if each element is 980 bytes, only 2% of memory is wasted

• Element size is an important consideration

Page 159: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

159

LinkedListConsiderations

• In order to make the implementation easy, we’ll have to make some changes to the LinkedList class

• It will become a specialized “helper” class for the doubly-linked list

Page 160: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

160

Changes toLinkedList

• change the name of the class from LinkedList to CollisionList

• modify the Node struct so it has the three pointers we need

• we need a function to retrieve the current pointer, called getcurrent

• instead of eliminating the current position when we insert a node, we will set the current position to the node we inserted

Page 161: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

161

HashTable ClassConsiderations

• We’ll make some changes to the HashTable class, too

• It will, again, be a specialized “helper” class just for use in the doubly-linked list

Page 162: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

162

HashTable Changes

• rename the HashTable class to DLHashTable, short for “Doubly-Linked Hash Table”

• keep the location, used throughout the class, as a private data member

• have a function getcurrent, which retrieves the current pointer of the CollisionList that was used in the last use of location; that is, return table[location].getcurrent( )

Page 163: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

163

HashTableChanges

• We’ll also need some functions which are convenient for the copy constructor and deepCopy

• gethashfunc, which will return the hash function pointer

• sethashfunc, which will set a hash function pointer

• getsize, which will get the Array size of the hash table

• changeSize, which will change the Array size of the hash table

Page 164: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

164

Returning a Function Pointer

int (*gethashfunc( ) const)(const DataType &);

This is the function prototype for gethashfunc, the function to return the hash function pointer.

Page 165: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

165

Returning a Function Pointer

(cont.)

int (*gethashfunc( ) const)(const DataType &);

This is the return type of the function – the return type is spread out instead of being at the beginning of the function heading, where it normally is.

Page 166: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

166

Returning a Function Pointer

(cont.)

int (*gethashfunc( ) const)(const DataType &);

The return type indicates we are returning a function pointer which can point to a function that passes in DataType by const reference and returns an integer (describing a hash function)

Page 167: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

167

Returning a Function Pointer

(cont.)

int (*gethashfunc( ) const)(const DataType &);

This is the name of the function that returns the function pointer – it has an empty parameter list in this case.

Page 168: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

168

Returning a Function Pointer

(cont.)

int (*gethashfunc( ) const)(const DataType &);

This const means that we are not going to change any of the data members in this function (it is the same use of const that you normally see at the end of an isEmpty function heading).

Page 169: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

169

Returning a Function Pointer

(cont.)

template <class DataType>int (*DLHashTable<DataType>::gethashfunc( ) const)

(const DataType &) {

return hashfunc;}

The function definition in the implementation file

Page 170: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

170

DoublyLinkedList.h1 #include "DLHashTable.h"23 template <class DataType>4 class DoublyLinkedList5 {6 public:7 DoublyLinkedList( 8 int (*hf)(const DataType &), int s ); 9 DoublyLinkedList( 10 const DoublyLinkedList<DataType> & aplist );11 ~DoublyLinkedList( );12 DoublyLinkedList<DataType> & operator =( 13 const DoublyLinkedList<DataType> & rlist );

Page 171: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

171

DoublyLinkedList.h(cont.)

14 bool insert( const DataType & element ); 15 bool first( DataType & listEl );16 inline bool getNext( DataType & listEl );17 bool last( DataType & listEl );18 inline bool getPrevious( DataType & listEl ); 19 bool find ( const DataType & element ); 20 bool retrieve( DataType & element ); 21 bool remove( DataType & element ); 22 bool replace( const DataType & newElement ); 23 bool isEmpty( ) const;24 void makeEmpty( );

another iterator

Page 172: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

172

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

These are static nodes (not made in the heap).

Page 173: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

173

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

header will point to headerNode

Page 174: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

174

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

likewise, trailer will point to trailerNode

Page 175: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

175

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

We don’t really need the header and trailer pointers, but without them the code may be confusing…

Page 176: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

176

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

Without header, we would access the first data node with:

headerNode.dlnext

Page 177: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

177

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

WITH header, we would access the first data node with:

header->dlnext

Page 178: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

178

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

With this private section, do we really need a destructor, copy constructor, and overloaded assignment operator?

Page 179: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

179

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

YES. There can be pointers to dynamic memory here (for example, header->next).

Page 180: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

180

DoublyLinkedList.h(cont.)

25 private:26 DLHashTable<DataType> table;27 Node<DataType> *current;28 Node<DataType> headerNode;29 Node<DataType> trailerNode;30 Node<DataType> *header;31 Node<DataType> *trailer;32 inline void deepCopy( 33 const DoublyLinkedList<DataType> & original );34 };3536 #include "DoublyLinkedList.cpp"

Sometimes pointers to dynamic memory are hidden.

Page 181: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

181

Constructor

1 template <class DataType>2 DoublyLinkedList<DataType>::DoublyLinkedList(3 int (*hf)(const DataType &), int s )4 : table( hf, s ), header( &headerNode ), 5 trailer( &trailerNode )6 {7 current = header->dlnext = trailer;8 trailer->dlback = header;9 }

When current is set to the trailer node, it means there is no current position

Page 182: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

182

Copy Constructor

10 template <class DataType>11 DoublyLinkedList<DataType>::DoublyLinkedList( 12 const DoublyLinkedList<DataType> & aplist )13 : table( aplist.table.gethashfunc( ), aplist.table.getsize( ) )15 {16 deepCopy( aplist );17 } Calls the constructor for

DLHashTable in the copy – passes in the hash function and size of the aplist hash table.

Page 183: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

183

Destructor

18 template <class DataType>19 DoublyLinkedList<DataType>::~DoublyLinkedList( )20 {21 makeEmpty( );22 }

Page 184: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

184

Overloaded AssignmentOperator

23 template <class DataType>24 DoublyLinkedList<DataType> & 25 DoublyLinkedList<DataType>::operator =( 26 const DoublyLinkedList<DataType> & rlist )27 {28 if ( this == &rlist )29 return *this;30 makeEmpty( );31 deepCopy( rlist );32 return *this;33 }

Page 185: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

185

insert

34 template <class DataType>35 bool DoublyLinkedList<DataType>::insert( 36 const DataType & element )37 {38 if ( !table.insert( element ) )39 return false;4041 current = table.getcurrent( );

When insert in the CollisionList is (eventually) used, it now makes the inserted node the current node.

Page 186: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

186

insert (cont.)

34 template <class DataType>35 bool DoublyLinkedList<DataType>::insert( 36 const DataType & element )37 {38 if ( !table.insert( element ) )39 return false;4041 current = table.getcurrent( );

This function eventually calls getcurrent in the CollisionList, which returns the current pointer there…

Page 187: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

187

insert (cont.)

34 template <class DataType>35 bool DoublyLinkedList<DataType>::insert( 36 const DataType & element )37 {38 if ( !table.insert( element ) )39 return false;4041 current = table.getcurrent( );

So the address of the node that was just inserted is assigned to the (different) current pointer here

Page 188: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

188

insert (cont.)

34 template <class DataType>35 bool DoublyLinkedList<DataType>::insert( 36 const DataType & element )37 {38 if ( !table.insert( element ) )39 return false;4041 current = table.getcurrent( );

insert continued…

Page 189: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

189

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

info dlback dlnext next

In showing how insert works, we’ll use this convention for the parts of Node.

Page 190: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

190

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

startThe current node has been inserted into the collision list (line 38 of insert)

Page 191: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

191

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

startHowever, it still needs to be placed into the doubly-linked list.

Page 192: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

192

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 193: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

193

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 194: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

194

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 195: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

195

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 196: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

196

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 197: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

197

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 198: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

198

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 199: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

199

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 200: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

200

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Page 201: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

201

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 }

doubly-linked list

collision list

currentheader

start

Now, current has been inserted into the doubly-linked list.

Page 202: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

202

insert (cont.)42 current->dlnext = header->dlnext;43 current->dlback = header;44 header->dlnext = header->dlnext->dlback = current;45 current = trailer;4647 return true;48 } There won’t be a current

position in the DoublyLinkedList after the insertion – the client is using the DoublyLinkedList and this is what the ADT describes.

Page 203: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

203

first

49 template <class DataType>50 bool DoublyLinkedList<DataType>::first( 51 DataType & listEl )52 {53 if ( header->dlnext == trailer ) 54 return false;5556 current = header->dlnext;57 listEl = current->info;58 return true;59 }

Page 204: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

204

getNext60 template <class DataType>61 inline bool DoublyLinkedList<DataType>::getNext( 62 DataType & listEl ) 63 {64 if ( current->dlnext == trailer )65 current = trailer;66 if ( current == trailer ) 67 return false;6869 current = current->dlnext;70 listEl = current->info;71 return true;72 }

Page 205: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

205

last

73 template <class DataType>74 bool DoublyLinkedList<DataType>::last( 75 DataType & listEl )76 {77 if ( header->dlnext == trailer ) 78 return false;7980 current = trailer->dlback;81 listEl = current->info;82 return true;83 }

Page 206: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

206

getPrevious84 template <class DataType>85 inline bool DoublyLinkedList<DataType>::getPrevious( 86

DataType & listEl )87 {88 if ( current->dlback == header )89 current = trailer;90 if ( current == trailer )91 return false;9293 current = current->dlback;94 listEl = current->info;95 return true;96 }

Page 207: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

207

find97 template <class DataType>98 bool DoublyLinkedList<DataType>::find( 99 const DataType & element )100 {101 DataType el = element;102 if ( table.retrieve( el ) ) {103 current = table.getcurrent( );104 return true;105 }106107 current = trailer;108 return false;109 }

If we pass in element here, retrieve will change the value of it…

Page 208: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

208

find (cont.)97 template <class DataType>98 bool DoublyLinkedList<DataType>::find( 99 const DataType & element )100 {101 DataType el = element;102 if ( table.retrieve( el ) ) {103 current = table.getcurrent( );104 return true;105 }106107 current = trailer;108 return false;109 }

which will give us an error…in find, we are not supposed to retrieve

Page 209: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

209

find (cont.)97 template <class DataType>98 bool DoublyLinkedList<DataType>::find( 99 const DataType & element )100 {101 DataType el = element;102 if ( table.retrieve( el ) ) {103 current = table.getcurrent( );104 return true;105 }106107 current = trailer;108 return false;109 }

So element is copied to el first, then passed into retreive.

Page 210: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

210

find (cont.)97 template <class DataType>98 bool DoublyLinkedList<DataType>::find( 99 const DataType & element )100 {101 DataType el = element;102 if ( table.retrieve( el ) ) {103 current = table.getcurrent( );104 return true;105 }106107 current = trailer;108 return false;109 }

We could have passed by value here (instead of by const reference) to avoid this problem…

Page 211: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

211

find (cont.)97 template <class DataType>98 bool DoublyLinkedList<DataType>::find( 99 const DataType & element )100 {101 DataType el = element;102 if ( table.retrieve( el ) ) {103 current = table.getcurrent( );104 return true;105 }106107 current = trailer;108 return false;109 }

but element copying (pass by value or here) can be avoided altogether by making a find function in DLHashTable (an exercise)

Page 212: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

212

find (cont.)97 template <class DataType>98 bool DoublyLinkedList<DataType>::find( 99 const DataType & element )100 {101 DataType el = element;102 if ( table.retrieve( el ) ) {103 current = table.getcurrent( );104 return true;105 }106107 current = trailer;108 return false;109 }

Page 213: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

213

retrieve

110 template <class DataType>111 bool DoublyLinkedList<DataType>::retrieve( 112 DataType & element )113 {114 if ( !find( element ) )115 return false;116117 element = current->info;118 return true;119 }

Page 214: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

214

remove120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( 122 DataType & element )123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

sets element before it is removed

Page 215: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

215

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( 122 DataType & element )123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

also sets current to the node that element is in

Page 216: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

216

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( 122 DataType & element )123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

removes current node from doubly-linked list

Page 217: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

217

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;

126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

Page 218: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

218

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;

126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

Page 219: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

219

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;

126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

Page 220: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

220

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;

126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

Page 221: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

221

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;

126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

Page 222: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

222

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;

128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

Page 223: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

223

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;

128 current = trailer;129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

trailer

Page 224: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

224

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;

129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

trailer

Page 225: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

225

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;

129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

trailer

element is in the node current was pointing to

Page 226: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

226

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;

129 table.remove( element );130131 return true;132 }

doubly-linked list

collision listcurrent

trailer

element is in the node current was pointing to

Page 227: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

227

remove (cont.)120 template <class DataType>121 bool DoublyLinkedList<DataType>::remove( DataType & element )122123 {124 if ( !retrieve( element ) )125 return false;126 current->dlback->dlnext = current->dlnext;127 current->dlnext->dlback = current->dlback;128 current = trailer;129 table.remove( element );130

131 return true;132 }

doubly-linked list

collision listcurrent

trailer

Page 228: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

228

replace

133 template <class DataType>134 bool DoublyLinkedList<DataType>::replace( 135 const DataType & newElement ) 136 {137 if ( current == trailer )138 return false;139 current->info = newElement;140 return true;141 }

Page 229: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

229

isEmpty / makeEmpty142 template <class DataType>143 bool DoublyLinkedList<DataType>::isEmpty( ) const144 {145 return header->dlnext == trailer;146 }147148 template <class DataType>149 void DoublyLinkedList<DataType>::makeEmpty( ) 150 {151 table.makeEmpty( );152 current = header->dlnext = trailer;153 trailer->dlback = header;154 }

Page 230: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

230

deepCopy

155 template <class DataType>156 inline void DoublyLinkedList<DataType>::deepCopy( 157 const DoublyLinkedList<DataType> & original )158 {159 if ( original.table.getsize( ) != table.getsize( ) )160 table.changeSize( original.table.getsize( ) );161 table.sethashfunc( original.table.gethashfunc( ) );162 header = &headerNode;163 trailer = &trailerNode;164 Node<DataType> *save = header->dlnext = trailer;165 trailer->dlback = header; used later to set current

Page 231: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

231

deepCopy (cont.)

166 Node<DataType> *originalptr = original.trailer->dlback;167 if ( (originalptr == original.header) || 168 !insert( originalptr->info ) )169 return;

start at the end of the original doubly-linked list

Page 232: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

232

deepCopy (cont.)

166 Node<DataType> *originalptr = original.trailer->dlback;167 if ( (originalptr == original.header) || 168 !insert( originalptr->info ) )169 return;

If original doubly-linked list is empty, we want to return; we’ve already created an empty copy

Page 233: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

233

deepCopy (cont.)

166 Node<DataType> *originalptr = original.trailer->dlback;167 if ( (originalptr == original.header) || 168 !insert( originalptr->info ) )169 return;

We may not be able to insert because of an error in the client’s hash function (detected in DLHashTable)

Page 234: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

234

deepCopy (cont.)

166 Node<DataType> *originalptr = original.trailer->dlback;167 if ( (originalptr == original.header) || 168 !insert( originalptr->info ) )169 return;

By starting at the back of the original list and going forwards, we insert each node encountered into the front of the copy – ensuring a duplication.

Page 235: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

235

deepCopy (cont.)192 while ( originalptr->dlback != original.header ) {193 originalptr = originalptr->dlback;194 if ( !insert( originalptr->info ) ) {195 makeEmpty( );196 return;197 }198 if ( original.current == originalptr )199 save = header->dlnext;200 }201202 current = save;203 }

We want the current pointer in the original to correspond to the current pointer in the copy

Page 236: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

236

deepCopy (cont.)192 while ( originalptr->dlback != original.header ) {193 originalptr = originalptr->dlback;194 if ( !insert( originalptr->info ) ) {195 makeEmpty( );196 return;197 }198 if ( original.current == originalptr )199 save = header->dlnext;200 }201202 current = save;203 }

But we can’t set current in the copy yet – on each insert, the current pointer is changed.

Page 237: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

237

deepCopy (cont.)192 while ( originalptr->dlback != original.header ) {193 originalptr = originalptr->dlback;194 if ( !insert( originalptr->info ) ) {195 makeEmpty( );196 return;197 }198 if ( original.current == originalptr )199 save = header->dlnext;200 }201202 current = save;203 }

So we just save the current position of the copy in the save pointer.

Page 238: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

238

deepCopy (cont.)192 while ( originalptr->dlback != original.header ) {193 originalptr = originalptr->dlback;194 if ( !insert( originalptr->info ) ) {195 makeEmpty( );196 return;197 }198 if ( original.current == originalptr )199 save = header->dlnext;200 }201202 current = save;203 }

Whether save was set to trailer (before the while loop), or save was set in the while loop, this sets current correctly.