Skip to content

Commit fb92670

Browse files
Open Addressing in C++
0 parents  commit fb92670

File tree

6 files changed

+269
-0
lines changed

6 files changed

+269
-0
lines changed

Diff for: hashing/open-addressing/Demo.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
#include<string>
8+
#include"hashtable.h"
9+
10+
using namespace std;
11+
12+
int main()
13+
{
14+
int id, choice, size;
15+
string name;
16+
17+
cout << "Enter initial size of table : ";
18+
cin >> size;
19+
20+
HashTable table(size);
21+
22+
while(true)
23+
{
24+
cout << "1.Insert a record \n";
25+
cout << "2.Search a record \n";
26+
cout << "3.Delete a record \n";
27+
cout << "4.Display table \n";
28+
cout << "5.Exit \n";
29+
cout << "Enter your choice : ";
30+
cin >> choice;
31+
32+
if( choice == 5 )
33+
break;
34+
35+
switch( choice )
36+
{
37+
case 1 :
38+
cout << "Enter student id : ";
39+
cin >> id;
40+
cout << "Enter student name : ";
41+
cin >> name;
42+
table.insertRecord(StudentRecord(id,name));
43+
break;
44+
case 2 :
45+
cout << "Enter a key to be searched : ";
46+
cin >> id;
47+
48+
if( table.searchRecord(id) )
49+
cout << "Key found \n";
50+
else
51+
cout << "Key not found \n";
52+
break;
53+
case 3:
54+
cout << "Enter a key to be deleted : ";
55+
cin >> id;
56+
table.deleteRecord(id);
57+
break;
58+
case 4:
59+
table.displayTable();
60+
break;
61+
case 5:
62+
exit(1);
63+
break;
64+
default:
65+
cout << "Wrong choice : \n";
66+
}
67+
}
68+
}
69+
70+

Diff for: hashing/open-addressing/HashTable.cpp

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
#include"hashtable.h"
8+
9+
using namespace std;
10+
11+
#define EMPTY 0
12+
#define DELETED 1
13+
#define OCCUPIED 2
14+
15+
HashTable :: HashTable(int tableSize)
16+
{
17+
m = tableSize;
18+
arr = new StudentRecord[m];
19+
status = new int[m];
20+
21+
for( int i=0; i<m; i++ )
22+
status[i] = EMPTY;
23+
24+
n = 0;
25+
}
26+
27+
HashTable :: ~HashTable()
28+
{
29+
delete[] arr;
30+
delete[] status;
31+
}
32+
33+
int HashTable :: hash(int key)
34+
{
35+
return key % m;
36+
}
37+
38+
void HashTable :: insertRecord(StudentRecord newRecord)
39+
{
40+
int key = newRecord.getstudentId();
41+
int h = hash(key);
42+
43+
int location = h;
44+
45+
for( int i=1; i<m; i++ )
46+
{
47+
if( status[location] == EMPTY || status[location] == DELETED )
48+
{
49+
arr[location] = newRecord;
50+
n++;
51+
status[location] = OCCUPIED;
52+
return;
53+
}
54+
55+
if( arr[location].getstudentId() == key )
56+
{
57+
cout << "Duplicate key \n";
58+
return;
59+
}
60+
61+
location = (h + i) % m;
62+
}
63+
cout << "Table is full : Record can't be inserted \n";
64+
}
65+
66+
bool HashTable :: searchRecord(int key)
67+
{
68+
int h = hash(key);
69+
int location = h;
70+
71+
for( int i=1; i<m; i++)
72+
{
73+
if( status[location] == EMPTY || status[location] == DELETED )
74+
return false;
75+
if( arr[location].getstudentId() == key )
76+
{
77+
cout << arr[location] << "\n";
78+
return true;
79+
}
80+
location = (h + i) % m;
81+
}
82+
return false;
83+
}
84+
85+
void HashTable :: deleteRecord(int key)
86+
{
87+
int h = hash(key);
88+
int location = h;
89+
90+
for( int i=1; i<m; i++ )
91+
{
92+
if( status[location] == EMPTY || status[location] == DELETED )
93+
{
94+
cout << "Key not found\n";
95+
return;
96+
}
97+
if( arr[location].getstudentId() == key )
98+
{
99+
status[location] = DELETED;
100+
n--;
101+
cout <<"Record " << arr[location] << " deleted\n";
102+
return;
103+
}
104+
location = (h + i) % m;
105+
}
106+
}
107+
108+
void HashTable :: displayTable()
109+
{
110+
for( int i=0; i<m; i++ )
111+
{
112+
cout << "[" << i << "] --> ";
113+
114+
if( status[i] == OCCUPIED )
115+
cout << arr[i] << "\n";
116+
else
117+
cout << "___" << "\n";
118+
}
119+
}

Diff for: hashing/open-addressing/StudentRecord.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
#include<string>
8+
#include"student.h"
9+
10+
using namespace std;
11+
12+
StudentRecord :: StudentRecord()
13+
{ }
14+
15+
StudentRecord :: StudentRecord(int i, string name)
16+
{
17+
studentId = i;
18+
studentName = name;
19+
}
20+
21+
int StudentRecord :: getstudentId()
22+
{
23+
return studentId;
24+
}
25+
26+
void StudentRecord :: setstudentId(int i)
27+
{
28+
studentId = i;
29+
}
30+
31+
ostream& operator<<(ostream& out, const StudentRecord& st)
32+
{
33+
out << " " << st.studentId << " " << st.studentName << " " ;
34+
return out;
35+
}

Diff for: hashing/open-addressing/hashtable.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
#include"student.h"
8+
using namespace std;
9+
10+
class HashTable
11+
{
12+
StudentRecord *arr;
13+
int m; //size of the array
14+
int n; //number of records
15+
int *status;
16+
int hash(int key);
17+
18+
public:
19+
HashTable(int tableSize=11);
20+
~HashTable();
21+
void insertRecord(StudentRecord newRecord);
22+
bool searchRecord(int key);
23+
void deleteRecord(int key);
24+
void displayTable();
25+
};

Diff for: hashing/open-addressing/student.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
using namespace std;
8+
9+
class StudentRecord
10+
{
11+
int studentId;
12+
string studentName;
13+
14+
public:
15+
StudentRecord();
16+
StudentRecord(int i, string name);
17+
int getstudentId();
18+
void setstudentId(int i);
19+
friend ostream& operator<<(ostream& out, const StudentRecord& st);
20+
};

Diff for: recursion/Thumbs.db

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)