-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashtable.cpp
179 lines (169 loc) · 3.01 KB
/
Hashtable.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
using namespace std;
#include "Hashtable.h"
#include <vector>
Hashtable::Hashtable()
{
start = nullptr;
}
void Hashtable::starthash()
{
for (int i = 0; i < 12; i++)
{
Node * temp1 = new Node(i);
if (start == nullptr)
{
start = temp1;
}
else
{
Node * current = start;
while (current->next != nullptr)
{
current = current->next;
}
current->next = temp1;
}
}
loadhashtable();
}
void Hashtable::add(int a, int p)
{
static int i = 0;
ofstream write;
write.open("hashtable.txt", ios::app);
if(i != 0)
{
write << endl;
write << a << endl << p;
}
else
{
i++;
write << a << endl << p;
}
write.close();
starthash();
}
bool Hashtable::match(int a, int p)
{
bool flag = false;
int r = a % 10;
Node * c = start;
while (c->data != r)
{
c = c->next;
}
Node_1 *c1 = c->pres;
while (c1 != nullptr)
{
if (c1->accountNumber == a && c1->password == p)
{
flag = true;
break;
}
c1 = c1->next;
}
return flag;
}
void Hashtable::display()
{
Node * current = start;
while (current != nullptr)
{
cout << current->data << endl;
current = current->next;
}
}
void Hashtable::loadhashtable()
{
int acc = 0, r, pass;
read.open("hashtable.txt");
while (!read.eof())
{
read >> acc;
read >> pass;
if (match(acc, pass))
{
continue;
}
if (acc!= -858993460 && pass!= -858993460)
{
r = acc % 10;
Node * c = start;
while (c->data != r)
{
c = c->next;
}
Node_1 *temp = new Node_1(acc, pass);
if (c->pre == nullptr)
{
c->pre = temp;
}
else
{
Node_1 *root;
root = c->pre;
while (root->next != nullptr)
{
root = root->next;
}
root->next = temp;
}
}
else
{
cout << "No Password Present" << endl;
}
}
read.close();
}
void Hashtable::displayPasswords()
{
starthash();
Node *c = start;
while (c != nullptr)
{
Node_1 *c1 = c->pre;
while (c1 != nullptr)
{
cout<< c1->accountNumber <<endl;
cout<< c1->password <<endl;
c1 = c1->next;
}
c = c->next;
}
}
void Hashtable:: delete_password(int accountno)
{
ifstream read;
read.open("hashtable.txt");
vector <int> v;
int acc=0, pass=0;
int i = 0;
while (!read.eof())
{
i++;
read >> acc;
read >> pass;
if (acc == accountno)
{// read both account number and password to skip them
continue;
}
v.push_back(acc);
v.push_back(pass);
}
read.close();
ofstream write;
write.open("temp.txt", ios::app);
for (int i = 0; i < v.size(); i++)
{
if (v[i] != 0)
{
write << v[i] << endl;
}
}
write.close();
remove("hashtable.txt");
rename("temp.txt", "hashtable.txt");
}