fs lab
TRANSCRIPT
FILE STRUCTURES LABORATORY
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 networksskrowtenfileelifsystemmetsys
C:\TC\BIN>edit inputnetworksfilessystemcompilergraphics
C:\TC\BIN>FS1.exe input outputskrowtenselifmetsysrelipmocscihparg
C:\TC\BIN>type outputskrowtenselifmetsysrelipmocscihparg
C:\TC\BIN>FS1.EXE INPUTusage:FS1.EXE filename1 filename2
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.
#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 = "; cin>>usn; student s;
//Unpacking the record 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,'|'); in.getline(extra,45,'\n');
if(strcmp(s.usn,usn)==0) {
cout<<"\nRecord found";cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\
t"<<s.branch;getch();return;
} } cout<<"\n Record not found"; getch(); return;}
void displayFile(){ student s; int c,i; char extra[45]; 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\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,'!'); 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 file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = ajayEnter the usn = 1vk07is002Enter the age = 20Enter the sem = 6Enter the branch = ise
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = rahulEnter the usn = 1vk07cs045Enter the age = 20Enter the sem = 6Enter the branch = cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:2Name Usn Age Sem Branchajay 1vk07is002 20 6 ise!!!!!!!!!!!rahul 1vk07cs045 20 6 cse!!!!!!!!!!!
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:4
Enter the record's usn you want to search = 1vk07cs045Record foundrahul 1vk07cs045 20 6 cse!!!!!!!!!!!
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:5
C:\TC|BIN>type student.txtajay|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, 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; //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 = "; cin>>usn; student s;
//Unpacking the record 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');
if(strcmp(s.usn,usn)==0) {
cout<<"\nRecord found";cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\
t"<<s.branch;getch();return;
} } cout<<"\n Record not found"; getch(); return;}
void displayFile(){ student s; int c,i; 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\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 = "<< 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++) { out1<<s1[j].name<<'|'<<s1[j].usn<<'|'<<s1[j].age<<'|'<<s1[j].sem<<'|'<<s1[j].sem<<'|'<<s1[j].branch<<'\n'; } 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 file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = ajayEnter the usn = 1vk07is002Enter the age = 20Enter the sem = 6Enter the branch = ise
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = rahulEnter the usn = 1vk07cs045Enter the age = 20Enter the sem = 6Enter the branch = cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:2Name Usn Age Sem Branchajay 1vk07is002 20 6 iserahul 1vk07cs045 20 6 cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:4
Enter the record's usn you want to search = 1vk07cs045Record foundrahul 1vk07cs045 20 6 cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:5
C:\TC|BIN>type student.txtajay|1vk07is002|20|6|iserahul|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:Insert2.Search3.exit
Enter the choice:1
Enter the student name = ajayEnter the usn = 1vk07is002Enter the age = 20Enter the sem = 6Enter the branch = ise
1:Insert2.Search3.exit
Enter the choice:1
Enter the student name = rahulEnter the usn = 1vk07cs045Enter the age = 20Enter the sem = 6Enter the branch = cse
1:Insert2.Search3.exit
Enter the choice:2Enter the usn to be searched:1vk07is007Record not found
1:Insert2.Search3.exit
Enter the choice:2Enter the usn to be searched:1vk07cs045Record found
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.
#include<iostream.h>#include<string.h>#include<fstream.h>#include<stdlib.h>
//Record specificationclass record{ public: char age[5];
char usn[20],name[20],branch[5]; char sem[2];
}rec[20];
char st_no[5];int no;
void retrieve_details(){ fstream file2; char name[20],usn[20],branch[5]; char ind[5],age[5],sem[5]; file2.open("record.txt",ios::in); for(int i=0;i<no;i++) //Unpacking record data { 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'); if(strcmp(ind,st_no)==0) //Required record found - print details {
cout<<"\n\n"<<"Student details are: "; cout<<"\n\nUSN: "<<usn<<"\nName: "<<name<<"\nAge: "<<age<<"\nSem:
"<<sem<<"\nBranch: "<<branch<<"\n"; } } file2.close();}
void delete_record(char usno[]){ 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: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice : 1
Enter the no. of students :2Enter the details:
Name: ajayAge: 20USN: 1vk07is002Sem: 6Branch: ise
Name: rahulAge: 20USN: 1vk07cs045Sem: 6Branch: cse
1: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice : 4
Name: ajayAge: 20USN: 1vk07is002Sem: 6Branch: ise
Name: rahulAge: 20USN: 1vk07cs045Sem: 6Branch: cse
1: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice :3
Enter USN whose record is to be deleted:1vk07cs045Deleted !
1: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice :4
Name: ajayAge: 20
USN: 1vk07is002Sem: 6Branch: 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;}
void delete_index(char* nam){ fstream file; int i; int k=0; char name[20],usn[20],ind[5],index[20][20],chusn[20]; 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(nam,name)==0) {
strcpy(found[k].name,name); strcpy(found[k].usn,usn); strcpy(index[k],ind); k++;
} } file.close(); if(k==1) { delete_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) {
delete_record(index[i]); 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: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice : 1
Enter the no. of students :2Enter the details:
Name: ajayAge: 20USN: 1vk07is002Sem: 6Branch: ise
Name: rahulAge: 20USN: 1vk07cs045Sem: 6Branch: cse
1: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice : 4
Name: ajayAge: 20USN: 1vk07is002Sem: 6Branch: ise
Name: rahulAge: 20USN: 1vk07cs045Sem: 6Branch: cse
1: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice :3
Enter USN whose record is to be deleted:1vk07cs045Deleted !
1: Add Record2: Search Record3: Delete Record4: Display Record5: Exit
Enter u'r choice :4
Name: ajayAge: 20USN: 1vk07is002Sem: 6Branch: ise
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<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>#include<fstream.h>#include<iostream.h>
void writeLists(){ fstream out1,out2; int i,m,n; char name[20];
out1.open("file1.txt",ios::out); out2.open("file2.txt",ios::out);
if( (!out1) || (!out2) ) {
cout<<"Unable to open one of the list files"; getch(); exit(0);
}
cout<<"Enter no. of names you want to enter in file1 :"; cin>>m; cout<<"\nEnter the names in ascending order\n"; for(i=0;i<m;i++) {
cin>>name; out1<<name; out1<<'\n';
}
cout<<"Enter no. of names you want to enter in file2 :"; cin>>n; cout<<"\nEnter the names in ascending order \n"; 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 : 3Enter the names in ascending ordercseisetc
Enter no. of names you want to enter in file1 : 2Enter the names in ascending orderecise
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"};
void merge_file(char* file1, char* file2, char* filename){
record recrd[20];int k; k=0;fstream f1,f2;f1.open(file1,ios::in); //open the first filef2.open(file2,ios::in); //open the second filewhile(!f1.eof()) //Unpack and retrieve first file{
f1.getline(recrd[k].name,20,'|'); f1.getline(recrd[k++].usn,20,'\n');
}while(!f2.eof()) //Unpack and retrieve second file{
f2.getline(recrd[k].name,20,'|');f2.getline(recrd [k++].usn, 20,'\n');
}record temp;int t,y;for(t=0;t<k-2;t++) //Sort the retrieved recordsfor(y=0;y<k-t-2;y++)if(strcmp(recrd[y].name,recrd[y+1].name)>0){
temp=recrd[y];recrd[y]=recrd[y+1];recrd[y+1]=temp;
}fstream temp1;temp1.open(filename,ios::out); //Open the file to be packed
intofor(t=1;t<k-1;t++) //Pack the sorted records onto the file
temp1<<recrd[t].name<<"|"<<recrd[t].usn<<"\n";f1.close();f2.close();temp1.close();return;
}void kwaymerge(){
char filename[7][20]={"ll.txt","22.txt","33.txt","44.txt","lll.txtw","222.txt","llll.txr"};
int i;int k;k=0;for(i=0;i<8;i+=2) //Merge and sort the 8 original files onto{ //the four files indicated {ll.txt,22.txt....}
merge_file(fname[i],fname[i+1],filename[k++]);}k=4;for(i=0;i<4;i+=2) //Merge and sort the four files onto lll.txt and
222.txt{
merge_file(filename[i],filename[i+1],filename[k++]);}//Merge and sort the two files onto the llll.txt filemerge_file(filename[4],filename[5],filename[6]);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(); //Mergefstream 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 :4Enter the details :
Name :rahulUSN :25
Name :laxmiUSN :16
Name :ajayUSN :2
Name :deepakUSN :8
Sorted Records :
Name :ajayUSN :2
Name :deepakUSN :8
Name :laxmiUSN :16
Name :rahulUSN :25
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.h>#include<stdio.h>#include<fstream.h>#include<stdlib.h>#include<string.h>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;
}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;for(int 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;}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;elset = 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){
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){
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 <=2){
cout<<"1.Insert a Key\n";cout<<"2.Search a key\n";cout<<"3.Exit\n";cout<<”\n\n Enter u’r 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;
}}return 0;
}
Output :
1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree100
1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree50-----------50 100-----------1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree75
-------------50 75 100-------------1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree200
---------------50 75 100 200
---------------
1.Insert a Key2.Search a key3.Exit
Enter u’r choice :2
Enter The key to be searched100Key 100 exists in B-tree at level 1
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.h>#include<stdio.h>#include<fstream.h>#include<stdlib.h>#include<string.h>
class node{
public:int a[4];node * next[4];node * parent;int size;node();
};
class linkleaf{
public:node * data;linkleaf *next;linkleaf();
};
linkleaf :: linkleaf(){
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(){
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;
}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;elset = 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<<" 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";cout<<”\n enter u’r 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 Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree100
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree50--------50 100--------
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree200
----------50 100 200----------1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree75---------------50 75 100 200---------------
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 2
Enter The key to be searched300
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]; //Array of record pointers equal to size of hash keys - 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 tablevoid 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:insert2:retrive3:exit
Enter the choice – 1
Enter the name – ajayEnter the usn - 10
1:insert2:retrive3:exit
Enter the choice – 1
Enter the name – rahul Enter the usn - 20
1:insert2:retrive
3:exit
Enter the choice – 1
Enter the name – deepakEnter the usn - 30
1:insert2:retrive3:exit
Enter the choice – 2
Enter the usn = 20
Record found : rahul 20
12. Write a C++ program to reclaim the free space resulting from the deletion of records using linked lists.
#include<stdio.h>#include<conio.h> #include<stdlib.h> #include<string.h> #include<iostream.h> #include<fstream.h> #include<new.h>
class node{ public: char name[20];
char usn[20]; node *link;
};
node *first=NULL;
void writeFile(){ node *p; char buffer[100]; fstream out; out.open("student.txt", ios::out); if(!out) { cout<<"\n Unable to open the file student.txt in out mode"; getch(); exit(0); } p=first; while(p!=NULL) { strcpy(buffer,p->name); strcat(buffer,"|"); 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 screenreturn;
} 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 \n 3-Exit \n Enter 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_id3-ExitEnter choice : 1
Enter name : ajay
Enter USN : 10
|ajay 10|->1-Insert_rear 2-Delete_id3-ExitEnter choice : 1
Enter name : rahulEnter USN : 20
|ajay 10|rahul 20|->
1-Insert_rear 2-Delete_id3-ExitEnter choice : 1
Enter name : deepakEnter USN : 30
|ajay 10|rahul 20|Deepak 30|->
1-Insert_rear 2-Delete_id3-ExitEnter choice :2
Enter the usn to be deleted = 20|ajay 10|Deepak 30|->