My Practice 4

6
Programming Practice Test 4 50 points Name: Time: 40 minutes Score: P-1 (6 pts) You are given a string with delimite rs within the string. With the delimiters, you c an tell how many fields you have for a given string a nd the va lues for each field. For example, a registration staff may receive a string Steven:85 8-552-9091:stevenliu @gmail.com: Torrey Pines High School:2010 and you want to write a fu nction to parse the string. In this example, the delimiter is : and you are aske d to split the string with delimiter : and retur n the parsedData p with p->length=5, and p->data[0] = Steven, p->data[1] = 858-552-9091, p->data[4] = 2010. // string library functions int string_length(string s); char string_charat(string s, int n); string string_join(string a, string b); string string_sub(string s, int start, int end); bool string_equal(string a, string b); int string_compare(string a, string b); struct parsedData { string [] data; int length; // number of fields }; typedef struct parsedData* parsedData; // 2 pts // return the number of fields in s // for example, with t he string mentioned on the above statement, // it should return 5 int number_field (string s) { } // 4 pts // split string s with delimiter and return parsedData parsedData split_string(strin g s, char delim) { }

Transcript of My Practice 4

Page 1: My Practice 4

8/8/2019 My Practice 4

http://slidepdf.com/reader/full/my-practice-4 1/6

Page 2: My Practice 4

8/8/2019 My Practice 4

http://slidepdf.com/reader/full/my-practice-4 2/6

P-2. 44 pts Assume that the hash function is defined as follows:

int hfun(string s, int size) where it computes the sum of {bit3-0, bit7-4} for each character in string s,

and returns an integer depending on the bucket size of the hash table.

The bucket size of the hash table is initially set to 4. Your task is to implement a hash table with the

average load factor less than or equal to 1. The data you are going to store in the hash table is defined

as follows:

struct student {string name;string city;string dob;string major;string email;

};typedef struct student *studentPtr;

The student information is stored in a single line of string, possible in a file. Each line stores one

students record with each field separated by a delimiter |. For example, with name, city, dob, and

others stored in a single line and is represented as

steven|san diego|08092010|cs|[email protected]

Requirements: Part 1struct list {

studentPtr data;struct list* next;

};

typedef struct list* list;/* linked lists may be NULL (= end of list) *//* we do not check for circularity */

// 3 pts

// define the structure of a hash tablestruct hash_table {};typedef struct hash_table* htable

// 3pts// complete the hash function as described above

// write the hfun (key is the concatenation of name and dob)int hfun(string key, int size){}

// 3pts// complete new hash table, ht_new, with init_bucket_sizehtable ht_new(int init_bucket_size){

Page 3: My Practice 4

8/8/2019 My Practice 4

http://slidepdf.com/reader/full/my-practice-4 3/6

}

// 1 pt// implement the hash table and the supporting functionsint get_bucket_size(htable ht)

{}

// 1 pt// get the number of entries stored in the hash tableint get_size(htable ht){}

// 1 pt// set the allowable average length in each bucketvoid ht_set_avg_length(htable ht, int len)

{}

// 3pts 

// find the student with key

studentPtr ht_find(htable ht, string key){}

// 6 pts// add a student to the hash table, return true when added// if the student has already existed in the hash table, return false

// expand the hash table when necessarybool ht_add(htable ht, studentPtr sp){}

// 3pts// double the bucket size with rehashingvoid ht_rehash(htable ht){}

// 4pts// delete the student from hash table, return the deleted studentPtr// key is the concatenation of name and dobstudentPtr ht_delete(htable ht, string key){}

Requirements: Part2Complete the test program to test the hash table you just created.

Page 4: My Practice 4

8/8/2019 My Practice 4

http://slidepdf.com/reader/full/my-practice-4 4/6

You may need to read the student records from a file such astestdata.txt and add them into the hash table to process the actualstudent information. For simplicity, we just skip the file readingstep and make several strings for you.

int main()

{htable ht = ht_new(4);string[] s = alloc_array(string, 5);s[0] = ³steven|san diego|08091992|cs|[email protected] ;s[1] = ³kevin|san jose|09191991|math|[email protected] ;s[2] = ³eric|Irvine|06141992|physics|[email protected]´;s[3] = ³mary|Santa Barbara|01051990|chemistry|[email protected] ;s[4] = ³chris|Cupertino|09181992|literature|[email protected]´;

// (5 pts) insert the above student information into hash table// you may use the function you wrote in P-1

// (2 pts) find student s[4] and make sure you find the right one

// (3 pts) delete s[2] and s[3] from hashtable and then find s[2] to// make sure it no longer exists in the hash table

}

Page 5: My Practice 4

8/8/2019 My Practice 4

http://slidepdf.com/reader/full/my-practice-4 5/6

 

Page 6: My Practice 4

8/8/2019 My Practice 4

http://slidepdf.com/reader/full/my-practice-4 6/6

// 8 pts

// What¶s the order of ht_add?

// What¶s the order (computation time) for ht_delete?

// What¶s the order of ht_rehash?

// Suppose you need to add n students into the hash table, what¶s the

order of computation time (including rehashing) when you establish the

hash table?