-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSHash.cpp
More file actions
189 lines (143 loc) · 2.88 KB
/
PSHash.cpp
File metadata and controls
189 lines (143 loc) · 2.88 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
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
180
181
182
183
184
185
186
187
188
/*
Name: Matthew Wilson
Course: CSCE 2110.002
Date: 11/15/21
This header file defines the member functions of the PlaneSeats hash table
*/
#include "PShash.h"
PSHash::PSHash(string file)
{
for(int i = 0; i < size; i++)
{
HashTable[i] = new node;
}
ifstream fin(file);
string line, word, Make, Model, Type;
int Seats = 0, i = 0;
getline(fin, line);
while(getline(fin, line))
{
for(unsigned int i = 0; i < line.length(); i++)
{
if(line[i] == ',')
{
line[i] = ' ';
}
}
stringstream ss(line);
ss >> Make >> Model >> Type >> Seats;
i = hash(Make);
if(HashTable[i]->make == " ")
{
HashTable[i]->make = Make;
HashTable[i]->model = Model;
HashTable[i]->seattype = Type;
HashTable[i]->seats = Seats;
}
else//handling collision by chaining
{
node *ptr = HashTable[i];
node *temp = new node;
temp->make = Make;
temp->model = Model;
temp->seattype = Type;
temp->seats = Seats;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
fin.close();
}
int PSHash::hash(string key)
{
int hash = 0;
int index;
for(unsigned int i = 0; i < key.length(); i++)
{
hash = hash + (int)key[i];
}
index = hash % size;//modulo hashing
return index;
}
void PSHash::display()
{
node* ptr = NULL;
cout << "Maker\t" << "Model\t" << "Seat Type\t" << "Number of Seats" << endl;
for(int i = 0; i < size; i++)
{
ptr = HashTable[i];
if(ptr->make != " ")
{
cout << ptr->make << "\t" << ptr->model << "\t\t";
cout << ptr->seattype << "\t\t" << ptr->seats;
cout << endl;
while(ptr->next != NULL)
{
ptr = ptr->next;
cout << ptr->make << "\t" << ptr->model << "\t\t";
cout << ptr->seattype << "\t\t" << ptr->seats;
cout << endl;
}
}
}
cout << endl;
}
void PSHash::insert(string mk, string md, string st, int s)
{
int index = hash(st);
if(HashTable[index]->seats != -999)
{
cout << "Error in plane seats table: need unique key from seats." << endl;
return;
}
HashTable[index]->make = mk;
HashTable[index]->model = md;
HashTable[index]->seattype = st;
HashTable[index]->seats = s;
}
void PSHash::update(string mk, string md, string st, int s)
{
int index = hash(st);
node* ptr = HashTable[index];
if(ptr->make == " ")
{
cout << "Error in plane table: index is empty." << endl;
return;
}
while(ptr->seats != s || ptr->next != NULL)
{
ptr = ptr->next;
}
if(ptr->seats == s)
{
ptr->make = mk;
ptr->model = md;
ptr->seattype = st;
ptr->seats = s;
}
}
void PSHash::select(int s)
{
int j = -1;
int a[23];
for(int i = 0; i < size; i++)
{
if(HashTable[i]-> seats == s)
{
j++;
a[j] = i;
}
}
if(j > -1)
{
cout << "Select: Plane(s) at index: ";
for(int i = 0; i <= j; i++)
{
cout << a[i] << ", ";
}
cout << "have " << s << " seats" << endl << endl;
}
}