Laboratory 10
Due at the beginning of class, Wednesday, 4/23
In today's lab you will practice working with hash tables.
The code for this lab is available in the directory:
Start the lab as follows:
Early in the semester, we discussed the game of life, and implemented some of the functions in the life.cc class. (See lab2 to recall our earlier assignment). We had to use various tricks to deal with conditions when life objects moved off the assigned grid, as the life game was represented on a finite 2D table. However the real game of life is meant to be played on a grid of infinite dimensions. Cells at any position should be able to be born or to die. Obviously, we cannot implement the game of life on an infinite 2D grid in C++, since we do not have infinite memory. However, we can implement it in a different way, using a hash table. In this implementation, we do not represent every cell on our 2D grid. Instead, we only keep track of which cells are alive. To do this, we use a hash table. If a cell at position (row, col) is alive, it is stored at its corresponding position in the hash table. If it is not alive, it is not represented.
In order to update our life game, we need to calculate new cells that are born and die based on the number of live neighbors. The only cells that can change status are those that are alive or those that are neighbors of the live cells. Our update() function needs only examine these cells. However, it is difficult to traverse a hash table, so in addition to using the hash table to keep track of live cells, we will also use a list that lists all the live cells. The update function will traverse this list and compute which live cells or which neighbors of live cells change status.
Representations: The cells in the grid for the life game are instances of the Cell class (see the file, cell.h, which has the specification and implementation of this class). The data members of this class are two integers: row and col. Because these cells are created dynamically (as each cell is born) and deleted dynamically (when cells die) it is easier to work with pointers to the Cell objects. Thus, both our linked list and hash table contain pointers to cells in each location as opposed to the cells themselves. In addition, the Hash_table class uses chaining to resolve collisions. Thus each occupied position in the hash table will contain a pointer to a linked list of pointers to cell objects. An example from the text of such an "indirect" linked list is shown below:
In addition this class has several member functions:
Public member functions:
The Hash_table class (see the hash.h file) has one private data member:
The Hash_table class also has the following public member functions:
These classes make use of the functions and definitions of a linked list provided in the List class (see list.h and list.cc).
Your task:
Nearly the entire program has been implemented for you, however there are several
functions that need to be completed. Your task is to fill in the function stubs for
the following functions:
Fill in these functions in the Hash_table class:
Fill in this function in the Life class:
Implementing the insert() function for the Hash_table class:
The sequential_search() function is specified as follows:
The first parameter is the list to be searched, the second and third parameters are the row and col keys to be searched for in the cells of the list, and the fourth parameter is the location of the cell pointer if it is found in the list.
Implementing the retrieve() function for the Hash_table class:
Implementing the neighbor_count() function for the Life class:
Compile your program with the following compile command:
Test out your program by entering the following pairs of integers:
10 10 10 11 10 12 -1 -1
Recall that this pattern will oscillate back and forth from vertical to horizontal in successive generations.
a) Determine the hash addresses and find how many collisions occur when these keys are reduced by applying the operation %hash_size.
b) Determine the hash addresses and find how many collisions occur when these keys are first folded by adding their digits together (in ordinary decimal representation) and then applying %hash_size.
What To Turn In.
Be sure that the file prologue for each file contains your name, course, lecture section, date and purpose of the program or a description of the contents of the file (e.g. specification of the stack class).
Reminder.
Be sure to save a copy of each file in your labs directory. It is your responsibility
to keep the copies of all programs written for this course until your
graded assignment is returned.
Home | | Syllabus | | Assignments | | Lecture Notes