0% found this document useful (0 votes)
15 views11 pages

Simple Hash Table Implementation

The document presents a C++ implementation of a simple hash table for managing customer account numbers. It includes methods for inserting, searching, deleting, and displaying accounts, along with a demonstration of its functionality through a menu-driven interface. The hash table uses linear probing for collision resolution and manages memory effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Simple Hash Table Implementation

The document presents a C++ implementation of a simple hash table for managing customer account numbers. It includes methods for inserting, searching, deleting, and displaying accounts, along with a demonstration of its functionality through a menu-driven interface. The hash table uses linear probing for collision resolution and manages memory effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Name:-Dhiraj Dalvi

//Roll:-24
//Branch:-AIDS-A

//Assignemnt:-08

#include <iostream>

using namespace std;

class SimpleHashTable {

private:

int* table;

int size;

int EMPTY = -1;

int DELETED = -2;

public:

SimpleHashTable(int tableSize) {

size = tableSize;

table = new int[size];

for (int i = 0; i < size; i++) {

table[i] = EMPTY;

cout << "Hash table created with size: " << size << endl;

int findPosition(int accountNumber) {

return accountNumber % size;

void insertAccount(int accountNumber) {

int position = findPosition(accountNumber);

int originalPosition = position;


cout << "Trying to insert account " << accountNumber << " at position " << position << endl;

while (true) {

if (table[position] == EMPTY || table[position] == DELETED) {

table[position] = accountNumber;

cout << "✓ Account " << accountNumber << " inserted at position " << position << endl;

return;

else if (table[position] == accountNumber) {

cout << "✗ Account " << accountNumber << " already exists at position " << position << endl;

return;

position = (position + 1) % size;

if (position == originalPosition) {

cout << "✗ Table is full! Cannot insert account " << accountNumber << endl;

return;

void searchAccount(int accountNumber) {

int position = findPosition(accountNumber);

int originalPosition = position;

cout << "Searching for account " << accountNumber << " starting from position " << position << endl;

while (true) {

if (table[position] == accountNumber) {

cout << "✓ Account " << accountNumber << " found at position " << position << endl;

return;

}
if (table[position] == EMPTY) {

cout << "✗ Account " << accountNumber << " not found!" << endl;

return;

position = (position + 1) % size;

if (position == originalPosition) {

cout << "✗ Account " << accountNumber << " not found!" << endl;

return;

void deleteAccount(int accountNumber) {

int position = findPosition(accountNumber);

int originalPosition = position;

cout << "Trying to delete account " << accountNumber << " starting from position " << position << endl;

while (true) {

if (table[position] == accountNumber) {

table[position] = DELETED;

cout << "✓ Account " << accountNumber << " deleted from position " << position << endl;

return;

if (table[position] == EMPTY) {

cout << "✗ Account " << accountNumber << " not found for deletion!" << endl;

return;

position = (position + 1) % size;


if (position == originalPosition) {

cout << "✗ Account " << accountNumber << " not found for deletion!" << endl;

return;

void displayTable() {

cout << "\n=== HASH TABLE CONTENTS ===" << endl;

cout << "Position\tAccount Number" << endl;

cout << "------------------------" << endl;

for (int i = 0; i < size; i++) {

cout << i << "\t\t";

if (table[i] == EMPTY) {

cout << "--- EMPTY ---";

else if (table[i] == DELETED) {

cout << "--- DELETED ---";

else {

cout << table[i];

cout << endl;

cout << "==========================\n" << endl;

~SimpleHashTable() {

delete[] table;

cout << "Hash table destroyed. Memory cleaned up!" << endl;

};
int main() {

cout << "=== SIMPLE HASH TABLE DEMO ===" << endl;

cout << "Storing Customer Account Numbers\n" << endl;

SimpleHashTable accounts(7);

int choice;

int accountNumber;

do {

cout << "===== MENU =====" << endl;

cout << "1. Insert Account" << endl;

cout << "2. Search Account" << endl;

cout << "3. Delete Account" << endl;

cout << "4. Display Table" << endl;

cout << "5. Exit" << endl;

cout << "Enter your choice (1-5): ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter account number to insert: ";

cin >> accountNumber;

[Link](accountNumber);

break;

case 2:

cout << "Enter account number to search: ";

cin >> accountNumber;

[Link](accountNumber);

break;
case 3:

cout << "Enter account number to delete: ";

cin >> accountNumber;

[Link](accountNumber);

break;

case 4:

[Link]();

break;

case 5:

cout << "Thank you for using Simple Hash Table!" << endl;

break;

default:

cout << "Invalid choice! Please enter 1-5." << endl;

cout << endl;

} while (choice != 5);

return 0;

****************OUTPUT************************************

cclab-14@cclab14-OptiPlex-3000:~$ g++ [Link]

cclab-14@cclab14-OptiPlex-3000:~$ ./[Link]

Hash table created with size: 7

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table
5. Exit

Enter your choice (1-5): 1

Enter account number to insert: 1001

Trying to insert account 1001 at position 0

✓ Account 1001 inserted at position 0

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 1

Enter account number to insert: 2002

Trying to insert account 2002 at position 6

✓ Account 2002 inserted at position 6

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 1

Enter account number to insert: 3003

Trying to insert account 3003 at position 0

✓ Account 3003 inserted at position 1

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table
5. Exit

Enter your choice (1-5): 4

=== HASH TABLE CONTENTS ===

Position Account Number

------------------------

0 1001

1 3003

2 --- EMPTY ---

3 --- EMPTY ---

4 --- EMPTY ---

5 --- EMPTY ---

6 2002

==========================

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 2

Enter account number to search: 2002

Searching for account 2002 starting from position 6

✓ Account 2002 found at position 6

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit
Enter your choice (1-5): 2

Enter account number to search: 9999

Searching for account 9999 starting from position 1

✗ Account 9999 not found!

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 3

Enter account number to delete: 1001

Trying to delete account 1001 starting from position 0

✓ Account 1001 deleted from position 0

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 4

=== HASH TABLE CONTENTS ===

Position Account Number

------------------------

0 --- DELETED ---

1 3003

2 --- EMPTY ---

3 --- EMPTY ---

4 --- EMPTY ---

5 --- EMPTY ---


6 2002

==========================

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 1

Enter account number to insert: 4004

Trying to insert account 4004 at position 0

✓ Account 4004 inserted at position 0

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 4

=== HASH TABLE CONTENTS ===

Position Account Number

------------------------

0 4004

1 3003

2 --- EMPTY ---

3 --- EMPTY ---

4 --- EMPTY ---

5 --- EMPTY ---

6 2002
==========================

===== MENU =====

1. Insert Account

2. Search Account

3. Delete Account

4. Display Table

5. Exit

Enter your choice (1-5): 5

Thank you for using Simple Hash Table!

Hash table destroyed. Memory cleaned up!

You might also like