#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>
class
db
{
public:
int
rno,marks,ri;
char name[10];
};
class
NODE
{
db s,s1;
public:
int data,ri;
NODE *next;
void create();
void append();
void display();
int hashfun(int);
void hashcreate();
void hashdisp();
void search();
};
NODE
*temp,*nw,*head[10];
void
NODE::create()
{
fstream obj;
s.ri=0;
obj.open("a.txt",ios::out);
if(obj==NULL)
{
cout<<"\nFile is not
open";
exit(0);
}
cout<<"\nFile created
successfully";
obj.close();
}
void
NODE::append()
{
fstream obj;
obj.open("a.txt",ios::app);
if(obj==NULL)
{
cout<<"\nFile is not
open";
exit(0);
}
cout<<"\nEnter name:";
cin>>s.name;
cout<<"\nEnter roll no:";
cin>>s.rno;
cout<<"\nEnter marks:";
cin>>s.marks;
s.ri++;
obj.write((char *)&s,sizeof(s));
obj.close();
}
void
NODE::display()
{
fstream obj;
obj.open("a.txt",ios::in);
if(obj==NULL)
{
cout<<"\nFile is not
open";
exit(0);
}
cout<<"\nThe database
is:\n\nName\tRoll no\tMarks\n";
while(obj.read((char *)&s,sizeof(s)))
{
cout<<"\n"<<s.name<<"\t"<<s.rno<<"\t"<<s.marks;
}
obj.close();
}
int
NODE::hashfun(int x)
{
int value;
value=x%10;
return(value);
}
void
NODE::hashcreate()
{
fstream obj;
for(int i=0;i<10;i++)
{
head[i]=new(NODE);
head[i]->data=i;
head[i]->next=NULL;
}
obj.open("a.txt",ios::in);
if(obj==NULL)
{
cout<<"\nFile is not
open";
exit(0);
}
while(obj.read((char *)&s1,sizeof(s1)))
{
int hno=hashfun(s1.rno);
nw=new(NODE);
nw->next=NULL;
nw->data=s1.rno;
nw->ri=s1.ri;
temp=head[hno];
while(temp->next!=NULL)
temp=temp->next;
temp->next=nw;
}
obj.close();
}
void
NODE::hashdisp()
{
for(int i=0;i<10;i++)
{
temp=head[i];
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL\n";
}
}
void
NODE::search()
{
int cno,l=0,x;
ifstream obj;
cout<<"\nEnter the record to
search:";
cin>>x;
cno=hashfun(x);
temp=head[cno]->next;
while(temp!=NULL)
{
if(temp->data==x)
{
l=temp->ri;
break;
}
temp=temp->next;
}
if(l==0)
cout<<"\nRecord not
found";
else
{
obj.open("a.txt",ios::in);
if(obj==NULL)
{
cout<<"\nFile is not
open";
exit(0);
}
obj.seekg(0,ios::beg);
long offset=(l-1)*sizeof(s1);
obj.seekg(offset);
obj.read((char *)&s1,sizeof(s1));
cout<<"\nThe database
is:\n\nName\tRoll no\tMarks";
cout<<"\n\n"<<s1.name<<"\t"<<s1.rno<<"\t"<<s1.marks;
obj.close();
}
}
void
main()
{
int ch,srch;
NODE a;
clrscr();
do
{
cout<<"\n\tMenu\n1.Create\n2.Append\n3.Display\n4.Hash
create";
cout<<"\n5.Hash
display\n6.Search\n7.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
a.create();
break;
case 2:
a.append();
break;
case 3:
a.display();
break;
case 4:
a.hashcreate();
break;
case 5:
a.hashdisp();
break;
case 6:
a.search();
break;
case 7:
exit(0);
break;
}
}while(ch!=7);
getch();
}
-----------------------------------Output-------------------------------
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:1
File
created successfully
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:2
Enter
name:n
Enter
roll no:75
Enter
marks:87
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:2
Enter
name:s
Enter
roll no:76
Enter
marks:80
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:3
The
database is:
Name Roll no Marks
n 75
87
s 76
80
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:4
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:5
0->NULL
1->NULL
2->NULL
3->NULL
4->NULL
5->75->NULL
6->76->NULL
7->NULL
8->NULL
9->NULL
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:6
Enter
the record to search:75
The
database is:
Name Roll no Marks
n 75
87
Menu
1.Create
2.Append
3.Display
4.Hash
create
5.Hash
display
6.Search
7.Exit
Enter
your choice:7