Hey, please help me identify the mistake. It is showing segmentation fault.
#include<iostream>
using namespace std;
template<typename T>
class node{
public:
string key;
T value;
node *next;
// constructor
node(string k, int v){
key = k;
value = v;
next = NULL;
}
};
template<typename T>
class hashtable{
public:
node<T> **table;
int cs;
int ts;
// constructor
hashtable(int t=7){
ts=7;
new node<T>*[ts];
cs = 0;
for(int i=0; i<ts; i++){
table[i] = NULL;
}
}
int hash_func(string key){
int idx = 0;
int p = 1;
for(int i=0; i<key.length(); i++){
idx = idx + ((p*key[i])%ts);
idx = idx%ts;
p = 27*p%ts;
}
return idx;
}
// insertion
void insert(string key, int value){
int idx = hash_func(key); // generate idx corresponding to key
node<T> *temp = new node<T>(key,value);
temp->next = table[idx];
table[idx] = temp;
cs++;
}
// print
void print(){
for(int i=0; i<ts; i++){
node<T> *temp = table[i];
while(temp){
cout<<"Key: "<<temp->key<<", Value: "<<temp->value<<" ";
temp = temp->next;
}cout<<endl;
}
}
};
int main(){
hashtable<int> price_menu;
price_menu.insert("Burger",120);
price_menu.insert("Pepsi",20);
price_menu.insert("BurgerPizza",150);
price_menu.insert("Noodles",25);
price_menu.insert("Coke",40);
price_menu.print();
return 0;
}