You are on page 1of 88

FS LAB MANUAL

BANGALORE INSTITUTE OF TECHNOLOGY


K.R. ROAD, V.V PURAM, BANGALORE – 560 004

(AFFILIATED TO VTU, BELGUAM)

CODE: 10ISL67

FILE STRUCTURE LABORATORY

FOR VI SEMESTER ISE AS PRESCRIBED BY VTU

Prepared By:
Prof. B.R. Shivakumar, M.Tech,
Assistant Professor
Dept of ISE, BIT

DEPARTMENT OF
INFORMATION SCIENCE AND ENGINEERING

Dept. of ISE Bangalore Institute of Technology Page 1


FS LAB MANUAL

PROGRAM 1:-
Write a C++ program to read series of names, one per line, from standard input and write
these names spelled in reverse order to the standard output using I/O redirection and
pipes. Repeat the exercise using an input file specified by the user instead of the standard
input and using an output file specified by the user instead of the standard output.

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

void reverse(char *name)


{
int i,j,size;
char temp;
size=strlen(name);

for(i=0,j=size-1;i<j;i++,j--)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}

main(int argc,char *argv[])


{
char name[30];
if(argc==1)
{
do
{
cin>>name;
reverse(name);
cout<<name;

Dept. of ISE Bangalore Institute of Technology Page 2


FS LAB MANUAL

if(cin.eof())
break;
}while(1);
}
else if(argc>2)
{
fstream ip,op;
ip.open(argv[1],ios::in);
op.open(argv[2],ios::out|ios::app);

do
{
if(ip.eof())
break;
ip>>name;
reverse(name);
cout<<name<<endl;
op<<name<<endl;

}while(1);
ip.close();
op.close();
}
else
cout<<"error";
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 3


FS LAB MANUAL

OUTPUT1:-

[root@localhost fslab]# ./a.out


vijay
yajiv

bit bangalore
tiberolagnab

information
noitamrofni
[1]+ Stopped ./a.out

[root@localhost fslab]# vi in.txt

[root@localhost fslab]# ./a.out in.txt out.txt


noitamrofni
ecneics
tib
erolagnab

Note : in.txt should be edited by the student before execution. The output file out.txt must be
verified after execution

Dept. of ISE Bangalore Institute of Technology Page 4


FS LAB MANUAL

PROGRAM 2:-
Write a C++ program to read and write student objects with fixed-length records and the
fields delimited by “|”. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;

class FLB;
class person
{
char usn[30],name[30],addr[30],branch[30],colg[30];
public:
void input();
void output();
void search(char *fname);
void modify(char *fname);
friend class FLB;
};
class FLB
{
char buff[160];
public:
FLB()
{
for(int i=0;i<160;i++)
buff[i]='\0';
}
void pack(person &p);
void unpack(person &p);
void read(fstream &fs);
void write(char *fname);
};
void person::input()
{
cout<<"enter usn,name,address,branch and college:";
cin>>usn>>name>>addr>>branch>>colg;
}
void person::output()
{

Dept. of ISE Bangalore Institute of Technology Page 5


FS LAB MANUAL

cout<<"usn: "<<usn<<"\nname: "<<name<<"\naddress: "<<addr<<"\nbranch:


"<<branch<<"\ncollege: "<<colg;
}
void FLB::pack(person &p)
{
strcpy(buff,p.usn);strcat(buff,"|");
strcat(buff,p.name);strcat(buff,"|");
strcat(buff,p.addr);strcat(buff,"|");
strcat(buff,p.branch);strcat(buff,"|");
strcat(buff,p.colg);
}
void FLB::unpack(person &p)
{
char *ptr=buff;
while(*ptr)
{
if(*ptr=='|')
*ptr='\0';
ptr++;
}
ptr=buff;
strcpy(p.usn,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.name,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.addr,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.branch,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.colg,ptr);
}
void FLB::read(fstream &fs)
{
fs.read(buff,sizeof(buff));
}
void FLB::write(char*fname)
{
fstream os(fname,ios::out|ios::app);
os.write(buff,sizeof(buff));
os.close();
}
void person::search(char * fname)
{

Dept. of ISE Bangalore Institute of Technology Page 6


FS LAB MANUAL

int found=0;
person p;
FLB b;
char key[30];
fstream is(fname,ios::in);
cout<<"enter the usn to be searched:";
cin>>key;
while((!is.eof())&&(!found))
{
b.read(is);
if(is.eof())
break;
b.unpack(p);
if(strcmp(p.usn,key)==0)
{
cout<<"record found!!\n";
p.output();
found=1;
}
}
if(!found)
cout<<"record with given usn does't exist\n";
is.close();
}
void person::modify(char*fname)
{
int found=0;
person p;
FLB b;
char key[30],tname[]="temp.txt";
fstream is(fname,ios::in);
fstream tfile(tname,ios::out|ios::app);
cout<<"enter the usn of record to be modified:";
cin>>key;
while((!is.eof()))
{
b.read(is);
if(is.eof())
break;
b.unpack(p);
if(strcmp(p.usn,key)==0 && !found)
{
cout<<"record found!!\n";

Dept. of ISE Bangalore Institute of Technology Page 7


FS LAB MANUAL

p.input();
found=1;
}
b.pack(p);
b.write(tname);
}
if(!found)
cout<<"record with given usn does't exist\n";
is.close();
tfile.close();
remove(fname);
rename(tname,fname);
}

main()
{
int ch;
person p;
FLB b;
char fname[]="prg2.txt";
do
{
cout<<"1:insert\t2:search\t3:modify\t4:exit\nenter your choice";
cin>>ch;
switch(ch)
{
case 1:p.input();
b.pack(p);
b.write(fname);
break;
case 2:p.search(fname);
break;
case 3:
p.modify(fname);
break;
case 4:break;
}
}while(ch!=4);

return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 8


FS LAB MANUAL

OUTPUT:-

1:insert 2:search 3:modify 4:exit

enter your choice1

enter usn,name,address,branch and college:101 Shivakumar Bangalore ISE BIT

1:insert 2:search 3:modify 4:exit

enter your choice 1

enter usn,name,address,branch and college: 202 Vijay Hassan CSE MCE

1:insert 2:search 3:modify 4:exit

enter your choice 2

enter the usn to be searched:202

record found!!

usn: 202

name: Vijay

address: Hassan

branch: CSE

college: MCE

1:insert 2:search 3:modify 4:exit

enter your choice 3

enter the usn of record to be modified:101

record found!!

enter usn,name,address,branch and college:300 Arnold Newyork CSE BIT

Dept. of ISE Bangalore Institute of Technology Page 9


FS LAB MANUAL

1:insert 2:search 3:modify 4:exit

enter your choice 2

enter the usn to be searched:300

record found!!

usn: 300

name: Arnold

address: Newyork

branch: CSE

college: BIT1:insert 2:search 3:modify 4:exit

Dept. of ISE Bangalore Institute of Technology Page 10


FS LAB MANUAL

PROGRAM 3:-
Write a C++ program to read and write student objects with Variable - Length records
using any suitable record structure. Implement pack ( ), unpack ( ), modify ( ) and search
( ) methods.

#include<fstream>
#include<iostream>
#include<string.h>

using namespace std;

int n, no_of_rec, length, len[20];

class student
{
public:
char usn[20], sem[20], name[20], branch[20];
};
class textbuff
{
public:
char buff[500];
void pack(student s[])
{
fstream file1("first.txt", ios::out | ios::app);
clear_buff();
for(int i = 0; i < n; i++)
{
strcat(buff, s[i].usn);
strcat(buff, "|");
strcat(buff, s[i].name);
strcat(buff, "|");
strcat(buff, s[i].branch);
strcat(buff, "|");
strcat(buff, s[i].sem);
strcat(buff, "|");
no_of_rec++;
len[no_of_rec] = strlen(buff) + length;
}

Dept. of ISE Bangalore Institute of Technology Page 11


FS LAB MANUAL

length = strlen(buff);
file1<<buff;
file1.close();
}

void unpack(student s[])


{
fstream file2("first.txt", ios::in);
clear_buff();
for(int i = 0; i < no_of_rec; i++)
{
file2.getline(s[i].usn, 20, '|');
file2.getline(s[i].name, 20, '|');
file2.getline(s[i].branch, 20, '|');
file2.getline(s[i].sem, 20, '|');
}
}

void clear_buff()
{
for(int i = 0; i < 500; i++)
buff[i] = NULL;
}
void display(student s[])
{
char ch;
if(no_of_rec != 0)
{
cout<<"USN\tName\tBranch\tSem\n";
for(int i = 0; i < no_of_rec; i++)
{
ch = s[i].usn[0];
if(ch != '*')

cout<<s[i].usn<<"\t"<<s[i].name<<"\t"<<s[i].branch<<"\t"<<s[i].sem<<endl;
}
}
else
cout<<"No record found\n";
}
void search(student s[])

Dept. of ISE Bangalore Institute of Technology Page 12


FS LAB MANUAL

{
int flag = 0;
char key[20];
clear_buff();
cout<<"Enter the USN for search: ";
cin>>key;
for(int i = 0; i < no_of_rec; i++)
{
if(strcmp(key, s[i].usn) == 0)
{
cout<<"Record found!!\n";
cout<<"USN: "<<s[i].usn<<", Name: "<<s[i].name<<", Branch:
"<<s[i].branch<<", Sem: "<<s[i].sem<<endl;
flag = 1;
}
}
if(!flag)
cout<<"No such USN found\n";
}
void modify(student s[])
{
char key[20], cvar[20];
/*int len1[20];*/
int flag = 0, len_field;
/*for(int i = 1; i <= no_of_rec; i++)
len1[i] = len[i] - len[i-1];*/
cout<<"Enter the USN of record to be modified: ";
cin>>key;
for(int i = 0; i < no_of_rec; i++)
{
if(strcmp(key, s[i].usn) == 0)
{
fstream file3("first.txt", ios::in | ios::out);
fstream file4("first.txt", ios::out | ios::app);
file3.seekg(len[i], ios::beg);
file3.getline(cvar, 20, '|');
len_field = strlen(cvar); //length of 1 field
file3.seekp(len[i], ios::beg);
for(int j = 0; j < len_field; j++)
file3<<'*';
file3.close();
cout<<"Enter new data:\nEnter USN, name, branch and sem:\n";
cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem;

Dept. of ISE Bangalore Institute of Technology Page 13


FS LAB MANUAL

clear_buff();
strcat(buff, s[i].usn);
strcat(buff, "|");
strcat(buff, s[i].name);
strcat(buff, "|");
strcat(buff, s[i].branch);
strcat(buff, "|");
strcat(buff, s[i].sem);
strcat(buff, "|");
no_of_rec++;
len[no_of_rec] = strlen(buff) + length;
// file3.seekp(0, ios::end);
file4<<buff;
file4.close();
flag = 1;
}
}
if(!flag)
cout<<"No such USN found\n";
}
};

main()
{
int ch, i;
student s[20];
textbuff b;
cout<<"1.Create\n2.Display\n3.Search\n4.Modify\n5.Exit\n";
do
{
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter no. of records: ";
cin>>n;
for(i = 0; i < n; i++)
{
cout<<"Enter USN, name, branch and sem of student "<<i+1<<endl;
cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem;
}
b.pack(s);

Dept. of ISE Bangalore Institute of Technology Page 14


FS LAB MANUAL

break;
case 2: b.unpack(s);
b.display(s);
break;
case 3: b.unpack(s);
b.search(s);
break;
case 4: b.unpack(s);
b.modify(s);
break;
case 5: break;
}
}while(ch != 5);

return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 15


FS LAB MANUAL

OUTPUT:-

Enter USN, name, branch and sem of student 3

300 SauravG ECE 6

Enter USN, name, branch and sem of student 4

400 Kapil Mech 8

Enter your choice: 2

USN Name Branch Sem

100 Sachin ISE 6

200 R.Dravid CSE 6

300 SauravG ECE 6

400 Kapil Mech 8

Enter your choice: 3

Enter the USN for search: 400

Record found!!

USN: 400, Name: Kapil, Branch: Mech, Sem: 8

Enter your choice: 4

Enter the USN of record to be modified: 100

Enter new data:

Enter USN, name, branch and sem:

500 Gavaskar IT 9

Enter your choice: 2

USN Name Branch Sem


Dept. of ISE Bangalore Institute of Technology Page 16
FS LAB MANUAL

200 R.Dravid CSE 6

300 SauravG ECE 6

400 Kapil Mech 8

500 Gavaskar IT 9

Dept. of ISE Bangalore Institute of Technology Page 17


FS LAB MANUAL

PROGRAM 4:-
Write a C++ program to write student objects with Variable – Length records using any
suitable record structure and to read from this file a student record using RRN.

#include<iostream>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<string.h>

using namespace std;

class delimtextbuffer;

class person
{
char usn[20];
char name[18];
char address[20];
char branch[20];
char college[20];
public:
void input();
void output();
friend class delimtextbuffer;
};
class delimtextbuffer
{
char buffer[160];
char delim;
public:
void pack(person& p);

Dept. of ISE Bangalore Institute of Technology Page 18


FS LAB MANUAL

void unpack(person& p);


void Read(fstream& fs);
int Write(char *filename);
delimtextbuffer();
};
class operations
{
char *rrnfilename;
char *recordfilename;
public:
int maxrecords();
operations(char *rrnfile,char *recordfile);
void search();
void insert();
};
operations::operations(char *rrnfile,char *recordfile)
{
rrnfilename=rrnfile;
recordfilename=recordfile;
}
int operations::maxrecords()
{
fstream file(rrnfilename,ios::in);
int pos;
file.seekg(0,ios::end);
pos=file.tellg();
return (pos/(sizeof(int)));
}
void person::input()
{
cout<<"Enter usn"<<endl;
cin>>usn;

Dept. of ISE Bangalore Institute of Technology Page 19


FS LAB MANUAL

cout<<"Enter name"<<endl;
cin>>name;
cout<<"Enter address"<<endl;
cin>>address;
cout<<"Enter branch"<<endl;
cin>>branch;
cout<<"Enter college"<<endl;
cin>>college;
}
void person::output()
{
cout<<"USN:";
puts(usn);
cout<<"Name:";
puts(name);
cout<<"Address:";
puts(address);
cout<<"Branch:";
puts(branch);
cout<<"College:";
puts(college);
}
delimtextbuffer::delimtextbuffer()
{
for(int i=0;i<160;i++)
buffer[i]='\0';
delim='|';
}
void delimtextbuffer::pack(person &p)
{
strcpy(buffer, p.usn);
strcat(buffer, "|");

Dept. of ISE Bangalore Institute of Technology Page 20


FS LAB MANUAL

strcat(buffer, p.name);
strcat(buffer, "|");
strcat(buffer, p.address);
strcat(buffer, "|");
strcat(buffer, p.branch);
strcat(buffer, "|");
strcat(buffer, p.college);
strcat(buffer, "|");
strcat(buffer,"*");
}
void delimtextbuffer::unpack(person &p)
{
char *ptr=buffer;
while(*ptr)
{
if(*ptr == '|')
*ptr = '\0';
ptr++;
}
ptr = buffer;
strcpy(p.usn, ptr);
ptr += strlen(ptr) + 1;
strcpy(p.name, ptr);
ptr += strlen(ptr) + 1;
strcpy(p.address, ptr);
ptr += strlen(ptr) + 1;
strcpy(p.branch, ptr);
ptr += strlen(ptr) + 1;
strcpy(p.college, ptr);
}
void delimtextbuffer::Read(fstream& fs)
{

Dept. of ISE Bangalore Institute of Technology Page 21


FS LAB MANUAL

fs.getline(buffer,160,'*');
}
int delimtextbuffer::Write(char *filename)
{
fstream os(filename,ios::out|ios::app);
os.seekg(0,ios::end);
int offset=os.tellp();
os.write(buffer,strlen(buffer));
os.close();
return offset;
}
void operations::search()
{
int rrn;
cout<<"enter the RRN of the record to be searched(1 - based)";
cin>>rrn;
if(rrn>maxrecords()||rrn<=0)
{
cout<<"record not found!!!"<<endl;
return;
}
delimtextbuffer b;
person p;
int offset=8;
fstream file(rrnfilename,ios::in);

file.seekg((rrn-1)*sizeof(int),ios::beg);
file.read((char*)&offset,sizeof(offset));

file.close();
cout<<"record found!!"<<endl;
fstream file1(recordfilename,ios::in);

Dept. of ISE Bangalore Institute of Technology Page 22


FS LAB MANUAL

file1.seekg(offset,ios::beg);
b.Read(file1);
b.unpack(p);
p.output();
file1.close();
}
void operations::insert()
{
person ob;
delimtextbuffer b;
int offset;
fstream file,file2;
ob.input();
b.pack(ob);
offset=b.Write(recordfilename);
file.open(rrnfilename,ios::out|ios::app);
file2.open("rr.txt",ios::out|ios::app);
file2<<offset;
file.write((char*)&offset,sizeof(int));
file.close();
}
int main()
{
int choice=1;
fstream file,file1;
person ob;
delimtextbuffer b;
char filename[20]="name.txt";
char rrnfilename[20]="rrn.txt";
operations o(rrnfilename,filename);
while(choice<3)
{

Dept. of ISE Bangalore Institute of Technology Page 23


FS LAB MANUAL

cout<<"1:Insert a Record"<<endl;
cout<<"2:Search for a Record"<<endl;
cout<<"3:exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:o.insert();
break;
case 2:o.search();
break;
}
}
return 0;
}

OUTPUT:-
1:Insert a Record
2:Search for a Record
3:exit
1
Enter usn
100
Enter name
Darshan
Enter address
Vijayanagar
Enter branch
ISE
Enter college
BIT

Dept. of ISE Bangalore Institute of Technology Page 24


FS LAB MANUAL

1:Insert a Record
2:Search for a Record
3:exit
1
Enter usn
200
Enter name
Bhimsen
Enter address
Pune
Enter branch
CSE
Enter college
MIT
1:Insert a Record
2:Search for a Record
3:exit
2
enter the RRN of the record to be searched(1 - based)2
record found!!
200
Bhimsen
Pune
CSE
MIT
USN:Name:Address:Branch:College:1:Insert a Record
2:Search for a Record
3:exit

Dept. of ISE Bangalore Institute of Technology Page 25


FS LAB MANUAL

Program 5:-

Write a C++ program to implement simple index on primary key for a file of student
objects. Implement the index.
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int index1[100],indexadd[100];
int nor;

void smi()
{
int temp,temp1;
int i,j;
for(i=0;i<nor;i++)
for(j=i+1;j<nor;j++)
if(index1[i]>index1[j])
{
temp=index1[i];
index1[i]=index1[j];
index1[j]=temp;

temp1=indexadd[i];
indexadd[i]=indexadd[j];
indexadd[j]=temp1;
}
}

Dept. of ISE Bangalore Institute of Technology Page 26


FS LAB MANUAL

class student{
public:
int usn;
char name[15],sem[15],branch[15],address[15];
void add1();
void unpack(int add);
void search();
void del();
void dis();
};

void student::add1()
{
char buff[150];
fstream file("record.txt",ios::out|ios::app);
fstream file2("index.txt",ios::out|ios::app);
cout<<"Enter the usn,name,sem,branch and address\n";
cin>>usn;
cin>>name>>sem>>branch>>address;
strcpy(buff,name);
strcat(buff,"|");
strcat(buff,sem);
strcat(buff,"|");
strcat(buff,branch);
strcat(buff,"|");
strcat(buff,address);
strcat(buff,"|");

Dept. of ISE Bangalore Institute of Technology Page 27


FS LAB MANUAL

index1[nor]=usn;
file.seekg(0,ios::end);
indexadd[nor]=file.tellp();
file2<<usn<<"|"<<file.tellp()<<endl;
file<<usn<<buff;
nor++;
smi();
}

void student::unpack(int add)


{
fstream file("record.txt",ios::in);
file.seekg(add);
file>>usn;
file.getline(name,15,'|');
file.getline(sem,15,'|');
file.getline(branch,15,'|');
file.getline(address,15,'|');
}

void student::search()
{
cout<<"Enter the USN to be searched\n";
int min=0;
int max=nor-1;
int flag=0;
int key;
int mid;
int add;

Dept. of ISE Bangalore Institute of Technology Page 28


FS LAB MANUAL

student s;
cin>>key;
while(min<=max && flag==0)
{
mid=(min+max)/2;
if(index1[mid]==key)
{
flag=1;
add=indexadd[mid];
s.unpack(add);

cout<<"USN:"<<s.usn<<"NAME:"<<s.name<<"SEM:"<<s.sem<<"BRANCH"<<s.branc
h<<"ADDRESS:"<<s.address<<endl;
}
else if(key>index1[mid])
min=mid+1;
else
max=mid-1;
}
if(flag==0)
cout<<"Record not found\n";
}

void student::del()
{
int key;
cout<<"Enter the USN to be found\n";
cin>>key;

Dept. of ISE Bangalore Institute of Technology Page 29


FS LAB MANUAL

int i;
int min=0,max=nor-1,mid;
while(min<=max)
{
mid=(min+mid)/2;
if(index1[mid]==key)
{
for(i=mid;i<nor;i++)
{
index1[i]=index1[i+1];
indexadd[i]=indexadd[i+1];
}
nor--;
}
else if(key>index1[mid])
min = mid+1;
else
max=mid-1;
}
fstream fs("index.txt",ios::out|ios::app);
for(i=0;i<nor;i++)
fs<<index1[i]<<"|"<<indexadd[i]<<endl;
fs.close();

void student::dis()
{
int i;

Dept. of ISE Bangalore Institute of Technology Page 30


FS LAB MANUAL

int add;
for(i=0;i<nor;i++)
{
add=indexadd[i];
unpack(add);

cout<<"USN:"<<usn<<"\tNAME:"<<name<<"\tSEM:"<<sem<<"\tBRANCH:"<<branch
<<"\tADDRESS:"<<address<<endl;
}
}
int main()
{
int ch;
student s;
for(;;)
{
cout<<"1.ADD 2.SEARCH 3.DISPLAY 4.EXIT\n";
cout<<"Enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:s.add1();break;
case 2:s.search();break;
case 3:s.dis();break;
case 4:exit(0);
}
}
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 31


FS LAB MANUAL

OUTPUT:-

1.ADD 2.SEARCH 3.DISPLAY 4.EXIT

enter your choice:1

enter usn,name,branch,and sem:


100 Ramya ISE 6
enter your choice:1

enter usn,name,branch,and sem:


200 Pooja CSE 6
enter your choice:2

enter usn for search:Pooja

search failed!!
enter your choice:2

enter usn for search:200


record found
usn:200 name:Pooja branch:CSE sem:6
enter your choice:3

enter usn to delete:200

record delated
enter your choice:4
usn name branch sem
100 Ramya ISE 6
enter your choice:

Dept. of ISE Bangalore Institute of Technology Page 32


FS LAB MANUAL

Program 6:-
Write a C++ program to implement index on secondary key, the name, for a file of
student objects. Implement add ( ), search ( ), delete ( ) using the secondary index.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
using namespace std;

class record
{
public:
char usn[10],name[20],branch[10],sem[2];
}rec[20],found[20];

int m=0,n;

void add()
{
int i;
cout<<"Enter number of students: ";
cin>>n;
cout<<"Enter Name, USN, Branch and Sem\n";
n=m+n;
for(i=m;i<n;i++)
{
cout<<"Student "<<i+1<<" :\n";
cin>>rec[i].name>>rec[i].usn>>rec[i].branch>>rec[i].sem;
m++;
}

Dept. of ISE Bangalore Institute of Technology Page 33


FS LAB MANUAL

}
void sort_records()
{
int i,j;
record temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(rec[j].name,rec[j+1].name)>0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
else if(strcmp(rec[j].name,rec[j+1].name)==0)
{
if(strcmp(rec[j].usn,rec[j+1].usn)>0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
}
}
}
}

void create_file()
{
fstream index("secindex.txt",ios::out);
fstream file("record.txt",ios::out);

Dept. of ISE Bangalore Institute of Technology Page 34


FS LAB MANUAL

for(int i=0;i<n;i++)
{
index<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";

file<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].branch<<"|"<<rec[i].se
m<<"\n";
}
index.close();
file.close();
}

void retrieve_record(char *index)


{
fstream f1("record.txt",ios::in);
int i;
char ind[3],usn[10],name[20],branch[10],sem[2];
for(i=0;i<n;i++)
{
f1.getline(ind,3,'|');
f1.getline(usn,10,'|');
f1.getline(name,20,'|');
f1.getline(branch,10,'|');
f1.getline(sem,2,'\n');
if(strcmp(index,ind)==0)
cout<<"USN: "<<usn<<" Name: "<<name<<" Branch:
"<<branch<<" Sem: "<<sem<<endl;
f1.seekg(2,ios::cur);
}
f1.close();
}

void search()

Dept. of ISE Bangalore Institute of Technology Page 35


FS LAB MANUAL

{
int k=0,i,flag;
fstream f1("secindex.txt",ios::in);
char name[20],usn[10],ind[3],key_usn[10],key_name[20];
char index[20][20];
cout<<"Enter the name for search: ";
cin>>key_name;
for(i=0;i<n;i++)
{
f1.getline(name,20,'|');
f1.getline(usn,10,'|');
f1.getline(ind,3,'\n');
if(strcmp(key_name,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
f1.close();
if(k==0)
cout<<"Search failed!!\n";
else if(k==1)
retrieve_record(index[0]);
else
{
cout<<"Choose USN from the list:\n";
for(i=0;i<k;i++)
cout<<found[i].usn<<endl;
cin>>key_usn;
flag=0;

Dept. of ISE Bangalore Institute of Technology Page 36


FS LAB MANUAL

for(i=0;i<k;i++)
{
if(strcmp(key_usn,found[i].usn)==0)
{
retrieve_record(index[i]);
flag=1;
}
}
if(!flag)
cout<<"Invalid entry!!\n";
}
}

void delete_record(char *indx)


{
int i,flag;
char ch;
fstream f1("record.txt",ios::in);
char index[20][20];
for(i=0;i<n;i++)
{
f1.getline(index[i],3,'|');
f1.getline(rec[i].usn,10,'|');
f1.getline(rec[i].name,20,'|');
f1.getline(rec[i].branch,10,'|');
f1.getline(rec[i].sem,2,'\n');
f1.seekg(2,ios::cur);
cout<<f1.tellg()<<endl;
}
flag=-1;
for(i=0;i<n;i++)
if(strcmp(index[i],indx)==0)

Dept. of ISE Bangalore Institute of Technology Page 37


FS LAB MANUAL

flag=i;
if(flag!=n-1)
{
for(i=flag;i<n;i++)
rec[i]=rec[i+1];
}
n--;
cout<<"Record deleted!!\n";
f1.close();
f1.open("secindex.txt",ios::out);
fstream f2("record.txt",ios::out);
for(i=0;i<n;i++)
{
f1<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";

f2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].branch<<"|"<<rec[i].sem
<<"\n";
}
f1.close();
f2.close();
}
void del()
{
int k=0,i,flag;
fstream f1("secindex.txt",ios::in);
char name[20],usn[10],ind[3],key_usn[10],key_name[20];
char index[20][20];
cout<<"Enter the name to delete: ";
cin>>key_name;
for(i=0;i<n;i++)
{
f1.getline(name,20,'|');

Dept. of ISE Bangalore Institute of Technology Page 38


FS LAB MANUAL

f1.getline(usn,10,'|');
f1.getline(ind,3,'\n');
if(strcmp(key_name,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
f1.close();
if(k==0)
cout<<"Deletion failed!!\n";
else if(k==1)
delete_record(index[0]);
else
{
cout<<"Choose USN from the list:\n";
for(i=0;i<k;i++)
cout<<found[i].usn<<endl;
cin>>key_usn;
flag=0;
for(i=0;i<k;i++)
{
if(strcmp(key_usn,found[i].usn)==0)
{
delete_record(index[i]);
flag=1;
}}
if(!flag)
cout<<"Invalid entry!!\n";
}}

Dept. of ISE Bangalore Institute of Technology Page 39


FS LAB MANUAL

int main()
{
int ch;
cout<<"1:Add\n2:Search\n3:Delete\n4:Exit\n";
do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
add();
sort_records();
create_file();
break;

case 2:
search();
break;

case 3:
del();
break;

case 4:
remove("secindex.txt");
remove("record.txt");
break;
}
}while(ch!=4);
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 40


FS LAB MANUAL

OUTPUT:-

1:Add
2:Search
3:Delete
4:Exit

Enter your choice: 1


Enter number of students: 2
Enter Name, USN, Branch and Sem
Student 1 :
Abc 100 ise 2
Student 2 :
Xyz 200 ise 6

Enter your choice: 2


Enter the name for search: xyz
USN: 200 Name: xyz Branch: ise Sem: 6

Enter your choice: 3


Enter the name to delete: abc
Record deleted!!

Enter your choice: 2


Enter the name for search: abc
Search failed!!

Enter your choice: 2


Enter the name for search: xyz
USN: 200 Name: xyz Branch: ise Sem: 6

Dept. of ISE Bangalore Institute of Technology Page 41


FS LAB MANUAL

PROGRAM 7:-
Write a C++ program to read two lists of names and then match the names in the two lists
using Consequential Match based on a single loop. Output the names common to both the
lists.

#include<fstream>
#include<iostream>
#include<stdio.h>
#include<string.h>

#include<stdlib.h>

using namespace std;

void write_file()
{
char name[20][20],temp[20];
fstream f1("file1.txt",ios::out);
fstream f2("file2.txt",ios::out);
int i,j,m,n;
if((!f1)||(!f2))
{
cout<<"Unable to open the files!!\n";
exit(0);
}
cout<<"Enter the number of records in file 1: ";
cin>>m;
cout<<"Enter the names:\n";
for(i=0;i<m;i++)
cin>>name[i];
for(i=0;i<m-1;i++)
{

Dept. of ISE Bangalore Institute of Technology Page 42


FS LAB MANUAL

for(j=0;j<m-i-1;j++)
{
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
}
for(i=0;i<m;i++)
f1<<name[i]<<endl;
cout<<"Enter the number of records in file 2: ";
cin>>n;
cout<<"Enter the names:\n";
for(i=0;i<n;i++)
cin>>name[i];
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
}
for(i=0;i<n;i++)
f2<<name[i]<<endl;
f1.close();

Dept. of ISE Bangalore Institute of Technology Page 43


FS LAB MANUAL

f2.close();
}

int main()
{
fstream f1("file1.txt",ios::in);
fstream f2("file2.txt",ios::in);
fstream f3("output.txt",ios::out);
char l1[100][20],l2[100][20],l3[100][20];
int i=0,j=0,k=0,m=0,n=0;
write_file();
if((!f1)||(!f2)||(!f3))
{
cout<<"Unable to open the files!!\n";
exit(0);
}
cout<<"Contents of file 1 are:\n";
while(!f1.eof())
{
f1.getline(l1[i],20,'\n');
cout<<l1[i++]<<endl;
m++;
}
cout<<"Contents of file 2 are:\n";
while(!f2.eof())
{
f2.getline(l2[j],20,'\n');
cout<<l2[j++]<<endl;
n++;
}
m--;
n--;

Dept. of ISE Bangalore Institute of Technology Page 44


FS LAB MANUAL

i=0,j=0;
while(i<m&&j<n)
{
if(strcmp(l1[i],l2[j])==0)
{
strcpy(l3[k],l1[i]);
f3<<l3[k]<<endl;
i++;
j++;
k++;
}
else if(strcmp(l1[i],l2[j])<0)
i++;
else
j++;
}
if(k==0)
cout<<"No common names!!\n";
else
{
cout<<"The names that are common are:\n";
for(i=0;i<k;i++)
cout<<l3[i]<<endl;
}
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 45


FS LAB MANUAL

Output:

Enter the number of records in file 1: 4


Enter the names:
Shiva
Puneeth
Rajanikanth
Amithabh
Enter the number of records in file 2: 4
Enter the names:
Sunil
Anilkapoor
Amithabh
Rajkapoor

Contents of file 1 are:


Amithabh
Puneeth
Rajanikanth
Shiva

Contents of file 2 are:


Amithabh
Anilkapoor
Rajkapoor
Sunil

The names that are common are:


Amithabh

Dept. of ISE Bangalore Institute of Technology Page 46


FS LAB MANUAL

PROGRAM 8:-

Write a C++ program to read k lists of names and merge them using ‘k way merge’
algorithm with k=8.

#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

class record
{
public:
char name[20],usn[20];
}rec[20];

char fname[8][8]={"1.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
fstream file[8];
int n;
void merge_file(char *fil1,char *fil2,char *fil)
{
record rcd[20];
fstream f1(fil1,ios::in);
fstream f2(fil2,ios::in);
fstream f3(fil,ios::out);
int i,j,k=0,k1;
record temp;
while(!f1.eof())
{

Dept. of ISE Bangalore Institute of Technology Page 47


FS LAB MANUAL

f1.getline(rcd[k].name,20,'|');
f1.getline(rcd[k++].usn,20);
}
while(!f2.eof())
{
f2.getline(rcd[k].name,20,'|');
f2.getline(rcd[k++].usn,20);
}
k1=k;
for(i=0;i<k-2;i++)
{
for(j=0;j<k-i-2;j++)
{
if(strcmp(rcd[j].name,rcd[j+1].name)>0)
{
temp=rcd[j];
rcd[j]=rcd[j+1];
rcd[j+1]=temp;
}
else if(strcmp(rcd[j].name,rcd[j+1].name)==0)
{
if(strcmp(rcd[j].usn,rcd[j+1].usn)>0)
{
temp=rcd[j];
rcd[j]=rcd[j+1];
rcd[j+1]=temp;
}
else if(strcmp(rcd[j].usn,rcd[j+1].usn)==0)
{
j++;
k1--;
n--;

Dept. of ISE Bangalore Institute of Technology Page 48


FS LAB MANUAL

}
}
} }
for(i=1;i<k1-1;i++)
{
f3<<rcd[i].name<<"|"<<rcd[i].usn<<endl;
}
f1.close();
f2.close();
f3.close();
}
void k_merge()
{
char filname[7][20]={"11.txt","22.txt","33.txt","44.txt","111.txt","222.txt","1111.txt"};
int i,j=0;
for(i=0;i<8;i+=2)
merge_file(fname[i],fname[i+1],filname[j++]);
j=4;
for(i=0;i<4;i+=2)
merge_file(filname[i],filname[i+1],filname[j++]);
merge_file(filname[4],filname[5],filname[6]);
}
int main()
{
int i;
char name[20],usn[20];
n=0;
for(i=0;i<8;i++)
file[i].open(fname[i],ios::out);
cout<<"Enter number of records: ";
cin>>n;
cout<<"Enter Name and USN:\n";

Dept. of ISE Bangalore Institute of Technology Page 49


FS LAB MANUAL

for(i=0;i<n;i++)
{
cout<<"Student "<<i+1<<" : ";
cin>>rec[i].name>>rec[i].usn;
file[i%8]<<rec[i].name<<"|"<<rec[i].usn<<endl;
}
for(i=0;i<8;i++)
file[i].close();
k_merge();
fstream output("1111.txt",ios::in);
cout<<"\nThe sorted records are:\n";
cout<<"NAME\tUSN\n";
for(i=0;i<n;i++)
{
output.getline(name,20,'|');
output.getline(usn,20);
cout<<name<<"\t"<<usn<<endl;
}
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 50


FS LAB MANUAL

OUTPUT:-

Note: All 8 files are to be populated with the data and the result to be verified in all the
intermediate files.

Enter number of records: 8


Enter Name and USN:
Student 1 : BhimsenJoshi 100
Student 2 : Basavaraj 200
Student 3 : MallikarjunMansoor 300
Student 4 : Hariprasadchaurasia 400
Student 5 : ShivakumarSharma 500
Student 6 : ZakirHussain 600
Student 7 : Ravishankar 700
Student 8 : AnushkaRavishnkar 800

The sorted records are:


NAME USN
AnushkaRavishnkar 800
Basavaraj 200
BhimsenJoshi 100
Hariprasadchaurasia 400
MallikarjunMansoor 300
Ravishankar 700
ShivakumarSharma 500
ZakirHussain 600

Dept. of ISE Bangalore Institute of Technology Page 51


FS LAB MANUAL

Program 9:-
Write a C++ program to implement B-Tree for a given set of integers and its operations
insert ( ) and search ( ). Display the tree.

#include<iostream>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>

using namespace std;

class node
{
public:
int a[4];
node * next[4];
node * parent;
int size;
node();
};

node :: node()
{
for(int i = 0; i < 4; i++)
next[i] = NULL;
parent = NULL;
size = 0;
}

Dept. of ISE Bangalore Institute of Technology Page 52


FS LAB MANUAL

class btree
{
node * root;
public:
node* findLeaf(int key,int &level);
void updateKey(node *p,node *c,int newKey);
void search(int key);
void insert(int key);
void insertIntoNode(node *n,int key,node *address);
void promote(node *n,int key,node *address);
node* split(node *n);
void traverse(node *ptr);
btree();
};

void btree :: traverse(node *ptr)


{
if(ptr == NULL)
return;
int i;
for(i = 0; i < ptr->size; i++)
cout<<ptr->a[i]<<" ";
cout<<endl;
for(i = 0; i < ptr->size;i++)
traverse(ptr->next[i]);

btree :: btree()
{
root = NULL;
}

Dept. of ISE Bangalore Institute of Technology Page 53


FS LAB MANUAL

node* btree :: findLeaf(int key,int &level)


{
node *ptr = root;
node *prevptr = NULL;
level = 0;
int i;
while(ptr)
{
i = 0;
level++;
cout<<level<<endl;
while(i < ptr -> size-1 && key > ptr -> a[i])
i++;
prevptr = ptr;
ptr = ptr -> next[i];
}
return prevptr;
}

node* btree :: split(node *n)


{
int midpoint = (n -> size+1)/2;
int newsize = n->size - midpoint;
node *newptr = new node;
node *child;
newptr->parent = n -> parent;
int i;
for(i = 0; i < midpoint; i++)
{
newptr->a[i] = n->a[i];
newptr ->next[i] = n->next[i];

Dept. of ISE Bangalore Institute of Technology Page 54


FS LAB MANUAL

n->a[i] = n->a[i+midpoint];
n->next[i] = n->next[i+midpoint];
}
n->size = midpoint;
newptr -> size = newsize;
for( i = 0; i < n->size; i++)
{
child = n->next[i];
if(child!= NULL)
child -> parent = n;
}
for( i = 0; i < newptr -> size; i++)
{
child = newptr -> next[i];
if(child!= NULL)
child -> parent = newptr;
}
return newptr;
}

void btree :: updateKey(node *parent,node *child,int newkey)


{
if(parent == NULL)
return;
if(parent->size == 0)
return;
int oldkey = child->a[child->size-2];
while(parent)
{
for(int i = 0; i < parent->size;i++)
if(parent->a[i] == oldkey)
{

Dept. of ISE Bangalore Institute of Technology Page 55


FS LAB MANUAL

parent->a[i] = newkey;
parent->next[i] = child;
}
child=parent;
parent=parent->parent;
}
}
void btree :: insertIntoNode(node *n,int key,node *address)
{
int i;
if( n == NULL)
return;

for(i = 0; i < n->size; i++)


if(n->a[i] == key)
return;

i = n->size-1;
while(i >= 0 && n -> a[i] > key)
{
n->a[i+1] = n->a[i];
n->next[i+1] = n->next[i];
i--;
}
i++;
n->a[i] = key;
n->next[i] = address;
n->size++;
if( i == n->size-1)
updateKey(n->parent,n,key);
}

Dept. of ISE Bangalore Institute of Technology Page 56


FS LAB MANUAL

void btree :: promote(node *n,int key,node *address)


{
if( n == NULL)
return;

if(n -> size < 4)


{
insertIntoNode(n,key,address);
return;
}

if( n == root)
{
root = new node;

n->parent = root;
}
node *newptr = split(n);
node *t;
if(key < n->a[0])
t = newptr;
else
t = n;
insertIntoNode(t,key,address);
promote(n->parent,n->a[n->size-1],n);
promote(newptr->parent,newptr->a[newptr->size-1],newptr);

void btree :: insert(int key)


{
if( root == NULL)

Dept. of ISE Bangalore Institute of Technology Page 57


FS LAB MANUAL

{
root = new node;
root->a[root->size] = key;
root->size++;
return;
}
int level;
node *leaf = findLeaf(key,level);
int i;
for(i = 0; i < leaf->size; i++)
if(leaf -> a[i] == key)
{
cout<<"The Key to be inserted already exists"<<endl;
return;
}
promote(leaf,key,NULL);
cout<<"---------------\n";
traverse(root);
cout<<"----------------\n";
}

void btree :: search(int key)


{
if(root == NULL)
{ cout<<"The tree Does not exist"<<endl;
return;
}
int level;
node *leaf = findLeaf(key,level);
int flag = 0;
for(int i = 0; i < leaf ->size; i++)
if(leaf->a[i] == key)

Dept. of ISE Bangalore Institute of Technology Page 58


FS LAB MANUAL

{
flag = 1;
cout<<"The Key "<<key<<" Exists in the B-Tree at the level"<<level<<endl;
}
if(!flag)
cout<<"The Key Searched for was not found"<<endl;
}
int main()
{
btree b;
int choice = 1,key;
while(choice !=3)
{
cout<<"1.Insert a Key\n";
cout<<"2.Search a key\n";
cout<<"3.Exit\n";
cin>>choice;
switch(choice)

{
case 1: cout<<"Enter The Key to be inserted in B-Tree\n";
cin>>key;
b.insert(key);
break;
case 2: cout<<"Enter The key to be searched\n";
cin>>key;
b.search(key);
break;
}
}
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 59


FS LAB MANUAL

OUTPUT:-
1.Insert a Key
2.Search a key
3.Exit
1
Enter The Key to be inserted in B-Tree
10
1.Insert a Key
2.Search a key
3.Exit
1
Enter The Key to be inserted in B-Tree
20
1
---------------
10 20
----------------
1.Insert a Key
2.Search a key
3.Exit
1
Enter The Key to be inserted in B-Tree
30
1
---------------
10 20 30
----------------
1.Insert a Key
2.Search a key
3.Exit
1
Enter The Key to be inserted in B-Tree

Dept. of ISE Bangalore Institute of Technology Page 60


FS LAB MANUAL

40
1
---------------
10 20 30 40
----------------
1.Insert a Key
2.Search a key
3.Exit
1
Enter The Key to be inserted in B-Tree
50
1
---------------
20 50
10 20
30 40 50
----------------
1.Insert a Key
2.Search a key
3.Exit
1
Enter The Key to be inserted in B-Tree
60
1
2
---------------
20 60
10 20
30 40 50 60
1.Insert a Key
2.Search a key
3.Exit

Dept. of ISE Bangalore Institute of Technology Page 61


FS LAB MANUAL

1
Enter The Key to be inserted in B-Tree
70
1
2
---------------
20 40 70
10 20
30 40
50 60 70
----------------
1.Insert a Key
2.Search a key
3.Exit
2
Enter The key to be searched
60
1
2
The Key 60 Exists in the B-Tree at the level2
1.Insert a Key
2.Search a key
3.Exit
2
Enter The key to be searched
100
1
2
The Key Searched for was not found
1.Insert a Key
2.Search a key
3.Exit

Dept. of ISE Bangalore Institute of Technology Page 62


FS LAB MANUAL

Program 10:-
Write a C++ program to implement B+ tree for a given set of integers and its operations
insert ( ), and search ( ). Display the tree.

#include<iostream>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<string.h>

using namespace std;

class node
{
public:
int a[4];
node * next[4];
node * parent;
int size;
node();
};

class linkleaf
{
public:
node * data;
linkleaf *next;
linkleaf();
};

linkleaf :: linkleaf()
{

Dept. of ISE Bangalore Institute of Technology Page 63


FS LAB MANUAL

next = NULL;
}

node :: node()
{
for(int i = 0; i < 4; i++)
next[i] = NULL;
parent = NULL;
size = 0;
}

class btree
{
node * root;
linkleaf *head;
int flag;
public:
node* findLeaf(int key,int &level);
void updateKey(node *p,node *c,int newKey);
void search(int key);
void insert(int key);
void insertIntoNode(node *n,int key,node *address);
void promote(node *n,int key,node *address);
node* split(node *n);
void traverse(node *ptr);
void connectLeaf(node *n,node *newptr);
void traverseleaf();
btree();
};

void btree :: traverseleaf()


{

Dept. of ISE Bangalore Institute of Technology Page 64


FS LAB MANUAL

linkleaf * ptr = head;


int i;
while(ptr)
{
for(i = 0; i < ptr -> data -> size; i++)
cout<<ptr -> data -> a[i]<<" ";
cout<<endl;
ptr = ptr ->next;
}
}

void btree :: connectLeaf(node *n,node *newptr)


{
linkleaf *ptr = head,*prevptr = NULL,*p;
while(ptr)
{
if(ptr-> data == n)
break;
prevptr = ptr;
ptr = ptr ->next;
}
if( ptr == NULL)
{
cout<<"Unexpected Error!!";
exit(0);
}

if( ptr == head)


{
p = new linkleaf;
p -> next = head;
head = p;

Dept. of ISE Bangalore Institute of Technology Page 65


FS LAB MANUAL

p->data = newptr;
}
else
{
p = new linkleaf;
p-> next = ptr;
prevptr -> next = p;
p->data = newptr;
}

void btree :: traverse(node *ptr)


{
if(ptr == NULL)
return;
for(int i = 0; i < ptr->size; i++)
cout<<ptr->a[i]<<" ";
cout<<endl;
for(int i = 0; i < ptr->size;i++)
traverse(ptr->next[i]);

btree :: btree()
{
root = NULL;
head = NULL;
}

node* btree :: findLeaf(int key,int &level)


{

Dept. of ISE Bangalore Institute of Technology Page 66


FS LAB MANUAL

node *ptr = root;


node *prevptr = NULL;
level = 0;
int i;
while(ptr)
{
i = 0;
level++;
while(i < ptr -> size-1 && key > ptr -> a[i])
i++;
prevptr = ptr;
ptr = ptr -> next[i];
}
return prevptr;
}

node* btree :: split(node *n)


{
int midpoint = (n -> size+1)/2;
int newsize = n->size - midpoint;
node *newptr = new node;
node *child;
newptr->parent = n -> parent;
int i;
for(i = 0; i < midpoint; i++)
{
newptr->a[i] = n->a[i];
newptr ->next[i] = n->next[i];
n->a[i] = n->a[i+midpoint];
n->next[i] = n->next[i+midpoint];
}
n->size = midpoint;

Dept. of ISE Bangalore Institute of Technology Page 67


FS LAB MANUAL

newptr -> size = newsize;


for( i = 0; i < n->size; i++)
{
child = n->next[i];
if(child!= NULL)
child -> parent = n;
}
for( i = 0; i < newptr -> size; i++)
{
child = newptr -> next[i];
if(child!= NULL)
child -> parent = newptr;
}
return newptr;
}

void btree :: updateKey(node *parent,node *child,int newkey)


{
if( parent == NULL)
return;
if(parent->size == 0)
return;
int oldkey = child->a[child->size-2];
while(parent)
{
for(int i = 0; i < parent->size;i++)
if(parent->a[i] == oldkey)
{
parent->a[i] = newkey;
parent->next[i] = child;
}
child=parent;

Dept. of ISE Bangalore Institute of Technology Page 68


FS LAB MANUAL

parent=parent->parent;
}
}
void btree :: insertIntoNode(node *n,int key,node *address)
{
int i;
if( n == NULL)
return;

for(i = 0; i < n->size; i++)


if(n->a[i] == key)
return;

i = n->size-1;
while(i >= 0 && n -> a[i] > key)
{
n->a[i+1] = n->a[i];
n->next[i+1] = n->next[i];
i--;
}
i++;
n->a[i] = key;
n->next[i] = address;
n->size++;
if( i == n->size-1)
updateKey(n->parent,n,key);
}

void btree :: promote(node *n,int key,node *address)


{
if( n == NULL)
return;

Dept. of ISE Bangalore Institute of Technology Page 69


FS LAB MANUAL

if(n -> size < 4)


{
insertIntoNode(n,key,address);
return;
}

if( n == root)
{
root = new node;
n->parent = root;
}
node *newptr = split(n);
node *t;
if(key < n->a[0])
t = newptr;
else
t = n;
insertIntoNode(t,key,address);
if(!flag)
{ connectLeaf(n,newptr);flag = 1;}
promote(n->parent,n->a[n->size-1],n);
promote(newptr->parent,newptr->a[newptr->size-1],newptr);
}

void btree :: insert(int key)


{
flag = 0;
if( root == NULL)
{
root = new node;
root->a[root->size] = key;

Dept. of ISE Bangalore Institute of Technology Page 70


FS LAB MANUAL

root->size++;
head = new linkleaf;
head -> data = root;
return;
}
int level;
node *leaf = findLeaf(key,level);
int i;
for(i = 0; i < leaf->size; i++)
if(leaf -> a[i] == key)
{
cout<<"The Key to be inserted already exists"<<endl;
return;
}
promote(leaf,key,NULL);
cout<<"---------------\n";
traverse(root);
cout<<"----------------\n";
}
void btree :: search(int key)
{
if(root == NULL)
{ cout<<"The tree Does not exist"<<endl;
return;
}
int level;
node *leaf = findLeaf(key,level);
int flag = 0;
for(int i = 0; i < leaf ->size; i++)
if(leaf->a[i] == key)
{
flag = 1;

Dept. of ISE Bangalore Institute of Technology Page 71


FS LAB MANUAL

cout<<"The Key "<<key<<" Exists in the B-Tree at the level"<<level<<endl;


}
if(!flag)
cout<<"The Key Searched for was not found"<<endl;
}
int main()
{
btree b;
int choice = 1,key;
while(choice <=3)
{
cout<<"1.Insert a Key\n";
cout<<"2.Search a key\n";
cout<<"3.Traverse Leaf\n";
cout<<"4.Exit\n";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter The Key to be inserted in B-Tree\n";
cin>>key;
b.insert(key);
break;
case 2: cout<<"Enter The key to be searched\n";
cin>>key;
b.search(key);
break;
case 3: b. traverseleaf();
break;
}
}
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 72


FS LAB MANUAL

OUTPUT:
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
1
Enter The Key to be inserted in B-Tree
10
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
1
Enter The Key to be inserted in B-Tree
20
---------------
10 20
----------------
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
1
Enter The Key to be inserted in B-Tree
30
---------------
10 20 30
----------------
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit

Dept. of ISE Bangalore Institute of Technology Page 73


FS LAB MANUAL

1
Enter The Key to be inserted in B-Tree
40
---------------
10 20 30 40
----------------
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
1
Enter The Key to be inserted in B-Tree
50
---------------
20 50
10 20
30 40 50
----------------
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
1
Enter The Key to be inserted in B-Tree
60
---------------
20 60
10 20
30 40 50 60
----------------
1.Insert a Key
2.Search a key

Dept. of ISE Bangalore Institute of Technology Page 74


FS LAB MANUAL

3.Traverse Leaf
4.Exit
1
Enter The Key to be inserted in B-Tree
70
---------------
20 40 70
10 20
30 40
50 60 70
----------------
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
3
10 20
30 40
50 60 70
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit

Dept. of ISE Bangalore Institute of Technology Page 75


FS LAB MANUAL

PROGRAM 11:-
C++ program to store and retrieve student data from file using hashing. Use any collision
resolution technique.

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>

using namespace std;

class node
{
public:
char name[15],usn[15];
node* next;
};

node*h[29];

void insert()
{
char name[15],usn[15],buf[50];

fstream file("student.txt",ios::app|ios::out);
cout<<"enter name and usn";
cin>>name>>usn;
strcpy(buf,name);
strcat(buf,"|");
strcat(buf,usn);

Dept. of ISE Bangalore Institute of Technology Page 76


FS LAB MANUAL

strcat(buf,"\n");
file<<buf;
file.close();
}
void hash_ins(char name[],char usn[],int hash_key)
{
node*p,*prev,*cur;
p=new node;
strcpy(p->name,name);
strcpy(p->usn,usn);
p->next=NULL;
prev=NULL;
cur=h[hash_key];
if(cur==NULL)
{
h[hash_key]=p;
return;
}
while(cur!=NULL)
{
prev=cur;
cur=cur->next;
}
prev->next=p;
}
void display()
{
node*cur;
fstream file("student.txt",ios::in);
char name[15],usn[15];
int j,count;
int i;

Dept. of ISE Bangalore Institute of Technology Page 77


FS LAB MANUAL

for(i=0;i<29;i++)
{
h[i]=NULL;

}
while(!file.eof())
{
file.getline(name,15,'|');
file.getline(usn,15);
count=0;
for(j=0;j<strlen(usn);j++)
count+=usn[j];
count=count%29;
hash_ins(name,usn,count);
}

for(i=0;i<29;i++)
{
cur=h[i];
if(cur!=NULL)
{
cout<<i<<"---";
while(cur!=NULL)
{
cout<<cur->name<<"|"<<cur->usn<<"\t";
cur=cur->next;
}
cout<<"\n";
}
}
file.close();
}

Dept. of ISE Bangalore Institute of Technology Page 78


FS LAB MANUAL

void retrieve()
{
fstream file("student.txt",ios::in);
char name[15],usn[15];
int j,count;
node*cur;
for(int i=0;i<29;i++)
{
h[i]=NULL;
}
while(!file.eof())
{
file.getline(name,15,'|');
file.getline(usn,15);
count=0;
for(j=0;j<strlen(usn);j++)
count+=usn[j];
count=count%29;
hash_ins(name,usn,count);
}

cout<<"enter usn\n";
cin>>usn;
count=0;
for(j=0;j<strlen(usn);j++)
count+=usn[j];
count=count%29;
cur=h[count];
if(cur==NULL)
{
cout<<"record not found\n";
return;

Dept. of ISE Bangalore Institute of Technology Page 79


FS LAB MANUAL

}
do
{
if(strcmp(cur->usn,usn)==0)
{
cout<<"record found\nname:"<<cur->name<<"\nUSN:"<<cur->usn<<endl;
return;
}
else
cur=cur->next;
}while(cur!=NULL);
}
int main()
{
int ch;
do
{
cout<<"1:insert 2:retrive 3:display the table 4:exit\n";
cout<<"enter your choice";
cin>>ch;
switch(ch)
{
case 1:insert();break;
case 2:retrieve();break;
case 3:display();break;
case 4:break;
}
}while(ch!=4);
return 0;
}

Dept. of ISE Bangalore Institute of Technology Page 80


FS LAB MANUAL

OUTPUT:-

1:insert 2:retrive 3:display the table 4:exit


enter your choice 1
enter name and usn ROHAN
25
1:insert 2:retrive 3:display the table 4:exit
enter your choice1
enter name and usn C.V.RAMAN 35
1:insert 2:retrive 3:display the table 4:exit
enter your choice1
enter name and usn VISVESHWARAYA 52
1:insert 2:retrive 3:display the table 4:exit
enter your choice 1
enter name and usn U.R.RAO 53
1:insert 2:retrive 3:display the table 4:exit
enter your choice3
0---|
16---ROHAN|25 VISVESHWARAYA|52
17---C.V.RAMAN|35 U.R.RAO|53
1:insert 2:retrive 3:display the table 4:exit
enter your choice2
enter usn
53
record found
name:U.R.RAO
USN:53
1:insert 2:retrive 3:display the table 4:exit
enter your choice

Dept. of ISE Bangalore Institute of Technology Page 81


FS LAB MANUAL

PROGRAM 12:-
Write a C++ program to reclaim the free space resulting from the deletion of records using
linked lists.

#include<iostream>
#include<fstream>
#include<ostream>
#include<string.h>
#include<stdio.h>
using namespace std;

class node
{
public:
node* next;
int offset;
node()
{
next=NULL;
offset=0;
}
};
node*first;
char p[20][20];
int nor;
char buff[90];
class student
{
public:
char name[20],usn[20],branch[20],sem[20];
void del();
void insert();

Dept. of ISE Bangalore Institute of Technology Page 82


FS LAB MANUAL

void display();
};
void student::insert()
{ int i;
fstream f1("record.txt",ios::app|ios::out);
fstream f2("avail.txt",ios::out|ios::app);
node*cur=first;
cout<<"enter the usn,name,branch,sem\n";
cin>>usn>>name>>branch>>sem;
for(i=0;i<90;i++)
buff[i]='\0';
strcpy(buff,usn);
strcat(buff,"|");
strcat(buff,name);
strcat(buff,"|");
strcat(buff,branch);
strcat(buff,"|");
strcat(buff,sem);
strcat(buff,"|");
if(cur==NULL)
{
f1.seekp(0,ios::end);
f1.write(buff,sizeof(buff));
strcpy(p[nor],usn);
nor++;
}
else
{
i=first->offset;
f1.seekp(i,ios::beg);
f1.write(buff,sizeof(buff));
node *cur=first;

Dept. of ISE Bangalore Institute of Technology Page 83


FS LAB MANUAL

first=first->next;
delete(cur);
strcpy(p[i/sizeof(buff)],usn);
}
cur=first;
while(cur)
{
f2<<cur->offset<<endl;
cur=cur->next;
}
f1.close();
f2.close();
}
void student::del()
{
fstream f1("record.txt",ios::in|ios::out);
fstream f2("avail.txt",ios::out);
cout<<"enter the usn to deleted\n";
char key[20];
int flag=-1,i;
cin>>key;
node *a;
for (i=0;i<nor;i++)
if(strcmp(p[i],key)==0)
{
flag=i;
}
if(flag!=-1)
{
cout<<"record deleted\n";
a=new node;
f1.seekp(flag*sizeof(buff),ios::beg);

Dept. of ISE Bangalore Institute of Technology Page 84


FS LAB MANUAL

a->offset=f1.tellp();
for(i=0;i<(int)strlen(key);i++)
f1<<"*";
a->next=first;
first=a;
}
else
{
cout<<"not found";
}
node *cur=first;
while(cur)
{
f2<<cur->offset<<endl;
cur=cur->next;
}
f1.close();
f2.close();
}
void student::display()
{
fstream f1("record.txt",ios::in|ios::out);
fstream f2("avail.txt",ios::out);
int j=0;
while(!f1.eof())
{
f1.seekg(j,ios::beg);
f1.getline(usn,20,'|');
f1.getline(name,20,'|');
f1.getline(branch,20,'|');
f1.getline(sem,20,'|');
cout<<"usn\tname\tbranch\tsem\n";

Dept. of ISE Bangalore Institute of Technology Page 85


FS LAB MANUAL

if(usn[0]!='*')
{
cout<<usn<<"\t"<<name<<"\t"<<branch<<"\t"<<sem<<endl;
}
j=j+sizeof(buff);
}
f1.close();
f2.close();
}
int main()
{
student s;
int ch;
do
{
cout<<"1:insert 2:delete 3:display 4:exit\n";
cout<<"enter your choice";
cin>>ch;
switch(ch)
{
case 1:s.insert();
break;
case 2:s.del();
break;
case 3:s.display();
break;
case 4:remove("record.txt");
remove("avail.txt");
break;
}
}while(ch!=4);
}

Dept. of ISE Bangalore Institute of Technology Page 86


FS LAB MANUAL

OUTPUT:-

Note: Verify the record file and avail file before and after insertion deletion of records
1:insert 2:delete 3:display 4:exit
enter your choice 1
enter the usn,name,branch,sem
50 Shankar ISE 6
1:insert 2:delete 3:display 4:exit
enter your choice1
enter the usn,name,branch,sem
75 RAM CSE 5
1:insert 2:delete 3:display 4:exit
enter your choice1
enter the usn,name,branch,sem
125 KRISHNA MECH 7
1:insert 2:delete 3:display 4:exit
enter your choice3
usn name branch sem
50 Shankar ISE 6
usn name branch sem
75 RAM CSE 5
usn name branch sem
125 KRISHNA MECH 7
usn name branch sem

1:insert 2:delete 3:display 4:exit


enter your choice2
enter the usn to deleted
75
record deleted
1:insert 2:delete 3:display 4:exit
enter your choice2

Dept. of ISE Bangalore Institute of Technology Page 87


FS LAB MANUAL

enter the usn to deleted


50
record deleted
1:insert 2:delete 3:display 4:exit
enter your choice3
usn name branch sem
125 KRISHNA MECH 7

1:insert 2:delete 3:display 4:exit


enter your choice1
enter the usn,name,branch,sem
356 NAVEEN ELE 8
1:insert 2:delete 3:display 4:exit
enter your choice3
usn name branch sem
356 NAVEEN ELE 8
125 KRISHNA MECH 7

Dept. of ISE Bangalore Institute of Technology Page 88

You might also like