-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDouble Hashing on string.cpp
More file actions
122 lines (116 loc) · 2.57 KB
/
Double Hashing on string.cpp
File metadata and controls
122 lines (116 loc) · 2.57 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#define size 6235
using namespace std;
class hashtable {
public:
string arr[size];
hashtable()
{
for (int i = 0; i < size; i++)
arr[i] = "";
}
int hashFunction(string x) // For string
{
int hash =0;
for (int i = 0; i < x.length(); i++)
{
hash += (int(x[i]));
}
return abs(hash % size);
}
int hash2(string x) // For string
{
int hash = 0;
for (int i = 0; i < x.length(); i++)
{
hash += (int(x[i]));
}
int a= abs(hash % 6229); //6229 is the last prime n
return 6229 - a;
}
void insertstring(string s)
{ //done by linear probing
int index = hashFunction(s);
for (int i = 0; i < size; i++)
{
if (arr[(index + i*hash2(s)) % size] == "") //to check if cell is empty or not.
{
arr[(index + i * hash2(s)) % size] = s;
return;
}
}
}
void findindex(string s)
{
int index=hashFunction(s);
while (arr[index] != s)
{
index=(index+ hash2(s))%size;
}
cout << s << " is at index # " << index;
}
void printhashtable()
{
cout << "ALL MOVIES AND SHOWS: " << endl;
for (int i = 0; i < size; i++)
{
cout<<i<<"\t" << arr[i] << endl;
}
}
};
hashtable titles;
void readfile()
{
string data;
fstream netflix; //File Pointer
netflix.open("C:\\Users\\HP\\Downloads\\countries.csv", ios::in);
if (netflix.fail()) {
cout << "COULD NOT READ FILE\n";
exit(1);
}
getline(netflix, data);
int line = 0;
string value;
while (!netflix.eof())
{
line++;
getline(netflix, value);
titles.insertstring(value);
}
}
int main()
{
readfile();
//titles.printhashtable();
while (1) {
cout << "\nWELCOME TO NETFLIX IMPLEMENTATION ON HASHTABLES\nBy Mashal Ashfaque\n\n\t1-Search movie by location (index)\n\t2-Search location of a movie\n\t3-Show all movies.";
int choice;
cin >> choice; getchar();
while (choice > 3 || choice < 1)
{
cout << "Invalid choice. Please select 1, 2 or 3.\n";
cin >> choice;
}
switch (choice)
{
case 1: {
cout << "\nWe have a total of 6235 movies and shows.\nEnter a number 0-6235 and we'll show what movie is over there.\n";
int s; cin >> s;
cout <<"\""<< titles.arr[s] << "\" is stored at your entered index." << endl;
break;
}
case 2: { cout << "\nTo find location of a movie, please enter its title. " << endl;
string m;
getline(cin, m);
titles.findindex(m);
cout << endl;
break; }
case 3:
titles.printhashtable();
}
}
}