CS 112 - Lab 8
Hashing


Section 1: Introduction

The goal of this lab is to:

If you have not already done so, create a directory for this lab and copy the lab 8 files into it. If you start off in your home directory you can do the following:

	mkdir ~/cs112/lab8
	cd ~/cs112/lab8
	cp ~cs112/lab8/* .

Section 2: Topics for Lecture

Section 3: Description of Assignment


You are to implement a hash table using a contiguous array of structures (instead of linked lists) in which each record contains information about a student:

Last name
Id number
GPA


Three operations will be available for manipulating the records:

Insert a record using name as the hash key (last name only)
Lookup a name and print associated information
Replace the GPA field of a person's record

Examples which could be processed include:

Insertion: I Roberts 12345 3.2
Lookup: L Roberts
Replacement: R Roberts 3.5

As with Lab 5 and 6, your program will read the input from a file named input_file.

In addition to the personal info in the node structure, there will be one more field which holds the number of collisions that occur while attempting to insert into the hash table during the course of the program. You will need to increment it each time you probe the table.

At the end of your program, the table must be traversed and the information in each (including the number of collisions field) printed to stdout.

Note:Your traversal will be based on the indexes of the array and therefore will not be in alphabetical order.

Example output:

	INDEX  NAME       ID    GPA COLLISIONS

	0    Steffey    36689   3.6   0
	1    Winslow    22332   2.1   2
	2    
	3    Matthews   34544   4.0   2
	4    Hamlin     12343   3.7   4
	5    Daniels    30933   3.9   2
	6    
	7    Woods      987     3.1   0
	8    Howard     22433   4.0   0
	9    Johns      11221   3.3   1
	10   Ford       34654   4.0   0



Note: You will also print which index of the array holds which name, and if an index is empty it's line will be blank.

You will give the user a choice of linear or quadratic probing, so you must modify the insert( ) function from the textbook so it can handle either method of probing.

Hint: An extra field has been added to the insert( ) and lookup( ) functions for the purpose of handling quadratic or linear probing.

Note: You are to use the hash( ) function on page 357 in the textbook for this lab.

Section 4: Code Development


You will be given the following files:

  • hash.h (containing typedefs, structs and macros and function prototypes)
  • main.c (containing the function main( ))

    We suggest writing the following functions:

  • int Hash(char *);
  • void insert(HashTable, Entry, char);
  • int lookup(HashTable, char *, char);
  • void print_info(Entry);
  • void replace(HashTable, float, int);
  • void traverse(HashTable);
  • void initialize(HashTable);

    These prototypes are in hash.h.

    Remember to use stubs in your top-down algorithm design, and note that the function lookup( ) can be easily derived from the function insert( ).
    
    

    Section 5: Handing in your C code and answers


    When you have completed this lab assignment, you should have the following program(s) in your ~/cs112/lab8 directory:

  • main.c (containing the function main( ))
  • output.c (containing print_info( ) and traverse( ))
  • hash.c (containing all other hash operation functions)
  • hash.h (containing all typedefs, prototypes, etc)
  • makefile

    Then, to submit your program(s) for grading, issue the command

    ~cs112/submit lab8

    at your UNIX prompt. This utility program will copy your programs for grading and an electronic mail message will be sent to you to acknowledge your submission. You may check the status of your submission at any time by typing the command

    ~cs112/query lab8

    at your UNIX prompt. You will automatically receive an electronic mail message confirming the status of your lab submission.

    You should also answer the questions below and submit them in hardcopy to the TAs at the beginning of lab on April 11 (no late submissions accepted).

    This week's questions

    For credit on this lab, your software must be received by midnight April 10 and your answers to the lab questions must be received at the beginning of lab on Friday, April 11.

    Note: Always bring your ID with you to lab.