0% found this document useful (0 votes)
6 views

database

The document contains a C++ program that implements a simple student database using a hash table for storing student records. It allows users to insert, display, and search for student records based on roll numbers. The program handles collisions and maintains a maximum capacity of 100 records.

Uploaded by

omkarshivajirao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

database

The document contains a C++ program that implements a simple student database using a hash table for storing student records. It allows users to insert, display, and search for student records based on roll numbers. The program handles collisions and maintains a maximum capacity of 100 records.

Uploaded by

omkarshivajirao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <string>

#include <vector>

using namespace std;

class Student {

public:

int rollNumber;

string name;

bool isEmpty;

bool isDeleted;

Student() {

rollNumber = 0;

name = "";

isEmpty = true;

isDeleted = false;

};

class StudentDatabase {

private:

static const int MAX = 100;

vector<Student> database;

int n;

int hashFunction(int rollNumber) {

return rollNumber % MAX;

public:
StudentDatabase() {

n = 0;

database.resize(MAX, Student());

void insert(int rollNumber, const string& name) {

int i = hashFunction(rollNumber);

int last = (i + MAX - 1) % MAX;

while (i != last && !database[i].isEmpty && !database[i].isDeleted &&

database[i].rollNumber != rollNumber) {

i = (i + 1) % MAX;

if (database[i].isEmpty || database[i].isDeleted) {

database[i].rollNumber = rollNumber;

database[i].name = name;

database[i].isEmpty = false;

database[i].isDeleted = false;

n++;

} else {

cout << "Error: Table full or roll number already exists." << endl;

void displayDatabase() {

cout << "Roll Number\tName" << endl;

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

if (!database[i].isEmpty && !database[i].isDeleted) {

cout << database[i].rollNumber << "\t\t" << database[i].name << endl;

}
}

void searchRecord(int rollNumber) {

int i = hashFunction(rollNumber);

int last = (i + MAX - 1) % MAX;

while (i != last) {

if (!database[i].isEmpty && !database[i].isDeleted && database[i].rollNumber == rollNumber)


{

cout << "Record Found: Roll Number: " << database[i].rollNumber

<< ", Name: " << database[i].name << endl;

return;

i = (i + 1) % MAX;

cout << "Record not found." << endl;

};

int main() {

StudentDatabase db;

int choice, rollNumber;

string name;

do {

cout << "\n1. Insert Record\n2. Display Database\n3. Search Record\n4. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {
case 1:

cout << "Enter Roll Number: ";

cin >> rollNumber;

cin.ignore(); // Clear newline from the buffer

cout << "Enter Name: ";

getline(cin, name);

db.insert(rollNumber, name);

break;

case 2:

db.displayDatabase();

break;

case 3:

cout << "Enter Roll Number to Search: ";

cin >> rollNumber;

db.searchRecord(rollNumber);

break;

case 4:

cout << "Exiting..." << endl;

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 4);

return 0;

You might also like