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

Student Record No 12

This C++ program implements a linear probing hash table to store student records. It defines a Student struct with roll number, name, marks, and status fields. The LinProbe class contains functions to insert, delete, search, and display records in a hash table stored in a binary file. The main function tests the class by allowing the user to insert, display, delete, search and quit the program by selecting options from a menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Student Record No 12

This C++ program implements a linear probing hash table to store student records. It defines a Student struct with roll number, name, marks, and status fields. The LinProbe class contains functions to insert, delete, search, and display records in a hash table stored in a binary file. The main function tests the class by allowing the user to insert, display, delete, search and quit the program by selecting options from a menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<iostream>

#include<fstream>

#include<cstring>

#define SIZE 10

#define h(x) x%SIZE

using namespace std;

struct student

int rollno;

char name[20];

float marks;

int status; /* O-record is active, 1-record is deleted */

};

class lin_probe

fstream tab;

char table[30];

student rec;//for reading/writing current record

public:

lin_probe(char *a);

void displayall();

void insert(student rec1);

void Delete(int rollno);

int search(int rollno);

void display(int recno) // display a particular record

int i=recno;

tab.open(table,ios::binary | ios::in);

tab.seekg(recno*sizeof(student),ios::beg);
tab.read((char*)&rec,sizeof(student));

if(rec.status==0)

cout<<"\n"<<i<<")"<<rec.rollno<<" "<<rec.name<<" "<<"\t"<<rec.marks;

else

cout<<"\n"<<i<<")** Empty **";

tab.close();

void read(int recno)

tab.open(table,ios::binary | ios::in );

tab.seekg(recno*sizeof(student),ios::beg);

tab.read((char*)&rec,sizeof(student));

tab.close();

void write(int recno)

tab.open(table,ios::binary |ios::out | ios::in);

tab.seekp(recno*sizeof(student),ios::beg);

tab.write((char*)&rec,sizeof(student));

tab.close();

};

lin_probe::lin_probe(char *a)

int i;

strcpy(table,a);

rec.status=1;

tab.open(table,ios::binary | ios::out);

tab.close();
for(i=0;i<SIZE;i++)

write(i);

void lin_probe::displayall()

int i=1,n;

cout<<"\n*****Data File*****\n";

for(i=0;i<SIZE;i++)

display(i);

void lin_probe::insert(student rec1)

int n,i,j,start;

rec1.status=0;

start=h(rec1.rollno);

for(i=0;i<SIZE;i++)

j=(start+i)%SIZE;

read(j);

if(rec.status==1)

rec=rec1;

write(j);

return;

cout<<"\nTable is full....can not insert";


}

void lin_probe::Delete(int rollno)

student rec1;

int recno;

recno =search(rollno);

if(recno>=0)

read(recno);

rec.status=1;

write(recno);

else

cout<<"\nRecord Not Found";

int lin_probe::search(int rollno)

int start,i,j;

start=h(rollno);

for(i=0;i<SIZE;i++)

j=(start+i)%SIZE;

read(j);

if(rec.status==0 &&rec.rollno==rollno)

return(j);

return(-1);

}
int main()

lin_probe object("table.txt");

int rollno,ch,recno;

student rec1;

cout<<"...............Student Record in Direct Access File.............";

do

cout<<"\n\n1)Insert\n2) Display\n3) Delete";

cout<<"\n4)Search\n5)Quit";

cout<<"\nEnter Your Choice:";

cin>>ch;

switch(ch)

{case 1:

cout<<"\nEnter a record to be inserted ";

cout<<"roll no: ";

cin>>rec1.rollno;

cout<<"Name: " ;

cin>>rec1.name;

cout<<"marks: ";

cin>>rec1.marks;

object.insert(rec1);

break;

case 2:

break;

case 3: cout<<"\nEnter the roll no.:";

cin>>rollno;

object.Delete(rollno);

break;

case 4: cout<<"\nEnter a roll no.: ";


cin>>rollno;

recno=object.search(rollno);

if(recno>=0)

cout<<"\n Record No.: "<<recno;

else

cout<<"\nRecord Not Found";

break;

} while(ch!=5);

You might also like