/*****************************************************
NAME : JABIR DAUD PATHAN
PROGRAM : Implement Direct Access File Using Linear
Probing With Chaining And With Replacement
Method
Of Collision Resolution.
*****************************************************/
/*---------------<< Header File
>>------------------*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define
size 10
#include<fstream.h>
#include<iomanip.h>
#define
hash(key) key%size
typedef
struct bank
{
int acc_no;
char name[size];
int bal;
int status;
int link;
};
/*--------<< Declaration of class linear_prob
>>--------*/
class
linear_prob
{
char table[size];
fstream table1;
bank b;
public:
linear_prob(char *a);
void display1(int);
void insert(bank);
void delet(int);
int search(int);
void display();
void read(int);
void write(int);
};
/*------<< Function defination for display
>>---------*/
void
linear_prob :: display1(int rec)
{
int i=rec;
table1.open(table,ios::binary|ios::in|ios::nocreate);
table1.seekg(rec*sizeof(bank),ios::beg);
table1.read((char
*)&b,sizeof(bank));
if(b.status==0)
{
cout<<"\n\n"<<i<<")"<<b.acc_no<<" "<<b.name<<"
"<<setprecision(2)<<b.bal<<" "<<b.link;
}
else
cout<<"\n\n"<<i<<")EMPTY";
table1.close();
}
/*----------<< Function defination for read
>>------------*/
void
linear_prob :: read(int rec)
{
table1.open(table,ios::binary|ios::in);
table1.seekg(rec*sizeof(bank),ios::beg);
table1.read((char
*)&b,sizeof(bank));
table1.close();
}
/*----------<< Function defination for write
>>------------*/
void
linear_prob :: write(int rec)
{
table1.open(table,ios::binary|ios::nocreate|ios::out|ios::in);
table1.seekg(rec*sizeof(bank),ios::beg);
table1.write((char
*)&b,sizeof(bank));
table1.close();
}
/*----------<< Function defination for linear probing
>>------------*/
linear_prob
:: linear_prob(char *a)
{
int i;
strcpy(table,a);
b.status=1;
b.link=-1;
table1.open(table,ios::binary|ios::out);
table1.close();
for(i=0;i<size;i++)
{
write(i);
}
}
/*----------<< Function defination for display
>>------------*/
void
linear_prob :: display()
{
int i=1;
cout<<"\n---------<<
FILE >>-------\n\n";
for(i=0;i<size;i++)
{
display1(i);
}
}
/*----------<< Function defination for insert
>>------------*/
void
linear_prob :: insert(bank b1)
{
int n,i,j,k,start;
b1.status=0;
b1.link=-1;
start=hash(b1.acc_no);
for(i=0;i<size;i++)
{
j=(start+i)%size;
read(j);
if(b.status==0
&& hash(b.acc_no)==start)
{
break;
}
}
if(i<size)
{
while(b.link!=-1)
{
j=b.link;
read(j);
}
for(i=0;i<size;i++)
{
k=(start+i)%size;
read(k);
if(b.status==1)
{
b=b1;
write(k);
read(j);
b.link=k;
write(j);
return;
}
}
cout<<"\nTable
is FULL";
}
else
{
for(i=0;i<size;i++)
{
k=(start+i)%size;
read(k);
if(b.status==1)
{
b=b1;
write(k);
return;
}
}
cout<<"\nTable
is Full..." ;
}
}
/*----------<< Function defination for delete
>>------------*/
void
linear_prob :: delet(int acc_no)
{
bank b1;
int rec,i,j,k,start;
start=hash(acc_no);
for(i=0;i<size;i++)
{
j=(start+i)%size;
read(j);
if(b.status==0
&& hash(b.acc_no)==start)
{
break;
}
}
if(i<10)
{
if(b.acc_no==acc_no)
{
b.status=1;
write(j);
}
else
{
while(b.acc_no!=acc_no
&& b.link!=-1)
{
k=j;
j=b.link;
read(j);
}
if(b.acc_no==acc_no)
{
b.status=1;
write(j);
int
nextlink=b.link;
read(k);
b.link=nextlink;
write(k);
}
else
{
cout<<"\n\nRecord
not found";
}
}
}
else
{
cout<<"\n\nRecord
not found";
}
}
/*----------<< Function defination for search
>>------------*/
int
linear_prob :: search(int acc_no)
{
int i,j,start;
start=hash(acc_no);
for(i=0;i<size;i++)
{
j=(start+i)%size;
read(j);
if(b.status==0
&& hash(b.acc_no)==start)
{
break;
}
}
if(i<size)
{
while(b.acc_no!=acc_no
&& b.link!=-1)
{
j=b.link;
read(j);
}
if(b.acc_no==acc_no)
{
return (j);
}
else
{
return(-1);
}
}
else
return(-1);
}
/*-------------<< Main Function Starts >>------------*/
void
main()
{
linear_prob
lp("Bank.txt");
int acc_no,ch,rec;
bank b1;
clrscr();
do
{
cout<<"\n\t--------<<
MENU >>-------" ;
cout<<"\n\t1.Insert";
cout<<"\n\t2.Print";
cout<<"\n\t3.Delete";
cout<<"\n\t4.Search";
cout<<"\n\t5.Exit";
cout<<"\n\t--------------------------";
cout<<"\n\tEnter
your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\nInsert
the record:\n";
cout<<"\nAccount
No:\t";
cin>>b1.acc_no;
cout<<"\nName:\t";
cin>>b1.name;
cout<<"\nBalance:\t";
cin>>b1.bal;
lp.insert(b1);
break;
case 2:
lp.display();
break;
case 3:
cout<<"\nEnter
the acc no which want to delete";
cin>>acc_no;
lp.delet(acc_no);
break;
case 4:
cout<<"\nEnter
the acc no which want to search";
cin>>acc_no;
rec=lp.search(acc_no);
if(rec>=0)
{
lp.display1(rec);
}
else
{
cout<<"\nRecord
not found";
}
break;
}
}while(ch!=5);
}
/*-------------<< End Of Main Function >>---------------*/
/*-----------------<< OUTPUT SCREEN
>>-------------------*/
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:1
Insert
the record:
Account
No: 13
Name: JABIR
Balance: 3000
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:1
Insert
the record:
Account
No: 15
Name: RITESH
Balance: 25000
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:1
Insert
the record:
Account
No: 16
Name: AKASH
Balance: 20000
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:2
---------<<
FILE >>-------
0)EMPTY
1)EMPTY
2)EMPTY
3)13 JABIR
3000 -1
4)EMPTY
5)15 RITESH
25000 -1
6)16
AKASH 20000
-1
7)EMPTY
8)EMPTY
9)EMPTY
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:1
Insert
the record:
Account
No: 23
Name: NIKHIL
Balance: 23000
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:1
Insert
the record:
Account
No: 25
Name: MANOJ
Balance: 24000
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:2
---------<<
FILE >>-------
0)EMPTY
1)EMPTY
2)EMPTY
3)13 JABIR
3000 4
4)23 NIKHIL
23000 -1
5)15 RITESH
25000 7
6)16 AKASH
20000 -1
7)25 MANOJ
24000 -1
8)EMPTY
9)EMPTY
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:3
Enter
the acc no which want to delete23
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:2
---------<<
FILE >>-------
0)EMPTY
1)EMPTY
2)EMPTY
3)13 JABIR
3000 -1
4)EMPTY
5)15 RITESH
25000 7
6)16 AKASH
20000 -1
7)25 MANOJ
24000 -1
8)EMPTY
9)EMPTY
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:4
Enter
the acc no which want to search13
3)13 JABIR
3000 -1
--------<< MENU >>-------
1.Insert
2.Print
3.Delete
4.Search
5.Exit
--------------------------
Enter your choice:5
No comments:
Post a Comment