-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create hash table for each character of word
Every character of the input word is entered into a hash table, of suitable hash function using separate chaining method.
- Loading branch information
1 parent
f8fb43c
commit 18d5fe6
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
const int T_S = 256; //hash table size | ||
using namespace std; | ||
|
||
struct Node { | ||
int key; | ||
Node *next; | ||
}; | ||
|
||
struct Node *ht[T_S]; | ||
void InitializeHT() { //initialization of hash table | ||
for(int i = 0; i < T_S; i++) | ||
ht[i] = NULL; | ||
} | ||
|
||
int HashFunc(int key) { //hash function | ||
return key % T_S; | ||
} | ||
|
||
void InsertInHashTable(struct Node** head, int new_data) | ||
{ | ||
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); | ||
struct Node *temp = *head; //temp is to traverse | ||
new_node->key = new_data; | ||
new_node->next = NULL; | ||
|
||
if(*head == NULL) { | ||
*head = new_node; | ||
return; | ||
} | ||
while(temp->next != NULL) | ||
temp = temp->next; | ||
temp->next = new_node; | ||
return; | ||
} | ||
|
||
int main() { | ||
int index, i=0; | ||
string str; | ||
cout<<"Enter the word "; | ||
cin>>str; | ||
InitializeHT(); | ||
while(str[i]!='\0') { | ||
index=HashFunc(str[i]); | ||
InsertInHashTable(ht[index], i); //check the error | ||
} | ||
return 0; | ||
} |