Unordered Map

Time limit: 5000ms
Memory limit: 256mb

-------------------------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>
#include <string.h>

#define INF -1

typedef enum {PUT = 1, GET, ERASE, SIZE, ERROR_OP} OP;

const int hashTableSize = 29989;

// this linked list has no head or tail, which means that if this list is empty, then it equals NULL
struct linkedList {
    int key;
    int value;
    struct linkedList* next;
};

typedef struct linkedList* ListNode;

struct map {
    int size;
    ListNode* hashTable;
};

typedef struct map* MapADT;

int HashFunction(int key) {
    return key % hashTableSize;
}

// linked list

ListNode CreateNode(int key, int value) {
    ListNode node = (ListNode) malloc(sizeof(struct linkedList));
    node->key = key;
    node->value = value;
    node->next = NULL;

    return node;
}

// map

// Initially, each linked list in the hashTable is NULL 
// Hashing collision resolution: chaining
MapADT CreateMap() {
    MapADT m = (MapADT) malloc(sizeof(struct map));
    m->size = 0;
    m->hashTable = (ListNode*) malloc(hashTableSize * sizeof(ListNode));
    memset(m->hashTable, 0, hashTableSize * sizeof(ListNode)); 

    return m;
}

int Size(MapADT myMap) {
    return myMap->size;
}

void Put(MapADT myMap, int key, int value) {
    // write your code here

}

int Get(MapADT myMap, int key) {
    // write your code here

}

void Erase(MapADT myMap, int key) {
    // write your code here

}

OP get_op() {
    char str[20];
    scanf("%s", str);
    if (strcmp(str, "put") == 0) {
        return PUT;
    } else if (strcmp(str, "get") == 0) {
        return GET;
    } else if (strcmp(str, "erase") == 0) {
        return ERASE;
    } else if (strcmp(str, "size") == 0) {
        return SIZE;
    } else {
        return ERROR_OP;
    }
}

int main() {
    int n, key, value;
    MapADT myMap = CreateMap();
    scanf("%d", &n);

    for(int i=0; i<n; i++)
    {
        switch (get_op()) {
            case PUT:
                scanf(" %d%d", &key, &value);
                Put(myMap, key, value);
                break;
            case GET:
                scanf(" %d", &key);
                printf("%d\n", Get(myMap, key));
                break;
            case ERASE:
                scanf(" %d", &key);
                Erase(myMap, key);
                break;
            case SIZE:
                printf("%d\n", Size(myMap));
                break;
            default:
                printf("Error Input\n");
        }
    }
}



-----------------------------------------End of Code-----------------------------------------
Description:
Implement the unordered map ADT with the hash table, where its hashing collision resolution is chaining.
The unordered map ADT has four operations:
1. Put(key, value): insert a key-value pair into the map. If it already has the key, then update the value.
2. Get(key): given a key, report its value. If it does not have this key, report INF.
3. Erase(key): delete this key from the map. If it does not have this key, do nothing.
4. Size(): report the number of elements inside the map.

Note that the linked list has no head or tail, which means that if this list is empty, then it equals NULL.

Input:
The first line contains one integer: N;
The next N lines, each of which contains one of these operations: 1. put key value (two Integers) or 2. get key 3. erase key 4. size

Output:
Each line is the result of get or size.

Sample Input 1:
12
put 0 0
put 29989 29989
get 0
put 0 1
get 0
put 2 2
get 2
size
erase 0
get 0
erase 3
size

Sample Output 1:
0
1
2
3
-1
2


Submit