Data Structures Project 8 Implement one of the hashing proce

Data Structures Project 8



Implement one of the hashing procedures that we have discussed, and the following related functions:

INSERT (item)

DELETE (item)

FIND (item)

Make sure that your program correctly handles collisions and full hash table!

Any expert that could do this program for me would definatly get a high rating

Im still learning Hashing

Solution

//program to insert,delete,find and display elements using hashing in c //pre processor directives #include #include #include struct hash *hashTable = NULL; int eleCount = 0; //creation of node type struct node { int item; struct node *next; }; struct hash { struct node *head; int count; }; struct node * createNode(int item) { struct node *newnode; //memory allocation for a node newnode = (struct node *) malloc(sizeof(struct node)); newnode->next = NULL; return newnode; } void INSERT(int item) { int hashIndex = item% eleCount; struct node *newnode = createNode(item); /* head of list for the bucket with index \"hashIndex\" */ if (!hashTable[hashIndex].head) { hashTable[hashIndex].head = newnode; hashTable[hashIndex].count = 1; return; } /* adding new node to the list */ newnode->next = (hashTable[hashIndex].head); /* * update the head of the list and no of * nodes in the current bucket */ hashTable[hashIndex].head = newnode; hashTable[hashIndex].count++; return; } void DELETE(int item) { /* find the bucket using hash index */ int hashIndex = item% eleCount, flag = 0; struct node *temp, *myNode; /* get the list head from current bucket */ myNode = hashTable[hashIndex].head; if (!myNode) { printf(\"Given data is not present in hash Table!!\ \"); return; } temp = myNode; while (myNode != NULL) { /* delete the node with given item*/ if (myNode->item == item) { 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 from Hash Table\ \"); else printf(\"Given data is not present in hash Table!!!!\ \"); return; } void FIND(int item) { int hashIndex = item % eleCount, flag = 0; struct node *myNode; myNode = hashTable[hashIndex].head; if (!myNode) { printf(\"Search element unavailable in hash table\ \"); return; } while (myNode != NULL) { if (myNode->key == item) { printf(\"item : %d\ \", myNode->item); flag = 1; break; } myNode = myNode->next; } if (!flag) printf(\"Search element unavailable in hash table\ \"); return; } int main() { int n, ch, item; printf(\"Enter the number of elements:\"); scanf(\"%d\", &n); eleCount = n; /* create hash table with \"n\" no of buckets */ hashTable = (struct hash *) calloc(n, sizeof(struct hash)); while (1) { printf(\"\ 1. Insertion\\t2. Deletion\ \"); printf(\"3. Searching\\t4. Exit\ \"); printf(\"Enter your choice:\"); scanf(\"%d\", &ch); switch (ch) { case 1: printf(\"Enter the item value:\"); scanf(\"%d\", &item); /*inserting new node to hash table */ INSERT(item); break; case 2: printf(\"Enter the item to perform deletion:\"); scanf(\"%d\", &item); /* delete node with \"item\" from hash table */ DELETE(item); break; case 3: printf(\"Enter the ITEM to search:\"); scanf(\"%d\", &item); FIND(item); break; case 4: exit(0); default: printf(\" wrong option!!\ \"); break; } } return 0; }
Data Structures Project 8 Implement one of the hashing procedures that we have discussed, and the following related functions: INSERT (item) DELETE (item) FIND

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site