linear probing

Time limit: 5000ms
Memory limit: 256mb

Description:
This program is to insert N key-record pair into hash table using linear probling and search some keys.


-------------------------Copy the following code, complete it and submit-------------------------


/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/

#include <stdio.h>
#include <stdlib.h>


const int hashTableSize = 29989;

struct Entry {
    int key;
    int record;
};

/**
 * @brief: create an entry
 * @param: 1. key   2. record
 * @return: address of the entry
 * @usage: struct Entry* newEntry = CreateEntry(yourKey, yourRecord)
 */
struct Entry* CreateEntry(int key, int record) {
    struct Entry* newEntry = (struct Entry*) malloc(sizeof(struct Entry));
    newEntry->key = key;
    newEntry->record = record;
    return newEntry;
}

/**
 * @brief: get the hash value of a key
 * @param: 1. key 
 * @return: hash value
 * @usage: int hashValue = HashFunction(yourKey)
 */
int HashFunction(int key) {
    return key % hashTableSize;
}

/**
 * @brief: insert an entry into a hashTable. If key has existed, modify its record with new one
 * @param: 1. hashTable   2. key  3. record
 * @return: void
 * @usage: InsertEntry(yourHashTable, yourKey, yourRecord)
 */
void InsertEntry(struct Entry* hashTable[], int key, int record) {
    // WRITE YOUR CODE HERE
    // DO NOT MODIFY THE OTHER CODE
}

/**
 * @brief: search an entry in a hashTable. 
 * @param: 1. hashTable   2. key 
 * @return: If key does not exist, return -1. Or return the index in the hashTable.
 * @example: If search key 2 is in the hashTable[1], Then you should return 1.
 * @usage: int index = SearchEntry(yourHashTable, yourKey)
 */
int SearchEntry(struct Entry* hashTable[], int key) {
    // WRITE YOUR CODE HERE
    // DO NOT MODIFY THE OTHER CODE
}


int main() {
    struct Entry* hashTable[hashTableSize];
    for (int i = 0; i < hashTableSize; i++) {
        hashTable[i] = NULL;
    }

    int insertNum, searchNum;
    int key, record;
    scanf("%d", &insertNum);
    for (int i = 0; i < insertNum; i++) {
        scanf("%d%d", &key, &record);
        InsertEntry(hashTable, key, record);
    }
 
    scanf("%d", &searchNum);
    for (int i = 0; i < searchNum; i++) {
        scanf("%d", &key);
        int index = SearchEntry(hashTable, key);
        printf("%d\n", index);
    }
    printf("\n");
}

-------------------------------------------End of Code-------------------------------------------

Input:
First line: num of insertion pairs (named insertNum)
next insertNum lines: two integers which are key and record
next line: num of search operation (named searchNum)
next searchNum lines: one integer which is search key

Output:
searchNum lines of search result

Sample Input 1:
5
1 32
2 755
4 46
5 237
5 589
6
5
1
1
3
1
3

Sample Output 1:
5
1
1
-1
1
-1

Submit