-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
56 lines (46 loc) · 925 Bytes
/
hash.cpp
File metadata and controls
56 lines (46 loc) · 925 Bytes
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
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
hash::hash(){
for(int i=0; i<tablesize; i++){
HashTable[i] = new item;
HashTable[i] -> name = "empty";
HashTable[i] -> drink = "empty";
HashTable[i] -> next = NULL;
}
}
void hash::AddItem(string name, string drink){
int index = Hash(name);
if(Hash[index]->name == "empty"){
HashTable ->name = name;
HashTable ->drink = drink;
}
else
{
item* ptr = HashTable[index];
item*n = new item;
n->name = name;
n-> drink = drink;
n->next = NULL;
while(ptr ->next !=NULL){
ptr = ptr->next;
}
ptr ->next =n;
}
}
int hash::Hash(string key) //hash:: comes from hash class
{
//table size (arr size) -> 100
int hash =0;
int index;
int sum=0;
for(int i=0; i<key.length(); i++){
hash = hash+ (int) key[i];
}
index = hash % tablesize;
cout<<hash<<endl;
return index;
}
//part 7 --look