Very basic hashtable

This is only a very basic hashtable
I use half an hour to write it(such a easy program takes me so long)
The hash function of string is simply sum all of the charactors up and times a prime number
This program is used to calculate how many times of every input string appears
The structure of this hashtable is like this:

hashtable
Just a simple implement without any optimize
The input is
n//The number of strings
string a

string n
The output is the hashtable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#define HASH 7
struct Node{
string val;
int num;
Node * next;
};
Node * hashTable[HASH];
int hash(string s){// stupid way to hash a string
int res = 0;
for(int i = 0;i < s.length();++ i) res += s[i];
res = res * 97 * 31 / 37;
return res;
}
bool insert(string s){
int h = hash(s) % HASH;
Node * node = hashTable[h];
if(!node) {
node = new Node;
node -> val = s;
node -> num = 1;
node -> next = NULL;
hashTable[h] = node;
}else{
while(node){
if(node -> val == s){
(node -> num) ++;
return true;
}
node = node -> next;
}
node = new Node;
node -> val = s;
node -> num = 1;
node -> next = hashTable[h];
hashTable[h] = node;
}
return false;
}
void output(){
Node * node;
for(int i = 0;i < HASH;++ i){
if(hashTable[i]){
node = hashTable[i];
while(node){
cout << node -> val << ' ' << node -> num << ' ';
node = node -> next;
}
cout << endl;
}
}
return ;
}
int main(){
int n;
string s;
cin >> n;
for(int i = 0;i < n;++ i){
cin >> s;
insert(s);
}
output();
return 0;
}

Contents
,