fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct hash *hashTable = NULL;
  6. int eleCount = 100;
  7.  
  8. struct node {
  9. int key, value;
  10. struct node *next;
  11. };
  12.  
  13. struct hash {
  14. struct node *head;
  15. int count;
  16. };
  17.  
  18. struct node * createNode(int key, int value) {
  19. struct node *newnode;
  20. newnode = (struct node *)malloc(sizeof(struct node));
  21. newnode->key = key;
  22. newnode->value = value;
  23. newnode->next = NULL;
  24. return newnode;
  25. }
  26.  
  27.  
  28. void insertToHash(int key, int value) {
  29. int hashIndex = key % eleCount;
  30. struct node *newnode = createNode(key, value);
  31. //for first entry
  32. if (!hashTable[hashIndex].head) {
  33. hashTable[hashIndex].head = newnode;
  34. hashTable[hashIndex].count = 1;
  35. return;
  36. }
  37. struct node *temp = hashTable[hashIndex].head;
  38. //checking for same key number and updating its value
  39. while(temp){
  40. if(temp->key==key){
  41. temp->value = value;
  42. return;
  43. }
  44. temp = temp->next;
  45. }
  46. //creating a new node in the LL
  47. hashTable[hashIndex].head->next = newnode;
  48. hashTable[hashIndex].count++;
  49. return;
  50. }
  51.  
  52.  
  53. void deleteFromHash(int key) {
  54. int hashIndex = key % eleCount;
  55. struct node *temp, *myNode;
  56. myNode = hashTable[hashIndex].head;
  57. if (!myNode) {
  58. printf("Given data is not present in hash Table!!\n");
  59. return;
  60. }
  61. temp = myNode;
  62. while (myNode) {
  63. if (myNode->key == key) {
  64. //first element of LL
  65. if (myNode == hashTable[hashIndex].head)
  66. hashTable[hashIndex].head = myNode->next;
  67. //second to last element of LL
  68. else
  69. temp->next = myNode->next;
  70.  
  71. hashTable[hashIndex].count--;
  72. free(myNode);
  73. break;
  74. }
  75. temp = myNode;
  76. myNode = myNode->next;
  77. }
  78. return;
  79. }
  80.  
  81. void searchInHash(int key) {
  82. int hashIndex = key % eleCount;
  83. struct node *myNode;
  84. myNode = hashTable[hashIndex].head;
  85. if (!myNode) {
  86. printf("Search element unavailable in hash table\n");
  87. return;
  88. }
  89. while (myNode) {
  90. if (myNode->key == key) {
  91. printf("Value: %d\n", myNode->value);
  92. break;
  93. }
  94. myNode = myNode->next;
  95. }
  96. return;
  97. }
  98.  
  99. void display() {
  100. struct node *myNode;
  101. int i;
  102. for (i = 0; i < eleCount; i++) {
  103. if (hashTable[i].count == 0)
  104. continue;
  105. myNode = hashTable[i].head;
  106. printf("\nData at index %d in Hash Table:\n", i);
  107. while (myNode != NULL) {
  108. printf("Key %d : Value %d\n", myNode->key, myNode->value);
  109. myNode = myNode->next;
  110. }
  111. }
  112. return;
  113. }
  114.  
  115. int main() {
  116. int ch, key, value;
  117. hashTable = (struct hash *)calloc(eleCount, sizeof (struct hash));
  118. while (1) {
  119. printf("\n1. Insertion\t2. Deletion\n");
  120. printf("3. Searching\t4. Display\n5. Exit\n");
  121. printf("Enter your choice:\n");
  122. scanf("%d", &ch);
  123. switch (ch) {
  124. case 1:
  125. printf("Enter the key number:\n");
  126. scanf("%d", &key);
  127. printf("Enter the value number:\n");
  128. scanf("%d", &value);
  129. insertToHash(key, value);
  130. break;
  131.  
  132. case 2:
  133. printf("Enter the key to perform deletion:\n");
  134. scanf("%d", &key);
  135. /* delete node with "key" from hash table */
  136. deleteFromHash(key);
  137. break;
  138.  
  139. case 3:
  140. printf("Enter the key to search:\n");
  141. scanf("%d", &key);
  142. searchInHash(key);
  143. break;
  144. case 4:
  145. display();
  146. break;
  147. case 5:
  148. exit(0);
  149. default:
  150. printf("You have entered wrong option!!\n");
  151. break;
  152. }
  153. }
  154. return 0;
  155. }
  156.  
Success #stdin #stdout 0s 9424KB
stdin
1
10
1000
1
20
2000
4
5
stdout
1. Insertion	2. Deletion
3. Searching	4. Display
5. Exit
Enter your choice:
Enter the key number:
Enter the value number:

1. Insertion	2. Deletion
3. Searching	4. Display
5. Exit
Enter your choice:
Enter the key number:
Enter the value number:

1. Insertion	2. Deletion
3. Searching	4. Display
5. Exit
Enter your choice:

Data at index 10 in Hash Table:
Key 10 : Value 1000

Data at index 20 in Hash Table:
Key 20 : Value 2000

1. Insertion	2. Deletion
3. Searching	4. Display
5. Exit
Enter your choice: