-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.h
More file actions
134 lines (112 loc) · 3.42 KB
/
HashTable.h
File metadata and controls
134 lines (112 loc) · 3.42 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
/*
HashTable.h
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - DSA Project
Description:
This file contains the HashTable class implementation for storing user credentials.
*/
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <string>
#include <iostream>
class HashTable {
public:
static const int TABLE_SIZE = 1000; // Size of the hash table
// Make Auth class a friend to access private members
friend class Auth;
// Node structure definition
struct Node {
std::string key;
std::string value;
Node* next;
Node(const std::string& k, const std::string& v) : key(k), value(v), next(nullptr) {}
};
private:
Node** table; // Array of pointers to Node
// Hash function
int hash(const std::string& key) const {
int hash = 0;
for (char c : key) {
hash = (hash * 31 + c) % TABLE_SIZE;
}
return hash;
}
public:
// Constructor and Destructor
HashTable() {
table = new Node * [TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++) {
table[i] = nullptr;
}
}
~HashTable() {
clear();
delete[] table;
}
// Core operations
void insert(const std::string& key, const std::string& value) {
int index = hash(key);
std::cout << "Inserting " << key << " at index " << index << std::endl;
// Check if key already exists
Node* current = table[index];
while (current != nullptr) {
if (current->key == key) {
current->value = value;
return;
}
current = current->next;
}
// Create new node
Node* newNode = new Node(key, value);
newNode->next = table[index];
table[index] = newNode;
}
std::string* get(const std::string& key) const {
int index = hash(key);
std::cout << "Getting " << key << " from index " << index << std::endl;
Node* current = table[index];
while (current != nullptr) {
if (current->key == key) {
return &(current->value);
}
current = current->next;
}
return nullptr;
}
void remove(const std::string& key) {
int index = hash(key);
std::cout << "Removing " << key << " from index " << index << std::endl;
Node* current = table[index];
Node* prev = nullptr;
while (current != nullptr) {
if (current->key == key) {
if (prev == nullptr) {
table[index] = current->next;
}
else {
prev->next = current->next;
}
delete current;
return;
}
prev = current;
current = current->next;
}
}
bool contains(const std::string& key) const {
return get(key) != nullptr;
}
void clear() {
for (int i = 0; i < TABLE_SIZE; i++) {
Node* current = table[i];
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp;
}
table[i] = nullptr;
}
}
};
#endif // HASHTABLE_H