Professional Documents
Culture Documents
Fs Lab
Fs Lab
Fs Lab
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.
2. Write a C++ program to read and write and student objects with fixed-length records
and the fields delimited by |.implement pack(),unpack(),modify() and search() methods.
3. Write a C++ program to read and write and student objects with variable-length
records using any suitable record structure. Implement pack(),unpack(),modify() and
search() methods
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.
5. Write a C++ program to implement simple index on primary key for a file of student
objects. Implement add(),search(),delete() using the index.
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.
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.
8. Write a C++ program to read k Lists of names and merge them using kway merge
algorithm with k = 8.
9. Write a C++ program to implement B-Tree for a given set of integers and its operations
insert ( ) and search ( ). Display the tree.
10. Write a C++ program to implement B+ tree for a given set of integers and its
operations insert ( ), and search ( ). Display the tree.
11. Write a C++ program to store and retrieve student data from file using hashing. Use
any collision resolution technique
12. Write a C++ program to reclaim the free space resulting from the deletion of records
using linked lists.
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.
Filename : fs1.cpp
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
void swap(char &x,char &y)
{
char temp=x;
x=y;
y=temp;
}
void reverse(char *name)
{
int count=strlen(name);
for(int i=0,j=count-1;i<count/2;++i,--j)
swap(name[i],name[j]);
}
void main(int argc,char *argv[])
{
if(argc==1)
{
char name[10];
do
{
cin>>name;
reverse(name);
cout<<name<<endl;
if(cin.eof())
break;
}while(1);
}
else if(argc>2)
{
char name[20];
fstream fd1,fd2;
fd1.open(argv[1],ios::in);
fd2.open(argv[2],ios::out|ios::app|ios::trunc);
do
{
fd1>>name;
reverse(name);
cout<<name;
fd2<<name<<endl;
if(fd1.eof())
break;
}while(1);
fd1.close();
fd2.close();
}
cout<<"usage:"<<argv[0]<<"filename1 filename2";
}
Output:
C:\TC\BIN>FS1.exe
networks
skrowten
file
elif
system
metsys
C:\TC\BIN>edit input
networks
files
system
compiler
graphics
C:\TC\BIN>FS1.exe input output
skrowtenselifmetsysrelipmocscihparg
C:\TC\BIN>type output
skrowten
selif
metsys
relipmoc
scihparg
C:\TC\BIN>FS1.EXE INPUT
usage:FS1.EXE filename1 filename2
2. Write a C++ program to read and write and student objects with fixedlength
records
and
the
fields
delimited
by
|.implement
pack(),unpack(),modify() and search() methods.
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[45];
};
student s2[100];
void writeRecord()
//Function to add record to file
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout<<"cannot open the file in output mode";
getch();
exit(0);
}
cout<<"\n Enter the student name = "; cin>>s.name;
cout<<"\n Enter the usn = "; cin>>s.usn;
cout<<"\n Enter the age = "; cin>>s.age;
cout<<"\n Enter the sem = "; cin>>s.sem;
cout<<"\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn);
strcat(s.buffer,"|");
strcat(s.buffer, s.age);
strcat(s.buffer,"|");
strcat(s.buffer, s.sem);
strcat(s.buffer,"|");
strcat(s.buffer, s.branch);
int count=strlen(s.buffer);
for(int k=0;k<45-count;k++)
strcat(s.buffer,"!");
strcat(s.buffer,"\n");
app<<s.buffer;
//writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the record's usn you want to search =
student s;
"; cin>>usn;
in.getline(extra,45,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t
%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
in.getline(extra,45,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<"\nThe old values of the record with usn "<<usn<<" are ";
cout<<"\nname
= "<< s1[j].name;
cout<<"\nusn
= "<< s1[j].usn;
cout<<"\nage
= "<< s1[j].age;
cout<<"\nsem
= "<< s1[j].sem;
cout<<"\nbranch = "<< s1[j].branch;
cout<<"\nEnter the new values \n";
cout<<"\nname = "; cin>>s1[j].name;
cout<<"\nusn
= "; cin>>s1[j].usn;
cout<<"\nage
= "; cin>>s1[j].age;
cout<<"\nsem
= "; cin>>s1[j].sem;
cout<<"\nbranch= "; cin>>s1[j].branch;
break;
}
}
if(j==i)
{
cout<<"\n Record with usn "<<usn<<" is not present";
getch();
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout<<"\nUnable to open file in output mode";
getch();
return;
}
for(j=0;j<i;j++)
{
strcpy(buffer,s1[j].name);
strcat(buffer,"|");
strcat(buffer,s1[j].usn);
strcat(buffer,"|");
strcat(buffer,s1[j].age);
strcat(buffer,"|");
strcat(buffer,s1[j].sem);
strcat(buffer,"|");
strcat(buffer,s1[j].branch);
int count=strlen(buffer);
for(int k=0;k<45-count;k++)
strcat(buffer,"!");
strcat(buffer,"\n");
out1<<buffer;
}
out1.close();
void main()
{
fstream in;
fstream out;
int ch;
out.open("student.txt",ios::out);
if(!in)
{
cout<<"\n\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
clrscr();
cout<<"\n 1: write to file\n 2:display the file"
<<"\n 3:modify the file\n 4:search\n 5.exit";
cout<<"\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 5: exit(0);
default: cout<<"\nInvalid input....";break;
}
Output :
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:1
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:1
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:2
Name
Usn
ajay
1vk07is002
rahul
1vk07cs045
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Age
20
20
Sem
6
6
Branch
ise!!!!!!!!!!!
cse!!!!!!!!!!!
1vk07cs045
cse!!!!!!!!!!!
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:5
C:\TC|BIN>type student.txt
ajay|1vk07is002|20|6|ise!!!!!!!!!!!
rahul|1vk07cs045|20|6|cse!!!!!!!!!!!
3. Write a C++ program to read and write and student objects with
variable-length records using any suitable record structure. Implement
pack(),unpack(),modify() and search() methods
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
student s2[100];
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout<<"cannot open the file in output mode";
getch();
exit(0);
}
cout<<"\n Enter the student name = "; cin>>s.name;
cout<<"\n Enter the usn = "; cin>>s.usn;
cout<<"\n Enter the age = "; cin>>s.age;
cout<<"\n Enter the sem = "; cin>>s.sem;
cout<<"\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name);
strcat(s.buffer,"|");
strcat(s.buffer,
strcat(s.buffer,
strcat(s.buffer,
strcat(s.buffer,
s.usn);
strcat(s.buffer,"|");
s.age);
strcat(s.buffer,"|");
s.sem);
strcat(s.buffer,"|");
s.branch);strcat(s.buffer,"\n");
app<<s.buffer;
app.close();
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the record's usn you want to search =
student s;
"; cin>>usn;
if(!in)
{
cout<<"\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t
%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<"\nThe old values of the record with usn "<<usn<<" are ";
cout<<"\nname
= "<< s1[j].name;
cout<<"\nusn
cout<<"\nage
cout<<"\nsem
cout<<"\nbranch
=
=
=
=
"<<
"<<
"<<
"<<
s1[j].usn;
s1[j].age;
s1[j].sem;
s1[j].branch;
clrscr();
cout<<"\n 1: write to file\n 2:display the file"
<<"\n 3:modify the file\n 4:search\n 5:exit";
cout<<"\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 5: exit(0);
default: cout<<"\nInvalid input....";break;
}
Output :
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:1
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:1
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:2
Name
Usn
ajay
1vk07is002
Age
20
Sem
6
Branch
ise
rahul
1vk07cs045
20
cse
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:4
Enter the record's usn you want to search =
Record found
rahul
1vk07cs045 20
6
1vk07cs045
cse
1:write to file
2:display the file
3:modify the file
4:search
5.exit
Enter the choice:5
C:\TC|BIN>type student.txt
ajay|1vk07is002|20|6|ise
rahul|1vk07cs045|20|6|cse
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<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
void search()
{
char usn[15];
int i=0;
student s1[100];
cout<<"\nEnter the usn to be searched: ";
cin>>usn;
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\ncannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\tUsn\tAge\tSem\tBranch\n");
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i].branch,15,'\n');
i++;
}
for(int j=0; j<i-1; j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
printf("\n Record found");
printf("\n%s\t%s\t%s\t%s\t
%s",s1[j].name,s1[j].usn,s1[j].age,s1[j].sem,s1[j].branch);
return;
}
}
cout<<"\nRecord not found";
return;
}
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app);
if(!app)
{
cout<<"cannot open the file in output mode";
getch();
exit(0);
}
cout<<"\nEnter the student name: "; cin>>s.name;
cout<<"\nEnter the usn: "; cin>>s.usn;
cout<<"\nEnter the age: "; cin>>s.age;
cout<<"\nEnter the sem: "; cin>>s.sem;
cout<<"\nEnter the branch: "; cin>>s.branch;
strcpy(s.buffer,s.name); strcat(s.buffer,"|");
strcat(s.buffer,s.usn); strcat(s.buffer,"|");
strcat(s.buffer,s.age); strcat(s.buffer,"|");
strcat(s.buffer,s.sem); strcat(s.buffer,"|");
strcat(s.buffer,s.branch); strcat(s.buffer,"\n");
app<<s.buffer;
app.close();
}
void main()
{
clrscr();
int ch;
fstream out;
out.open("student.txt",ios::out);
if(!out)
{
cout<<"\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
cout<<"\n1:Insert\n2:Search\n3:exit"
<<"\nEnter the choice = "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: search();break;
case 3: exit(0);
default: cout<<"\nInvalid option";
}
}
Output :
1:Insert
2.Search
3.exit
Enter the choice:1
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
1:Insert
2.Search
3.exit
Enter the choice:1
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
1:Insert
2.Search
3.exit
Enter the choice:2
int i;
fstream file1, file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
file2.open("record.txt",ios::in);
for(i=0;i<no;i++) //Unpack records
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
//Check for the record's existence
{
if(strcmp(rec[i].usn,usno)==0)
flag=i;
}
if(flag==-1)
//Record not found
{
cout<<"Error !\n";
return;
}
if(flag==(no-1))
//Delete found record
{
no--;
cout<<"Deleted !\n";
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout<<"\nDeleted !\n";
file2.close();
file1.open("index.txt",ios::out); //Open index and record files
file2.open("record.txt",ios::out); //After deletion
for(i=0;i<no;i++)
//Pack index n record data onto files
{
file1<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"<
<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
file1.close();
file2.close();
return;
}
int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout<<"File creation Error! \n";
exit(0);
}
for(;;)
{
cout<<"\n1: Add Record"
<<"\n2: Search Record"
<<"\n3: Delete Record"
<<"\n4: Display Record"
<<"\n5: Exit"
<<"\n\n Enter u'r choice :";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the no. of students : "; cin>>no;
cout<<"Enter the details:\n";
for(i=0;i<no;i++) //Pack data onto the index and record files
{
//USN is the indexed data
cout<<"\nName: "; cin>>rec[i].name;
cout<<"Age: ";
cin>>rec[i].age;
cout<<"USN: ";
cin>>rec[i].usn;
cout<<"Sem: ";
cin>>rec[i].sem;
cout<<"Branch: "; cin>>rec[i].branch;
file1<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"<<rec[i
].sem<<"|"<<rec[i].branch<<"\n";
}
file1.close();
file2.close();
break;
case 2: cout<<"Enter USN whose record is to be displayed: ";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout<<"\nError !\n";
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,'|');
//Unpack index file and
file1.getline(st_no,4,'\n'); //look for a match in the USN
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details();
//Retrieve details if index found
flag1=1;
}
}
if(!flag1)
cout<<"Record search failed!\n";
file1.close();
break;
case 3: cout<<"Enter USN whose record is to be deleted: ";
cin>>st_usn;
file1.open("index.txt", ios::in);
if(!file1)
{
cout<<"Error! \n";
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,'|'); //Search index file and
file1.getline(st_no,4,'\n');
//call del if index found
if(strcmp(st_usn, rt_usn)==0)
{
delete_record(rt_usn);
flag=1;
}
}
if(!flag)
cout<<"Deletion Failed\n";
file1.close();
break;
case 4: for(i=0;i<no;i++)
{
cout<<"\n\n USN: "<<rec[i].usn
<<"\n Name: "<<rec[i].name
<<"\n Age: "<<rec[i].age
<<"\n Sem: "<<rec[i].sem
<<"\n Branch: "<<rec[i].branch;
}
break;
case 5:exit(0);
default: cout<<"Invalid choice";
exit(0);
break;
}
}
Output :
1:
2:
3:
4:
5:
Add Record
Search Record
Delete Record
Display Record
Exit
Add Record
Search Record
Delete Record
Display Record
Exit
Add Record
Search Record
Delete Record
Display Record
Exit
Add Record
Search Record
Delete Record
Display Record
Exit
Name: ajay
Age: 20
USN: 1vk07is002
Sem: 6
Branch: ise
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.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
//using namespace std;
class record
{
public: char age[5];
char usn[20],name[20],branch[5]; char sem[2];
}rec[20],found[20];
char st_no[5],rt_name[20];
int no;
void sort_records()
{
int i,j;
record temp;
for(i=0;i<no-1;i++)
for(j=0;j<no-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;
}
}
void create_indexfile()
{
fstream index,index2;
int i;
index.open("secindex.txt",ios::out);
index2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
index<<rec[i].name<<"|"
<<rec[i].usn<<"|"<<i<<"\n";
index2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"
<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
}
void retrieve_record(char* index)
{
fstream file;
int i;
char ind[2],usn[20],name[20],age[3],sem[3],branch[10];
file.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file.getline(ind,4,'|');
file.getline(usn,20,'|');
file.getline(name,20,'|');
file.getline(age,4,'|');
file.getline(sem,4,'|');
file.getline(branch,5,'\n');
if(strcmp(index,ind) == 0)
cout<<"\nUSN: "<<usn
<<"\nName: "<<name
<<"\nAge: "<<age
<<"\nSem: "<<sem
<<"\nBranch: "<<branch;
}
file.close();
return;
void retrieve_details()
{
int k=0,i;
char name[20],usn[20],ind[2];
char chusn[20];
char index[20][20];
fstream file;
file.open("secindex.txt",ios::in);
for(i=0;i<no;i++)
{
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(ind,4,'\n');
if(strcmp(name,rt_name) == 0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
retrieve_record(index[0]);
return;
}
else
{
cout<<"Please choose the candidate's USN: \n";
for(i=0;i<k;i++)
cout<<"Name: "<<found[i].name<<" USN: "<<found[i].usn<<endl;
}
cin>>chusn;
for(i=0;i<k;i++)
{
if(strcmp(chusn,found[i].usn) == 0)
{
retrieve_record(index[i]);
return;
}
}
cout<<"Invalid Entry! \n";
return;
}
void delete_record(char indx[])
{
int i;
fstream file1,file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
char index[20][20];
file2.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file2.getline(ind,4,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,8,'\n');
strcpy(index[i],ind);
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(index[i],indx) == 0)
flag=i;
}
if(flag==-1)
{
cout<<"Error!\n";
return;
}
if(flag==(no-1))
{
no--;
cout<<"Deleted!\n";
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout<<"Deleted!\n";
file2.close();
file1.open("secindex.txt",ios::out);
file2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
file1<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"
<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
file1.close();
file2.close();
return;
}
}
cout<<"Invalid Entry!\n";
return;
}
int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_name[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout<<"File creation Error!\n";
exit(0);
}
for(;;)
{
cout<<"\nl:Add Record\n2:Search Record"
<<"\n3:Delete Record\n4:Display Record\n5:Exit"
<<"\n\nEnter u'r choice :";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the no. of students : "; cin>>no;
cout<<"Enter the details :\n";
for(i=0;i<no;i++)
{
cout<<"\nName : "; cin>>rec[i].name;
cout<<"Age : ";
cin>>rec[i].age;
cout<<"USN : ";
cin>>rec[i].usn;
cout<<"Sem : ";
cin>>rec[i].sem;
cout<<"Branch : "; cin>>rec[i].branch;
}
sort_records();
create_indexfile();
file1.close ();
file2.close();
break;
case 2: cout<<"Enter name of the student whose record is to be
displayed\n";
cin>>st_name;
file1.open("secindex.txt",ios::in);
if(!file1)
{
cout<<"Error !\n";
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_name,20,'|');
file1.getline(rt_usn,20,'|');
file1.getline(st_no,4,'\n');
if(strcmp(st_name,rt_name)==0)
{
retrieve_details();
flag1=1;
goto one;
}
}
one: if(!flag1)
cout<<"Record search failed !\n";
file1.close();
break;
case 3: cout<<"Enter name of student whose record is to be
deleted\n";
cin>>st_name;
file1.open("secindex.txt",ios::in);
if(!file1)
{
cout<<"Error !\n";
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_name,20,'|');
file1.getline(rt_usn,20,'|');
file1.getline(ind,4,'\n');
if(strcmp(st_name,rt_name) == 0)
{
delete_index(rt_name);
flag=1;
}
}
if(!flag)
cout<<"Deletion failed !\n";
file1.close();
break;
case 4: for(i=0;i<no;i++)
{
cout<<"\n\nUSN : "<<rec[i].usn
<<"\nName: "<<rec[i].name
<<"\nAge : "<<rec[i].age
<<"\nSem : "<<rec[i].sem
<<"\nBranch : "<<rec[i].branch<<"\n";
}
break;
case 5:exit(0);
default: cout<<"Invalid option !\n";
exit(0);
break;
}
}
Output :
1:
2:
3:
4:
5:
Add Record
Search Record
Delete Record
Display Record
Exit
Add Record
Search Record
Delete Record
Display Record
Exit
Add Record
Search Record
Delete Record
Display Record
Exit
Deleted !
1:
2:
3:
4:
5:
Add Record
Search Record
Delete Record
Display Record
Exit
for(i=0;i<n;i++)
{
cin>>name;
out2<<name;
out2<<'\n';
}
out1.close();
out2.close();
}
void main()
{
char list1[100][20], list2[100][20];
int i,j,m,n;
clrscr();
fstream out1,out2,out3;
writeLists();
out1.open("file1.txt",ios::in);
out2.open("file2.txt",ios::in);
out3.open("file3.txt",ios::out);
if( (!out3) || (!out1) || (!out2) )
{
printf("Unable to open one of the file");
getch();
exit(0);
}
clrscr();
m=0;
n=0;
while(!out1.eof())
{
out1.getline(list1[m],20,'\n');
cout<<list1[m];
m++;
}
while(!out2.eof())
{
out2.getline(list2[n],20,'\n');
cout<<list2[n];
n++;
}
m--;
n--;
i=0;
j=0;
cout<<"\nElements common to both files are: \n ";
while(i<m && j<n)
{
if( strcmp(list1[i],list2[j]) == 0 )
{
out3<<list1[i];
cout<<list1[i]<<"\n";
out3<<'\n';
i++;
j++;
}
else if( strcmp(list1[i],list2[j]) < 0 )
i++;
else
j++;
}
getch();
}
Output :
Enter no. of names you want to enter in file1 : 3
Enter the names in ascending order
cse
ise
tc
Enter no. of names you want to enter in file1 : 2
Enter the names in ascending order
ec
ise
cseisetcecise
Elements common to both files are:
ise
8. Write a C++ program to read k Lists of names and merge them using
kway merge algorithm with k = 8.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
// Record specification
class record
{
public: char name[20];
char usn[20];
}rec[20];
int no;
fstream file[8];
//The first 8 files
char
fname[8][8]
{"l.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
return;
void main()
{
int i;
clrscr();
cout<<"Enter the no. of records : ";
cin>>no;
cout<<"\nEnter the details : \n";
for(i=0;i<8;i++) //Create 8 files to store the split data
file[i].open(fname[i],ios::out);
for(i=0;i<no;i++) //Split and pack data onto the files
{
cout<<"Name :"; cin>>rec[i].name;
cout<<"USN : ";cin>>rec[i].usn;
file[i%8]<<rec[i].name<<'|'<<rec[i].usn<<"\n";
}
for(i=0;i<8;i++)
file[i].close();
kwaymerge(); //Merge
fstream result;
result.open("lll.txt",ios::in);
cout<<"\nSorted Records : \n";
char name[20],usn[20];
for(i=0;i<no;i++) //Unpack the sorted records and dispL
{
result.getline(name,20,'|');
result.getline(usn,20,'\n');
cout<<"\nName
: "<<name<<"\nUSN : "<<usn<<"\n";
}
getch();
}
Output :
Enter the no. of records :4
Enter the details :
Name :rahul
USN :25
Name :laxmi
USN :16
Name :ajay
USN :2
Name :deepak
USN :8
Sorted Records :
Name :ajay
USN :2
Name :deepak
USN :8
Name :laxmi
USN :16
Name :rahul
USN :25
traverse(ptr->next[i]);
}
btree :: btree()
{
root = NULL;
}
node* btree :: findLeaf(int key,int &level)
{
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;
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];
for(int i = 0; i < parent->size;i++)
if(parent->a[i] == oldkey)
{
parent->a[i] = newkey;
parent->next[i] = child;
}
}
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;
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);
}
the
}
return 0;
Output :
1.Insert a Key
2.Search a key
3.Exit
Enter ur choice :1
Enter The Key to be inserted in B-Tree
100
1.Insert a Key
2.Search a key
3.Exit
Enter ur choice :1
Enter The Key to be inserted in B-Tree
50
----------50 100
----------1.Insert a Key
2.Search a key
3.Exit
Enter ur choice :1
Enter The Key to be inserted in B-Tree
75
------------50 75 100
------------1.Insert a Key
2.Search a key
3.Exit
Enter ur choice :1
Enter The Key to be inserted in B-Tree
200
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()
{
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;
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 j = 0; j < ptr->size; j++)
traverse(ptr->next[j]);
}
btree :: btree()
{
root = NULL;
head = NULL;
}
node* btree :: findLeaf(int key,int &level)
{
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;
newptr -> size = newsize;
for( i = 0; i < n->size; i++)
{
child = n->next[i];
if(child!= NULL)
child -> parent = n;
}
}
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;
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;
cout<<"The Key "<<key<<"
level"<<level<<endl;
Exists
in
the
B-Tree
at
the
}
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";
cout<<\n enter ur choice :;
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;
}
Output:
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
enter ur choice : 1
Enter The Key to be inserted in B-Tree
100
1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
enter ur choice : 1
Enter The Key to be inserted in B-Tree
50
-------50 100
-------1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
enter ur choice : 1
Enter The Key to be inserted in B-Tree
200
---------50 100 200
---------1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
enter ur choice : 1
Enter The Key to be inserted in B-Tree
75
--------------50 75 100 200
--------------1.Insert a Key
2.Search a key
3.Traverse Leaf
4.Exit
enter ur choice : 2
Enter The key to be searched
300
The Key Searched for was not found
11. Write a C++ program to store and retrieve student data from file
using hashing. Use any collision resolution technique
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
//Record specification
class node
{
public: char name[15],usn[15];
node *link;
};
node *h[29];
- 29
void insert()
{
char name[15], usn[15], buffer[50];
fstream out;
out.open("student.txt",ios::app); //opening student.txt in append
mode
if(!out)
{
cout<<"\nUnable to open the file in append mode";
getch();
return;
}
cout<<"\nEnter the name = "; cin>>name;
cout<<"\nEnter the usn = "; cin>>usn;
strcpy(buffer,name); //Packing the record onto the file using '|' as
a delimiter
strcat(buffer,"|");
strcat(buffer,usn);
strcat(buffer,"\n"); // \n delimiter for the record
out<<buffer;
// appending the packed record onto the file
out.close();
}
//Insert record into the hash table
void hash_insert(char name1[], char usn1[], int hash_key)
{
node *p,*prev,*curr;
p = new node;
//dynamically allocate space using 'new'
strcpy(p->name,name1);
strcpy(p->usn,usn1);
p->link=NULL;
prev=NULL;
curr=h[hash_key];
if(curr==NULL) //getting hash pointer location Case: No collision
{
h[hash_key]=p;
return;
}
while(curr!=NULL)
// Case : On collision - Insert at rear end
{
prev=curr;
curr=curr->link;
}
prev->link=p;
void retrive()
{
fstream in;
char name[15],usn[15];
int j,count;
node *curr;
in.open("student.txt",ios::in); // open record file in input mode
if(!in)
{
cout<<"\n Unable to open the file in input mode";
getch();
exit(0);
}
while(!in.eof())
{
in.getline(name,15,'|'); //unpacking the record
in.getline(usn,15,'\n');
count=0;
for(j=0; j<strlen(usn); j++)
{
count=count+usn[j];
}
count=count%29;
//Hash Key = ASCII count% 29
hash_insert(name,usn,count);
}
cout<<"\n Enter the usn = ";
cin>>usn;
count=0;
for(j=0; j<strlen(usn); j++)
// Calculating Hash Key
count=count+usn[j];
count=count%29;
curr=h[count];
if(curr == NULL)
{
cout<<"\nRecord not found";
getch();
return;
}
do
{
if(strcmp(curr->usn,usn)==0) //When the record is found, retrieve
{
cout<<"\nRecord found : "<<curr->usn<<" "<<curr->name;
getch();
return;
}
else
{
curr=curr->link;
}
}while(curr!=NULL);
//Search till end of list
if(curr==NULL) //End of list reached with no record found
{
cout<<"\nRecord not found";
getch();
return;
}
void main()
{
int choice;
clrscr();
fstream out;
out.open("student.txt",ios::out);
if(!out)
{
cout<<"\nUnable to open the file in out mode";
getch();
exit(0);
}
for(;;)
{
cout<<"\n1:insert\n2: retrive\n3:exit\nEnter the choice - ";
cin>>choice;
switch(choice)
{
case 1: insert();
break;
case 2: retrive();
break;
case 3: exit(0);
default: cout<<"\nInvalid option";
}
}
}
Output :
1:insert
2:retrive
3:exit
Enter the choice 1
Enter the name ajay
Enter the usn - 10
1:insert
2:retrive
3:exit
Enter the choice 1
Enter the name rahul
Enter the usn - 20
1:insert
2:retrive
3:exit
Enter the choice 1
Enter the name deepak
Enter the usn - 30
1:insert
2:retrive
3:exit
Enter the choice 2
Enter the usn =
20
strcat(buffer,p->usn);
strcat(buffer,"\n");
out<<buffer;
p=p->link;
}
void display()
{
node *p;
if(first==NULL)
{
cout<<"\nList is empty";
return;
}
p=first;
while(p!=NULL)
{
cout<<"|"<<p->name<<" "<<p->usn<<"|"<<"->";
p=p->link;
}
}
void Insert()
//Insert the record at the rear end
{
char name[20],usn[15];
node *p,*q;
cout<<"\n Enter name = "; cin>>name;
cout<<"\nEnter usn
= "; cin>>usn;
p=new node;
strcpy(p->name,name);
strcpy(p->usn,usn);
p->link=NULL;
if(first==NULL)
{
first=p;
writeFile();
display();
//display the record on the screen
return;
}
for(q=first; q->link!=NULL; q=q->link)
{
;
}
q->link=p;
writeFile(); //writing the record to the file
display();
//display the records to the screen.
}
void Delete()
{
char usn[15];
node *curr,*prev,*del;
if(first==NULL)
{
printf("\nThe list is empty. Deletion is not possible");
return;
}
cout<<"\nEnter the usn to be deleted = "; cin>>usn;
if(strcmp(first->usn,usn)==0)
{
cout<<"\n Record deleted";
del = first;
delete del;
first=first->link;
writeFile();
return;
}
prev=NULL;
curr=first;
while( ( strcmp(curr->usn,usn) != 0 ) && curr!=NULL)
{
prev=curr;
curr=curr->link;
}
if(curr == NULL)
{
cout<<"\nThe student with usn "<<usn<<" is not present";
return;
}
prev->link = curr->link;
writeFile();
display();
//display the records to the screen
}
void main()
{
int ch;
clrscr();
for(;;)
{
cout<<"\n 1-Insert_rear \n 2-Delete_id
choice :";
cin>>ch;
switch(ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: exit(0);
default: cout<<"\n Invalid option";
break;
}
}
}
Output :
1-Insert_rear
2-Delete_id
3-Exit
Enter choice : 1
\n
3-Exit
\n
Enter