1.3 (50 points) Suppose you are implementing a dynamic set of student records as
ID: 3852999 • Letter: 1
Question
1.3 (50 points) Suppose you are implementing a dynamic set of student records as a hash table. Each record has an integer key. Key can have values from 0 through 65,536 and no two records can have the same key values. In addition to the key, each record has following information. Each record has an integer key Key can have aues from 0thirougn osss0cords Name: GPA Academic level: Even though the key can take a value between 0 and 65536, this university can have max 10000 students at a given time. Hash Table Implementation Details Assume that the hash key is k mod m and using chaining in case of a collision. Also, the size (m) of the hash table is 1000. Also, you can assume that keys for student are generated using a random uniform distribution function. Write a C or C++ program that Implements the hash table construction for the above scenario and then implement the following three functions a) INSERT(T, x) insert the student record x to the table b) DELETE(T, x) // delete the student record x from the table c) SEARCH(T, k) //search key k in the hash tableExplanation / Answer
//find the below program.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct hash *hashTable = NULL;
int eleCount = 0;
struct node {
int key, gpa, ac_level;
char name[100];
struct node *next;
};
struct hash {
struct node *head;
int count;
};
struct node * createNode(int key, char *name, int gpa, int ac_level) {
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->key = key;
newnode->gpa = gpa;
newnode->ac_level = ac_level;
strcpy(newnode->name, name);
newnode->next = NULL;
return newnode;
}
void insertToHash(int key, char *name, int gpa, int ac_level) {
int hashIndex = key % eleCount;
struct node *newnode = createNode(key, name, gpa, ac_level);
if (!hashTable[hashIndex].head) {
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count = 1;
return;
}
newnode->next = (hashTable[hashIndex].head);
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count++;
return;
}
void deleteFromHash(int key) {
int hashIndex = key % eleCount, flag = 0;
struct node *temp, *myNode;
myNode = hashTable[hashIndex].head;
if (!myNode) {
printf("key not found ");
return;
}
temp = myNode;
while (myNode != NULL) {
if (myNode->key == key) {
flag = 1;
if (myNode == hashTable[hashIndex].head)
hashTable[hashIndex].head = myNode->next;
else
temp->next = myNode->next;
hashTable[hashIndex].count--;
free(myNode);
break;
}
temp = myNode;
myNode = myNode->next;
}
if (flag)
printf("Data deleted successfully ");
else
printf("key not found in hash table ");
return;
}
void searchInHash(int key) {
int hashIndex = key % eleCount, flag = 0;
struct node *myNode;
myNode = hashTable[hashIndex].head;
if (!myNode) {
printf("key not found ");
return;
}
while (myNode != NULL) {
if (myNode->key == key) {
printf("key : %d ", myNode->key);
printf("Name : %s ", myNode->name);
printf("GPA : %d ", myNode->gpa);
printf("Academic Level : %d ", myNode->ac_level);
flag = 1;
break;
}
myNode = myNode->next;
}
if (!flag)
printf("Search element unavailable in hash table ");
return;
}
#define randnum(min, max)
((rand() % (int)(((max) + 1) - (min))) + (min))
int main() {
srand(time(NULL));
int n=1000,ch, key=0, gpa, ac_level;
key=randnum(1, 10000);
printf("%d",key);
char name[100];
eleCount = n;
hashTable = (struct hash *)calloc(n, sizeof (struct hash));
while (1) {
printf(" 1. Insert 2. Delete ");
printf("3. Search 4. Exit ");
printf("Enter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
getchar();
printf("Name:");
fgets(name, 100, stdin);
name[strlen(name) - 1] = '';
printf("GPA:");
scanf("%d", &gpa);
printf("Academic Level:");
scanf("%d",&ac_level);
insertToHash(key, name, gpa, ac_level);
break;
case 2:
printf("Enter the key to delete:");
scanf("%d", &key);
deleteFromHash(key);
break;
case 3:
printf("Enter the search key:");
scanf("%d", &key);
searchInHash(key);
break;
case 4:
exit(0);
default:
printf("Invalid option ");
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.