Skip to content

Commit

Permalink
Create hash table for each character of word
Browse files Browse the repository at this point in the history
Every character of the input word is entered into a hash table, of suitable hash function using separate chaining method.
  • Loading branch information
sushmita04 authored May 18, 2020
1 parent f8fb43c commit 18d5fe6
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Hash_Table_Insert_Value.cpp
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;
}

0 comments on commit 18d5fe6

Please sign in to comment.