124
Vidyaya Amrutham Ashnuthe Department of Information Science & Engineering FILE STRUCTURE LABORATORY MANUAL SUBCODE: 06ISL67, VI SEMESTER B.E BNM Institute of Technology (Approved by AICTE & Affiliated to VTU, Karnataka) (An ISO 9001:2008 Certified Institution)

BNMIT file structure lab manual

Embed Size (px)

Citation preview

Page 1: BNMIT file structure lab manual

Vidyaya Amrutham Ashnuthe

Department of Information Science & Engineering

FILE STRUCTURE LABORATORY MANUAL

SUBCODE: 06ISL67, VI SEMESTER B.E

BNM Institute of Technology

(Approved by AICTE & Affiliated to VTU, Karnataka)(An ISO 9001:2008 Certified Institution)

Post Box No, 7087, 27th cross, 12th Main Road,Banshankari 2nd Stage, Bengalore-560070

Phone: 91-8026711781/26711782Fax: 91-80-26710881,E-mail:[email protected]

Visit us at: www.bnmit.org

Page 2: BNMIT file structure lab manual

VTU LAB SYLLABUS

FILE STRUCTURE LABORATORY

Subject Code : 06ISL67   IA Marks : 25

No. of Practical

Hours/Week    

: 03 Exam

Hours

: 03

Total No. of Practical

Hours

: 42 Exam

Marks

: 50

 

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 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 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.

BNMIT/ISE/2012/VI/ File Structure laboratory Page 2

Page 3: BNMIT file structure lab manual

 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 Cosequential 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 k-way 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.

BNMIT/ISE/2012/VI/ File Structure laboratory Page 3

Page 4: BNMIT file structure lab manual

ContentsVTU LAB SYLLABUS....................................................................................2

Program 1..................................................................................................5

Program 2..................................................................................................9

Program 3................................................................................................19

Program 4................................................................................................28

Program 5................................................................................................34

Program 6................................................................................................43

Program 7................................................................................................60

Progrm 8..................................................................................................64

Program 9................................................................................................69

Program 10..............................................................................................80

Program 11..............................................................................................94

Program 12............................................................................................101

BNMIT/ISE/2012/VI/ File Structure laboratory Page 4

Page 5: BNMIT file structure 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.h>

#include<conio.h>

#include<process.h>

#include<string.h>

#include<fstream.h>

class names

{

public:char name[50];

};

void reverse(ofstream &out,char name[255])

{

char *rev;

rev=name+strlen(name)-1;

while(rev>=name)

{

cout<<*rev;

out<<*rev;

rev--;

}

cout<<endl;

out<<"\n";

BNMIT/ISE/2012/VI/ File Structure laboratory Page 5

Page 6: BNMIT file structure lab manual

}

void main()

{

names n[10];

clrscr();

ofstream out;

out.open("file.txt",ios::out|ios::app|ios::trunc);

int m;

cout<<"enter the no. of names to be entered\n";

cin>>m;

for(int i=0;i<m;i++)

{

cout<<"enter name";

cin>>n[i].name;

cout<<"the name in reverse order";

reverse(out,n[i].name);

}

out.close();

ifstream in;

in.open("file.txt",ios::in|ios::binary);

ofstream outf;

outf.open("f1.txt",ios::out|ios::app|ios::trunc);

char ch[255];

cout<<"names from files\n";

while(in)

{

in.getline(ch,255);

if(in)

BNMIT/ISE/2012/VI/ File Structure laboratory Page 6

Page 7: BNMIT file structure lab manual

reverse(outf,ch);

}

cout<<endl;

in.close();

outf.close();

in.open("f1.txt",ios::in|ios::binary);

outf.open("f2.txt",ios::out|ios::app|ios::trunc);

cout<<"reverse order from files\n";

while(in)

{

in.getline(ch,255);

if(in)

reverse(outf,ch);

}

in.close();

outf.close();

getch();

}

Output:

Enter the no. of names to be entered

5

Enter name

priya

The name in reverse order

ayirp

Enter name

padma

The name in reverse order

BNMIT/ISE/2012/VI/ File Structure laboratory Page 7

Page 8: BNMIT file structure lab manual

amdap

Enter name

ajit

The name in reverse order

tija

Enter name

sohan

The name in reverse order

nahos

Enter name

dilip

The name in reverse order

pilid

The names from files (f1.txt)

priya

padma

ajit

soni

dilip

Reverse order from files (f2.txt)

ayirp

amdap

tija

nahos

pilid

BNMIT/ISE/2012/VI/ File Structure laboratory Page 8

Page 9: BNMIT file structure lab manual

Program 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<conio.h>

#include<iostream.h>

#include<stdlib.h>

#include<fstream.h>

#include<string.h>

class student

{

public:char name[25],usn[15],branch[15],buffer[45];

};

student s,s1[100];

char extra[45];

int i,no=0,mode=0;

void pack()

{

fstream app;

if(mode==0)

app.open("st1.txt",ios::app);

else

app.open("st1.txt",ios::out);

if(!app)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 9

Page 10: BNMIT file structure lab manual

cout<<"cant open the file in output mode";

getch();

exit(0);

}

strcpy(s.buffer,s.name);

strcat(s.buffer,"|");

strcat(s.buffer,s.usn);

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;

app.close();

}

void unpack()

{

fstream in;

in.open("st1.txt",ios::in);

i=0,no=0;

if(!in)

{

cout<<"cant open the file in input mode";

getch();

exit(0);

}

while(!in.eof())

BNMIT/ISE/2012/VI/ File Structure laboratory Page 10

Page 11: BNMIT file structure lab manual

{

in.getline(s1[i].name,15,'|');

in.getline(s1[i].usn,15,'|');

in.getline(s1[i].branch,15,'|');

in.getline(extra,45,'\n');

no++;

i++;

}

in.close();

}

void write()

{

cout<<"\n enter the student name\n";

cin>>s.name;

cout<<"enter the student usn\n";

cin>>s.usn;

cout<<"enter the student branch\n";

cin>>s.branch;

pack();

mode=0;

}

void search()

{

char usn[15],extra[45];

cout<<"enter the usn to search=";

cin>>usn;

unpack();

for(i=0;i<no;i++)

BNMIT/ISE/2012/VI/ File Structure laboratory Page 11

Page 12: BNMIT file structure lab manual

{

if(strcmp(s1[i].usn,usn)==0)

{

cout<<"\nrecord found";

cout<<"\n"<<s1[i].name<<"\t"<<s1[i].usn<<"\t"<<s1[i].branch;

getch();

return;

}

}

cout<<"record not found";

getch();

return;

}

void display()

{

cout<<"name\t\t usn\t\t branch\n\n";

unpack();

for(int i=0;i<no;i++)

cout<<"\n\n"<<s1[i].name<<"\t\t"<<s1[i].usn<<"\t\t"<<s1[i].branch;

getch();

}

void modify()

{

char usn[15],buffer[15],extra[45];

cout<<"enter the usn to search\n";

cin>>usn;

unpack();no--;

for(int j=0;j<no;j++)

BNMIT/ISE/2012/VI/ File Structure laboratory Page 12

Page 13: BNMIT file structure lab manual

{

if(strcmp(usn,s1[j].usn)==0)

{

cout<<"the old values of the record are with

usn"<<usn<<"are";

cout<<"\nname="<<s1[j].name;

cout<<"\nusn="<<s1[j].usn;

cout<<"\nbranch="<<s1[j].branch;

cout<<"enter the new values\n";

cout<<"\nname=";

cin>>s1[j].name;

cout<<"\nusn=";

cin>>s1[j].usn;

cout<<"\nbranch=";

cin>>s1[j].branch;

break;

}

}

if(j==no)

{

cout<<"the record with usn is not present";

getch();

return;

}

mode=1;

for(j=0;j<no;j++)

{

strcpy(s.name,s1[j].name);

BNMIT/ISE/2012/VI/ File Structure laboratory Page 13

Page 14: BNMIT file structure lab manual

strcpy(s.usn,s1[j].usn);

strcpy(s.branch,s1[j].branch);

pack();

mode=0;

}

cout<<"record modified\n";

}

void main()

{

clrscr();

int choice;

for(;;)

{

//clrscr();

cout<<"\n0:exit";

cout<<"\n1:write";

cout<<"\n2:display";

cout<<"\n3:modify";

cout<<"\n4:search";

cout<<"enter u choice\n";

cin>>choice;

switch(choice)

{

case 1:write();

break;

case 2:display();

break;

case 3:modify();

BNMIT/ISE/2012/VI/ File Structure laboratory Page 14

Page 15: BNMIT file structure lab manual

break;

case 4:search();

break;

case 0:exit(0);

default:cout<<"\ninvalid input";

break;

}

}

}

Output:

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

1

Enter the student name

Karthik

Enter the student USN

1bg09is015

Enter the student branch

Ise

0: Exit

1: write

2: Display

3: Modify

BNMIT/ISE/2012/VI/ File Structure laboratory Page 15

Page 16: BNMIT file structure lab manual

4: Search

Enter your choice

1

Enter the student name

Dhanvi

Enter the student USN

1bg09is007

Enter the student branch

Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

2

Name USN Branch

Karthik 1bg09is015 Ise

Dhanvi 1bg09is007 Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

3

Enter the USN to Modify

1bg09is007

BNMIT/ISE/2012/VI/ File Structure laboratory Page 16

Page 17: BNMIT file structure lab manual

Record found

The old values of the record with usn 1bg09is007 are

USN=1bg09is007

Name=Dhanvi

Branch=ise

Enter new values

Name=dhruva

USN=1bg09is023

Branch=ise

Record modified

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

4

Enter the usn to search

1bg09is023

Record found

Dhruva

1bg09is023

Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

BNMIT/ISE/2012/VI/ File Structure laboratory Page 17

Page 18: BNMIT file structure lab manual

Enter your choice

0

Output from text file

karthik|1bg09is015|ise|||||||||||||||||||||||

dhruva|1bg09is023|ise||||||||||||||||||||||||

BNMIT/ISE/2012/VI/ File Structure laboratory Page 18

Page 19: BNMIT file structure lab manual

Program 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<conio.h>

#include<iostream.h>

#include<stdlib.h>

#include<fstream.h>

#include<string.h>

class student

{

public:char name[25],usn[15],branch[15],buffer[45];

};

student s,s1[100];

char extra[45];

int i,no=0,mode=0;

void pack()

{

fstream app;

if(mode==0)

app.open("st2.txt",ios::app);

else

app.open("st2.txt",ios::out);

if(!app)

{

cout<<"cant open the file in output mode";

BNMIT/ISE/2012/VI/ File Structure laboratory Page 19

Page 20: BNMIT file structure lab manual

getch();

exit(0);

}

strcpy(s.buffer,s.name);

strcat(s.buffer,"|");

strcat(s.buffer,s.usn);

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;

app.close();

}

void unpack()

{

fstream in;

in.open("st2.txt",ios::in);

i=0,no=0;

if(!in)

{

cout<<"cant open the file in input mode";

getch();

exit(0);

}

while(!in.eof())

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 20

Page 21: BNMIT file structure lab manual

in.getline(s1[i].name,15,'|');

in.getline(s1[i].usn,15,'|');

in.getline(s1[i].branch,15,'\n');

//in.getline(extra,45,'\n');

no++;

i++;

}

in.close();

}

void write()

{

cout<<"\n enter the student name\n";

cin>>s.name;

cout<<"enter the student usn\n";

cin>>s.usn;

cout<<"enter the student branch\n";

cin>>s.branch;

pack();

mode=0;

}

void search()

{

char usn[15],extra[45];

cout<<"enter the usn to search=";

cin>>usn;

unpack();

for(i=0;i<no;i++)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 21

Page 22: BNMIT file structure lab manual

if(strcmp(s1[i].usn,usn)==0)

{

cout<<"\nrecord found";

cout<<"\n"<<s1[i].name<<"\t"<<s1[i].usn<<"\t"<<s1[i].branch;

getch();

return;

}

}

cout<<"record not found";

getch();

return;

}

void display()

{

cout<<"name\t\t usn\t\t branch\n\n";

unpack();

for(int i=0;i<no;i++)

cout<<"\n\n"<<s1[i].name<<"\t\t"<<s1[i].usn<<"\t\t"<<s1[i].branch;

getch();

}

void modify()

{

char usn[15],buffer[15],extra[45];

cout<<"enter the usn to search\n";

cin>>usn;

unpack();no--;

for(int j=0;j<no;j++)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 22

Page 23: BNMIT file structure lab manual

if(strcmp(usn,s1[j].usn)==0)

{

cout<<"the old values of the record are with usn" <<usn << " are";

cout<<"\nname="<<s1[j].name;

cout<<"\nusn="<<s1[j].usn;

cout<<"\nbranch="<<s1[j].branch;

cout<<"enter the new values\n";

cout<<"\nname=";

cin>>s1[j].name;

cout<<"\nusn=";

cin>>s1[j].usn;

cout<<"\nbranch=";

cin>>s1[j].branch;

break;

}

}

if(j==no)

{

cout<<"the record with usn is not present";

getch();

return;

}

mode=1;

for(j=0;j<no;j++)

{

strcpy(s.name,s1[j].name);

strcpy(s.usn,s1[j].usn);

strcpy(s.branch,s1[j].branch);

BNMIT/ISE/2012/VI/ File Structure laboratory Page 23

Page 24: BNMIT file structure lab manual

pack();

mode=0;

}

cout<<"record modified\n";

}

void main()

{

int choice;

for(;;)

{

clrscr();

cout<<"\n0:exit";

cout<<"\n1:write";

cout<<"\n2:display";

cout<<"\n3:modify";

cout<<"\n4:search";

cout<<"enter u choice\n";

cin>>choice;

switch(choice)

{

case 1:write();

break;

case 2:display();

break;

case 3:modify();

break;

case 4:search();

break;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 24

Page 25: BNMIT file structure lab manual

case 0:exit(0);

default:cout<<"\ninvalid input";

break;

}

} }

Output:

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

1

Enter the student name

Karthik

Enter the student USN

1bg09is015

Enter the student branch

Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

1

Enter the student name

Dhanvi

BNMIT/ISE/2012/VI/ File Structure laboratory Page 25

Page 26: BNMIT file structure lab manual

Enter the student USN

1bg09is007

Enter the student branch

Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

2

Name USN Branch

Karthik 1bg09is015 Ise

Dhanvi 1bg09is007 Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

3

Enter the USN to Modify

1bg09is007

Record found

The old values of the record with usn 1bg09is007 are

USN=1bg09is007

Name=Dhanvi

Branch=ise

BNMIT/ISE/2012/VI/ File Structure laboratory Page 26

Page 27: BNMIT file structure lab manual

Enter new values

Name=dhruva

USN=1bg09is023

Branch=ise

Record modified

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

4

Enter the usn to search

1bg09is023

Record found

Dhruva

1bg09is023

Ise

0: Exit

1: write

2: Display

3: Modify

4: Search

Enter your choice

0

Output from file

karthik|1bg09is015|ise

dhruva|1bg09is023|ise

BNMIT/ISE/2012/VI/ File Structure laboratory Page 27

Page 28: BNMIT file structure lab manual

Program 4

Write a c++ program to write student objects with variable-length records using any sitable record structure and to read from this file a student record using RRN.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<stdlib.h>

#include<fstream.h>

#include<string.h>

class student

{

public:char name[25],usn[15],branch[15],buffer[45];

};

student s,s1[100];

char extra[45];

int i,no=0,mode=0;

void pack()

{

fstream app;

if(mode==0)

app.open("st3.txt",ios::app);

else

app.open("st3.txt",ios::out);

if(!app)

{

cout<<"cant open the file in output mode";

getch();

BNMIT/ISE/2012/VI/ File Structure laboratory Page 28

Page 29: BNMIT file structure lab manual

exit(0);

}

strcpy(s.buffer,s.name);

strcat(s.buffer,"|");

strcat(s.buffer,s.usn);

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;

app.close();

}

void unpack()

{

fstream in;

in.open("st3.txt",ios::in);

i=0,no=0;

if(!in)

{

cout<<"cant open the file in input mode";

getch();

exit(0);

}

while(!in.eof())

{

in.getline(s1[i].name,15,'|');

BNMIT/ISE/2012/VI/ File Structure laboratory Page 29

Page 30: BNMIT file structure lab manual

in.getline(s1[i].usn,15,'|');

in.getline(s1[i].branch,15,'\n');

//in.getline(extra,45,'\n');

no++;

i++;

}

in.close();

}

void write()

{

cout<<"\n enter the student name\n";

cin>>s.name;

cout<<"enter the student usn\n";

cin>>s.usn;

cout<<"enter the student branch\n";

cin>>s.branch;

pack();

mode=0;

}

void search()

{

char usn[15],extra[45];

int rrn;

cout<<"enter the rrn to search=";

cin>>rrn;

unpack();

for(int i=0;i<no-1;i++)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 30

Page 31: BNMIT file structure lab manual

if(rrn==i+1)

{

cout<<"\nrecord found";

cout<<"\n"<<s1[i].name<<"\t"<<s1[i].usn<<"\t"<<s1[i].branch;

getch();

return;

}

}

cout<<"record not found";

getch();

return;

}

void main()

{

int choice;

for(;;)

{

clrscr();

cout<<"\n0:exit";

cout<<"\n1:write";

cout<<"\n2:search";

cout<<"enter u choice\n";

cin>>choice;

switch(choice)

{

case 1:write();

break;

case 2:search();

BNMIT/ISE/2012/VI/ File Structure laboratory Page 31

Page 32: BNMIT file structure lab manual

break;

case 0:exit(0);

default:cout<<"\ninvalid input";

break;

}

}

}

Output:

0: Exit

1: write

2: Search

Enter your choice

1

Enter the student name

Satish

Enter the student USN

1bg09is055

Enter the student branch

Ise

0: Exit

1: write

2: Search

Enter your choice

1

Enter the student name

divya

Enter the student USN

1bg09is408

BNMIT/ISE/2012/VI/ File Structure laboratory Page 32

Page 33: BNMIT file structure lab manual

Enter the student branch

Ise

0: Exit

1: write

2: Search

Enter your choice

2

Enter the RRN to search=1

Satish 1bg09is055 Ise

Enter the RRN to search=2

divya 1bg09is408 Ise

Enter your choice

0

BNMIT/ISE/2012/VI/ File Structure laboratory Page 33

Page 34: BNMIT file structure lab manual

Program 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<stdio.h>#include<conio.h>

#include<string.h>

#include<iostream.h>

#include<process.h>

#include<values.h>

#include<fstream.h>

#include<stdlib.h>

class record

{

public:char name[20],usn[20],branch[20],ind[5];

}rec[20];

char st_no[5];

int no;

void pack()

{

fstream fle1,fle2;

fle1.open("i.txt",ios::out);

fle2.open("r.txt",ios::out);

if((!fle1)||(!fle2))

{

cout<<"cant open either\n";

getch();

exit(0);

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 34

Page 35: BNMIT file structure lab manual

for(int i=0;i<no;i++)

{

fle1<<rec[i].usn<<"|"<< i<<"\n";

fle2<<i<<"|"<<rec[i].name<<"|"<<rec[i].usn<<"|"<<rec[i].branch<<"\n";

}

fle1.close();

fle2.close();

}

void unpack()

{

fstream fle2;

fle2.open("r.txt",ios::in);

for(int i=0;i<no;i++)

{

fle2.getline(rec[i].ind,5,'|');

fle2.getline(rec[i].name,20,'|');

fle2.getline(rec[i].usn,20,'|');

fle2.getline(rec[i].branch,20,'\n');

}

fle2.close();

}

void search()

{

char name[20],usn[20],branch[20],ind[5];

unpack();

for(int i=0;i<no;i++)

if(strcmp(st_no,rec[i].ind)==0)

BNMIT/ISE/2012/VI/ File Structure laboratory Page 35

Page 36: BNMIT file structure lab manual

{

cout<<"search sucess\n";

cout<<"\nname="<<rec[i].name<<"usn="<<rec[i].usn<<"bran="<<rec[i].branch<<"\n";

}

}

void delete_rec(char rt_no[])

{

int flag=1;

char name[20],usn[20],branch[20];

for(int i=0;i<no;i++)

if(strcmp(rec[i].usn,rt_no)==0)

flag=i;

if(flag==-1)

{

cout<<"error\n";

getch();

exit(0);

}

if(flag==no)

{

no--;

cout<<"deleted\n";

pack();

return;

}

for(i=flag;i<no;i++)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 36

Page 37: BNMIT file structure lab manual

rec[i]=rec[i+1];

no--;

cout<<"deleted\n";

pack();

return;

}

}

void main()

{

char st_usn[20],rt_usn[20];

fstream fle1,fle2;

char name[20],usn[20],branch[20],ind[5];

int i,flag,ch;

clrscr();

for(;;)

{

cout<<"1:add\n2:search\n3:delete\n4:display\n";

cout<<"enter your choice\n";

cin>>ch;

switch(ch)

{

case 1:cout<<"enter the no of records\n";

cin>>no;

cout<<"enter the details of students\n";

for(i=0;i<no;i++)

{

cout<<"name=";

cin>>rec[i].name;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 37

Page 38: BNMIT file structure lab manual

cout<<"usn=";

cin>>rec[i].usn;

cout<<"branch=";

cin>>rec[i].branch;

}

pack();

break;

case 2:cout<<"enter thye usn u want to search\n";

cin>>st_usn;

//fstream fle1;

fle1.open("i.txt",ios::in);

if(!fle1)

{

cout<<"cant open the file\n";

getch();

exit(0);

}

flag=0;

for(i=0;i<no;i++)

{

fle1.getline(rt_usn,20,'|');

fle1.getline(st_no,5,'\n');

if(strcmp(rt_usn,st_usn)==0)

{

search();

flag=1;

}

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 38

Page 39: BNMIT file structure lab manual

if(!flag)

cout<<"not found\n";

fle1.close();

break;

case 3:cout<<"enter thye usn u want to delete\n";

cin>>st_usn;

fle1.open("i.txt",ios::in);

if(!fle1)

{

cout<<"cant open the file\n";

getch();

exit(0);

}

flag=0;

for(i=0;i<no;i++)

{

fle1.getline(rt_usn,20,'|');

fle1.getline(st_no,5,'\n');

if(strcmp(rt_usn,st_usn)==0)

{

delete_rec(rt_usn);

flag=1;

}

}

if(!flag)

cout<<"failure\n";

fle1.close();

break;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 39

Page 40: BNMIT file structure lab manual

case 4:for(int i=0;i<no;i++)

{

cout<<"name="<<rec[i].name<<"usn="<<rec[i].usn<<"

branch="<<rec[i].branch<<"\n";

break;

default:exit(0);

break;

}

}

}

Output:

1: add

2: search

3: delete

4: display

Enter your choice

1

Enter the number of records

2

Enter the details of students

Name = anu

Usn = 1bg09is003

Branch = ise

Name= naveen

Usn = 1bg09is089

Branch = ise

1: add

2: search

BNMIT/ISE/2012/VI/ File Structure laboratory Page 40

Page 41: BNMIT file structure lab manual

3: delete

4: display

Enter your choice

4

Name= anu usn= 1bg09is003 branch=ise

Name= naveen usn= 1bg09is067 branch=ise

1: add

2: search

3: delete

4: display

Enter your choice

2

Enter the usn you want to search

1bg09is067

Search success

Name= naveen usn= 1bg09is067 branch=ise

1: add

2: search

3: delete

4: display

Enter your choice

2

Enter the usn you want to search

1bg09is006

Not found

1: add

2: search

3: delete

BNMIT/ISE/2012/VI/ File Structure laboratory Page 41

Page 42: BNMIT file structure lab manual

4: display

Enter your choice

3

Enter the usn you want to delete

1bg09is067

Deleted

1: add

2: search

3: delete

4: display

Enter your choice

4

Name= anu usn= 1bg09is003 branch=ise

Enter your choice

5.

Output from Index file

1bg09is003|0

1bg09is067|1

Output from Record file

0|anu|1bg09is003|ise

1|Naveen|1bg09is067|ise

BNMIT/ISE/2012/VI/ File Structure laboratory Page 42

Page 43: BNMIT file structure 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.h>

#include<stdio.h>

#include<fstream.h>

#include<stdlib.h>

#include<string.h>

class buffer;

class student

{

char age[5];

char usn[20];

char name[20];

char branch[10];

char sem[5];

public:

void input();

void output();

friend class buffer;

friend class primaryindexing;

friend class secondaryindexing;

};

class buffer

{

char b[70];

public:

void pack(student &s);

BNMIT/ISE/2012/VI/ File Structure laboratory Page 43

Page 44: BNMIT file structure lab manual

void unpack(student &s);

void Read(fstream& fs);

int Write(char* filename);

buffer();

};

class primaryindexing

{

char keys[30][2][20];

int size;

char *indexfilename;

char *recordfilename;

public:

void retrieveOffset(int i,int &offset);

primaryindexing(char *rfilename,char *ifilename,int &offset);

~primaryindexing();

void writeOffset(int &offset);

int Insert(student &s,int &offset);

void Delete(char * usn);

void Search(char *usn);

int find(char *usn);

};

class secondaryindexing

{

char keys[30][2][20];

int size;

char *indexfilename;

char *recordfilename;

public:

BNMIT/ISE/2012/VI/ File Structure laboratory Page 44

Page 45: BNMIT file structure lab manual

void Insert(student &s);

void Search(char *name,primaryindexing &p);

void Delete(char *key);

secondaryindexing(char *rfilename,char *ifilename);

~secondaryindexing();

};

secondaryindexing :: secondaryindexing(char *rfilename,char *ifilename)

{

indexfilename = ifilename;

recordfilename = rfilename;

fstream indexfile(indexfilename,ios::in);

char usn[20],name[20];

size = 0;

while(indexfile)

{

indexfile.getline(name,20,'|');

indexfile.getline(usn,20,'|');

if(indexfile.eof())

break;

strcpy(keys[size][0],name);

strcpy(keys[size][1],usn);

size++;

}

indexfile.close();

}

void secondaryindexing :: Insert(student& s)

{

int i = size-1;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 45

Page 46: BNMIT file structure lab manual

while(i>=0 && strcmp(s.name,keys[i][0]) < 0)

{

strcpy(keys[i+1][0],keys[i][0]);

strcpy(keys[i+1][1],keys[i][1]);

i--;

}

while(i>=0 && strcmp(s.name,keys[i][0]) == 0 && strcmp(s.usn,keys[i][1]) < 0)

{

strcpy(keys[i+1][0],keys[i][0]);

strcpy(keys[i+1][1],keys[i][1]);

i--;

}

i++;

strcpy(keys[i][0],s.name);

strcpy(keys[i][1],s.usn);

size++;

}

void secondaryindexing :: Search(char *name,primaryindexing &p)

{

int offset,pos;

student s;

buffer b;

fstream file(recordfilename,ios::in);

int flag = 0;

for(int i = 0; i < size; i++)

{

if(strcmp(name,keys[i][0]) == 0)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 46

Page 47: BNMIT file structure lab manual

pos = p.find(keys[i][1]);

if(pos!= -1)

{

p.retrieveOffset(pos,offset);

file.seekg(offset);

b.Read(file);

b.unpack(s);

cout<<"-------------"<<endl;

s.output();

cout<<"-------------"<<endl;

flag = 1;

}

}

}

if(!flag)

cout<<" The Record(s) with the given name was not found "<<endl;

}

void secondaryindexing :: Delete(char *key)

{

for(int i = 0; i < size; i++)

if(strcmp(key,keys[i][1]) == 0)

sprintf(keys[i][1],"%d",-1);

}

secondaryindexing :: ~secondaryindexing()

{

fstream indexfile(indexfilename,ios::out|ios :: trunc);

for(int i = 0; i < size;i++)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 47

Page 48: BNMIT file structure lab manual

indexfile.write(keys[i][0],strlen(keys[i][0]));

indexfile<<'|';

indexfile.write(keys[i][1],strlen(keys[i][1]));

indexfile<<'|';

}

Indexfile.close();

}

int primaryindexing :: find(char *usn)

{

int first = 0, last = size-1;

int mid,temp;

while(first <= last)

{

mid = (first+last)/2;

temp = strcmp(keys[mid][0],usn);

if(temp == 0)

return mid;

else if(temp > 0)

last = mid-1;

else

first = mid+1;

}

return -1;

}

int primaryindexing :: Insert(student &s,int &offset)

{

int pos = find(s.usn);

if( pos != -1)

BNMIT/ISE/2012/VI/ File Structure laboratory Page 48

Page 49: BNMIT file structure lab manual

{

cout<<" The Record With the Given Key Already Exists "<<endl;

return 0;

}

buffer b;

b.pack(s);

int add = b.Write(recordfilename);

int i = size-1;

while(i >= 0 && strcmp(s.usn,keys[i][0]) < 0)

{

strcpy(keys[i+1][0],keys[i][0]);

strcpy(keys[i+1][1],keys[i][1]);

i--;

}

i++;

char offstr[20];

sprintf(offstr,"%d",offset);

strcpy(keys[i][0],s.usn);

strcpy(keys[i][1],offstr);

size++;

offset = offset + add;

return 1;

}

void primaryindexing :: Delete(char *usn)

{

int i = find(usn);

if(i == -1)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 49

Page 50: BNMIT file structure lab manual

cout<<"No Such Record Exists "<<endl;

return;

}

if( i == size-1)

{

cout<<"Record With the Given usn Was successfully deleted!!!"<<endl;

size--;

return;

}

for(; i < size; i++)

{

strcpy(keys[i][0],keys[i+1][0]);

strcpy(keys[i][1],keys[i+1][1]);

}

size--;

cout<<"Record With the Given usn Was successfully deleted!!!"<<endl;

}

void primaryindexing :: Search(char *usn)

{

int i = find(usn);

if(i == -1)

{

cout<<"No Such Record Exists "<<endl;

return;

}

fstream fs(recordfilename,ios::in|ios::out);

int offset;

student s;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 50

Page 51: BNMIT file structure lab manual

retrieveOffset(i,offset);

fs.seekg(offset);

buffer b;

b.Read(fs);

b.unpack(s);

s.output();

fs.close();

}

primaryindexing :: primaryindexing(char *rfilename,char* ifilename,int &offset)

{

indexfilename = ifilename;

recordfilename = rfilename;

fstream indexfile(indexfilename,ios::in);

char usn[20],offstr[20];

size = 0;

if(!indexfile)

{

offset = 0;

return;

}

indexfile.read((char*)&offset,sizeof(offset));

while(indexfile)

{

indexfile.getline(usn,20,'|');

indexfile.getline(offstr,20,'|');

if(indexfile.eof())

break;

strcpy(keys[size][0],usn);

BNMIT/ISE/2012/VI/ File Structure laboratory Page 51

Page 52: BNMIT file structure lab manual

strcpy(keys[size][1],offstr);

cout<<usn<<" "<<offstr<<endl;

size++;

}

indexfile.close();

}

void primaryindexing :: retrieveOffset(int i,int& offset)

{

char offstr[20];

strcpy(offstr,keys[i][1]);

offset = atoi(offstr);

}

void primaryindexing :: writeOffset(int &offset)

{

fstream indexfile(indexfilename,ios::out|ios :: trunc);

indexfile.write((char*)&offset,sizeof(offset));

indexfile.close();

}

primaryindexing :: ~primaryindexing()

{

fstream indexfile(indexfilename,ios::out|ios :: app);

for(int i = 0; i < size;i++)

{

indexfile.write(keys[i][0],strlen(keys[i][0]));

indexfile<<'|';

indexfile.write(keys[i][1],strlen(keys[i][1]));

indexfile<<'|';

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 52

Page 53: BNMIT file structure lab manual

indexfile.close();

}

void student :: input()

{

cout<<"Enter The Usn"<<endl;

cin>>usn;

cout<<"Enter The Name"<<endl;

cin>>name;

cout<<"Enter The Branch"<<endl;

cin>>branch;

cout<<"Enter The Semester"<<endl;

cin>>sem;

}

void student :: output()

{

cout<<"Usn:";

puts(usn);

cout<<"Name:";

puts(name);

cout<<"Branch:";

puts(branch);

cout<<"Semester:";

puts(sem);

}

buffer :: buffer()

{

for(int i = 0; i < 70; i++)

b[i] = '\0';

BNMIT/ISE/2012/VI/ File Structure laboratory Page 53

Page 54: BNMIT file structure lab manual

}

void buffer :: pack(student &s)

{

strcpy(b,s.usn);

strcat(b,"|");

strcat(b,s.name);

strcat(b,"|");

strcat(b,s.branch);

strcat(b,"|");

strcat(b,s.sem);

strcat(b,"|");

}

void buffer :: unpack(student &s)

{

char *p = b;

while(*p)

{

if(*p == '|')

*p = '\0';

p++;

}

p = b;

strcpy(s.usn,p);

p = p +strlen(p)+1;

strcpy(s.name,p);

p = p +strlen(p)+1;

strcpy(s.branch,p);

p = p +strlen(p)+1;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 54

Page 55: BNMIT file structure lab manual

strcpy(s.sem,p);

}

int buffer :: Write(char *filename)

{

fstream datafile(filename,ios::out|ios::app);

datafile.write(b,sizeof(b));

datafile.close();

return sizeof(b);

}

void buffer :: Read(fstream& fs)

{

fs.read(b,sizeof(b));

}

int main()

{

int choice =1,flag;

int offset;

student s;

char recordfilename[] = "record.txt";

char pindexfilename[] = "pindex.txt";

char sindexfilename[] = "sindex.txt";

char key[20];

primaryindexing iobj(recordfilename,pindexfilename,offset);

secondaryindexing sobj(recordfilename,sindexfilename);

while(choice < 4)

{

cout<<"1.Add Record"<<endl;

cout<<"2.Delete Record"<<endl;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 55

Page 56: BNMIT file structure lab manual

cout<<"3.Search Record"<<endl;

cout<<"4.Exit"<<endl;

cin>> choice;

switch(choice)

{

case 1: s.input();

flag = iobj.Insert(s,offset);

if(flag)

sobj.Insert(s);

break;

case 2: cout<<"Enter The Usn Of Record to be deleted"<<endl;

cin>>key;

iobj.Delete(key);

sobj.Delete(key);

break;

case 3: cout<<"Enter The Name Of Record(s) to be

Searched"<<endl;

cin>>key;

sobj.Search(key,iobj);

break;

}

}

iobj.writeOffset(offset);

return 0;

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 56

Page 57: BNMIT file structure lab manual

Output:

1: add record

2: delete record

3:search record

4:exit

1

Enter the USN

1bg09is002

Enter the name

Akshata

Enter the branch

Ise

Enter the semester

6

1: add record

2: delete record

3:search record

4:exit

1

Enter the USN

1bg09is087

Enter the name

sandeep

Enter the branch

Ise

Enter the semester

6

1: add record

BNMIT/ISE/2012/VI/ File Structure laboratory Page 57

Page 58: BNMIT file structure lab manual

2: delete record

3:search record

4:exit

3

Enter the name of record to be searched

Sandeep

----------------------------------

USN: 1bg09is087

Name: sandeep

Branch:ise

Sem:6

-----------------------------------

1: add record

2: delete record

3:search record

4:exit

3

Enter the name of record to be searched

Anitha

Record with the given name was not found

1: add record

2: delete record

3:search record

4:exit

2

Enter your usn of record to be deleted

1bg09is087

Record with given usn was successfully deleted

BNMIT/ISE/2012/VI/ File Structure laboratory Page 58

Page 59: BNMIT file structure lab manual

1: add record

2: delete record

3:search record

4:exit

3

Enter the name of record to be searched

Sandeep

Record with given name was not found

1: add record

2: delete record

3:search record

4:exit

4

Output from files before deletion

Record.txt

1bg09is002|akshata|ise|6

1bg09is087|sandeep|ise|6

Pindex.txt

1bg09is002|0|1bg09is087|70

Sindex.txt

Akshata|1bg09is002|sandeep|1bg09is087

Output from files after deletion

Record.txt

1bg09is002|akshata|ise|6

Pindex.txt

1bg09is002|0

Sindex.txt

Akshata|1bg09is002

BNMIT/ISE/2012/VI/ File Structure laboratory Page 59

Page 60: BNMIT file structure lab manual

Program 7

Write a C++ program to read two lists of names and then match the names in the two lists using Cosequential Match based on a single loop.

#include<iostream.h>

#include<stdio.h>

#include<fstream.h>

#include<stdlib.h>

#include<string.h>

#include<process.h>

#include<conio.h>

class coseq

{

char list1[100][20],list2[100][20];

int n1,n2;

public: void load();

void seq_sort();

void match();

};

fstream f1,f2,of;

void coseq :: load()

{

n1=n2=-1;

while(!f1.eof())

f1.getline(list1[++n1],20,'\n');

while(!f2.eof())

f2.getline(list2[++n2],20,'\n');

}

void coseq :: seq_sort()

BNMIT/ISE/2012/VI/ File Structure laboratory Page 60

Page 61: BNMIT file structure lab manual

{

char temp[30];

for(int i=0;i<n1;i++)

{

for(int j=i+1;j<n2;j++)

{

if(strcmp(list1[i],list1[j])>0)

{

strcpy(temp,list1[i]);

strcpy(list1[i],list1[j]);

strcpy(list1[j],temp);

}

}

}

for(i=0;i<n2;i++)

{

for(int j=i+1;j<n2;j++)

{

if(strcmp(list2[i],list2[j])>0)

{

strcpy(temp,list2[j]);

strcpy(list2[i],list2[j]);

strcpy(list2[j],temp);

}

}

}

}

void coseq :: match()

BNMIT/ISE/2012/VI/ File Structure laboratory Page 61

Page 62: BNMIT file structure lab manual

{

int i=0,j=0;

while((i<=n1)&&(j<=n2))

{

if(strcmp(list1[i],list2[j])==0)

{

cout<<list1[i]<<endl;

of<<list1[i]<<endl;

i++;

j++;

continue;

}

else if(strcmp(list1[i],list2[j])>0)

j++;

else

i++;

}

}

void main()

{

clrscr();

coseq cq;

f1.open("file1.txt",ios::in);

f2.open("file2.txt",ios::in);

if(!f1||!f2)

{

cout<<"\n ERROR file dont exist";

getch();

BNMIT/ISE/2012/VI/ File Structure laboratory Page 62

Page 63: BNMIT file structure lab manual

exit(1);

}

cq.load();f1.close();f2.close();

f1.open("file1.txt",ios::in);

f2.open("file2.txt",ios::in);

of.open("file3.txt",ios::out);

cq.seq_sort(); cq.match();

f1.close();

f2.close();

of.close();

getch();

}

Output:

Sunil

Input file “file1.txt” contains list1

sunil

jyothi

pradeep

Input file “file2.txt” contains list2

suma

avinash

sunil

kruthi

Output file “file3.txt” contains matched names

sunil

BNMIT/ISE/2012/VI/ File Structure laboratory Page 63

Page 64: BNMIT file structure lab manual

Progrm 8

Write a C++ program to read k Lists of names and merge them using kway merge algorithm with k = 8.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<string.h>

#include<fstream.h>

#include<stdlib.h>

fstream file[8];

char fname[8][8]={"1.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};

int no;

class record

{

public:char name[20],usn[20];

}rec[20];

void merge(char* file1,char* file2,char* filename)

{

int k=0;

record rec[20];

fstream f1,f2;

f1.open(file1,ios::in);

f2.open(file2,ios::in);

while(!f1.eof())

{

f1.getline(rec[k].name,20,'|');

f1.getline(rec[k++].usn,20,'\n');

BNMIT/ISE/2012/VI/ File Structure laboratory Page 64

Page 65: BNMIT file structure lab manual

}

while(!f2.eof())

{

f2.getline(rec[k].name,20,'|');

f2.getline(rec[k++].usn,20,'\n');

}

int t,y;

record temp;

for(t=0;t<k-2;t++)

for(y=0;y<k-t-2;y++)

if(strcmp(rec[y].name,rec[y+1].name)>0)

{

temp=rec[y];

rec[y]=rec[y+1];

rec[y+1]=temp;

}

fstream temp1;

temp1.open(filename,ios::out);

for(t=1;t<k-1;t++)

temp1<<rec[t].name<<"|"<<rec[t].usn<<"\n";

f1.close();

f2.close();

temp1.close();

return;

}

void kwaymerge()

{

char filename[7][20]={"11.txt","22.txt","33.txt", "44.txt"," 111.txt","222.txt" ,

BNMIT/ISE/2012/VI/ File Structure laboratory Page 65

Page 66: BNMIT file structure lab manual

"1111.txt"};

int k=0,i;

for(i=0;i<8;i+=2)

merge(fname[i],fname[i+1],filename[k++]);

k=4;

for(i=0;i<4;i+=2)

merge(filename[i],filename[i+1],filename[k++]);

merge(filename[4],filename[5],filename[6]);

return;

}

void main()

{

char name[20],usn[20],branch[20];

int i;

clrscr();

cout<<"enter the no of records\n";

cin>>no;

cout<<"enter the details of students";

for(i=0;i<8;i++)

file[i].open(fname[i],ios::out);

for(i=0;i<no;i++)

{

cout<<"name:";

cin>>rec[i].name;

cout<<"usn:";

cin>>rec[i].usn;

file[i%8]<<rec[i].name<<"|"<<rec[i].usn<<"\n";

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 66

Page 67: BNMIT file structure lab manual

for(i=0;i<8;i++)

file[i].close();

kwaymerge();

fstream result;

result.open("1111.txt",ios::in);

cout<<"sorted order\n";

for(i=0;i<no;i++)

{

result.getline(name,20,'|');

result.getline(usn,20,'\n');

cout<<"\nname="<<name<<"\n"<<"usn="<<usn<<"\n";

} getch();

}

Output:

Enter the no of records 10

Enter the details

Name: jyothi

Usn: 1

Name: nidhi

Usn: 2

Name: ashik

Usn: 3

Name: suraj

Usn: 4

Name: kruthi

Usn: 5

Name: harsha

Usn: 6

BNMIT/ISE/2012/VI/ File Structure laboratory Page 67

Page 68: BNMIT file structure lab manual

Name: shruthi

Usn: 7

Name: teju

Usn: 8

Name: seema

Usn: 9

Name: kavitha

Usn: 2

Sorted records are:

Name: ashik

Usn: 3

Name: harsha

Usn: 6

Name: jyothi

Usn: 1

Name: kavitha

Usn: 2

Name: kruthi

Usn: 5

Name: nidhi

Usn: 2

Name: seema

Usn: 9

Name: shruthi

Usn: 7

Name: suraj

Usn: 4

Name: teju

BNMIT/ISE/2012/VI/ File Structure laboratory Page 68

Page 69: BNMIT file structure lab manual

Usn: 8

BNMIT/ISE/2012/VI/ File Structure laboratory Page 69

Page 70: BNMIT file structure 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.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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 70

Page 71: BNMIT file structure lab manual

publicc:

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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 71

Page 72: BNMIT file structure lab manual

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];

BNMIT/ISE/2012/VI/ File Structure laboratory Page 72

Page 73: BNMIT file structure lab manual

}

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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 73

Page 74: BNMIT file structure lab manual

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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 74

Page 75: BNMIT file structure 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);

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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 75

Page 76: BNMIT file structure lab manual

}

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)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 76

Page 77: BNMIT file structure 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 <=2)

{

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;

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 77

Page 78: BNMIT file structure lab manual

}

return 0;

}

Output

1.Insert a Key

2.Search a key

3.Exit

1

Enter The Key to be inserted in B-Tree

5

1.Insert a Key

2.Search a key

3.Exit

1

Enter The Key to be inserted in B-Tree

8

---------------

5 8

----------------

1.Insert a Key

2.Search a key

3.Exit

1

Enter The Key to be inserted in B-Tree

4

---------------

4 5 8

----------------

BNMIT/ISE/2012/VI/ File Structure laboratory Page 78

Page 79: BNMIT file structure lab manual

1.Insert a Key

2.Search a key

3.Exit

1.Insert a Key

2.Search a key

3.Exit

1

Enter The Key to be inserted in B-Tree

7

---------------

4 5 7 8

----------------

1.Insert a Key

2.Search a key

3.Exit

1

Enter The Key to be inserted in B-Tree

11

---------------

5 11

4 5

7 8 11

----------------

1.Insert a Key

2.Search a key

3.Exit

2

BNMIT/ISE/2012/VI/ File Structure laboratory Page 79

Page 80: BNMIT file structure lab manual

Enter the key to be searched

11

Key found at level 2

1.Insert a Key

2.Search a key

3.Exit

3

BNMIT/ISE/2012/VI/ File Structure laboratory Page 80

Page 81: BNMIT file structure 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.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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 81

Page 82: BNMIT file structure lab manual

}

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()

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 82

Page 83: BNMIT file structure 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)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 83

Page 84: BNMIT file structure lab manual

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 i = 0; i < ptr->size;i++)

traverse(ptr->next[i]);

}

btree :: btree()

{

root = NULL;

head = NULL;

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 84

Page 85: BNMIT file structure lab manual

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];

BNMIT/ISE/2012/VI/ File Structure laboratory Page 85

Page 86: BNMIT file structure lab manual

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++)

BNMIT/ISE/2012/VI/ File Structure laboratory Page 86

Page 87: BNMIT file structure lab manual

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);

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 87

Page 88: BNMIT file structure 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);

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);

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 88

Page 89: BNMIT file structure lab manual

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)

{

BNMIT/ISE/2012/VI/ File Structure laboratory Page 89

Page 90: BNMIT file structure lab manual

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";

BNMIT/ISE/2012/VI/ File Structure laboratory Page 90

Page 91: BNMIT file structure lab manual

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 tree

4.Exit

1

Enter The Key to be inserted in B+Tree

5

1.Insert a Key

2.Search a key

3.traverse tree

BNMIT/ISE/2012/VI/ File Structure laboratory Page 91

Page 92: BNMIT file structure lab manual

4.Exit

1

Enter The Key to be inserted in B+Tree

8

---------------

5 8

----------------

1.Insert a Key

2.Search a key

3.traverse tree

4.Exit

1

Enter The Key to be inserted in B-Tree

4

---------------

4 5 8

----------------

1.Insert a Key

2.Search a key

3.traverse tree

4.Exit

1

Enter The Key to be inserted in B-Tree

7

---------------

4 5 7 8

----------------

1.Insert a Key

BNMIT/ISE/2012/VI/ File Structure laboratory Page 92

Page 93: BNMIT file structure lab manual

2.Search a key

3.traverse tree

4.Exit

1

Enter The Key to be inserted in B-Tree

11

---------------

5 11

4 5

7 8 11

----------------

1.Insert a Key

2.Search a key

3.traverse tree

4.Exit

2

Enter the key to be searched

11

Key found at level 2

1.Insert a Key

2.Search a key

3.traverse tree

4.Exit

3

------------------------

4 5

7 8 11

------------------------

BNMIT/ISE/2012/VI/ File Structure laboratory Page 93

Page 94: BNMIT file structure lab manual

1.Insert a Key

2.Search a key

3.traverse tree

4.Exit

4

BNMIT/ISE/2012/VI/ File Structure laboratory Page 94

Page 95: BNMIT file structure lab manual

Program 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;

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 95

Page 96: BNMIT file structure lab manual

cout<<"\nEnter the name = "; cin>>name;

cout<<"\n Enter 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 the 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;

BNMIT/ISE/2012/VI/ File Structure laboratory Page 96

Page 97: BNMIT file structure lab manual

}

prev->link=p;

}

void retrive()

{

fstream in;

char name[15],usn[15];

int j,count;

node *curr;

in.open("student.txt",ios::in); // open the 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++) //Calculate sum of ascii values of USN

{

count=count+usn[j];

}

count=count%29; //Hash Key = ASCII count% 29

hash_insert(name,usn,count);

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 97

Page 98: BNMIT file structure lab manual

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";

BNMIT/ISE/2012/VI/ File Structure laboratory Page 98

Page 99: BNMIT file structure lab manual

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\n 2: retrive \n3:exit \nEnter the choice \n";

cin>>choice;

switch(choice)

{

case 1: insert();

break;

case 2: retrive();

break;

case 3: exit(0);

default: cout<<"\nInvalid option";

BNMIT/ISE/2012/VI/ File Structure laboratory Page 99

Page 100: BNMIT file structure lab manual

}

}

}

Output:

1: insert

2: retrieve

3: exit

Enter your choice

1

Enter name= sanjay

Enter usn=1bg09is056

1: insert

2: retrieve

3: exit

Enter your choice

1

Enter name= ashish

Enter usn=1bg09is006

1: insert

2: retrieve

3: exit

Enter your choice

1

Enter name= shreya

Enter usn=1bg09is056

1: insert

2: retrieve

3: exit

BNMIT/ISE/2012/VI/ File Structure laboratory Page 100

Page 101: BNMIT file structure lab manual

Enter your choice

2

Enter usn= 1bg09is056

Record found 1bg09is056 sanjay

1: insert

2: retrieve

3: exit

Enter your choice

3

BNMIT/ISE/2012/VI/ File Structure laboratory Page 101

Page 102: BNMIT file structure lab manual

Program 12

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

#include<stdio.h>

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

#include<fstream.h>

class node

{

public:char name[20],usn[20];

node *link;

};

node *first=NULL;

void writefile()

{

fstream out;

node *p;

char buffer[500];

out.open("a1.txt",ios::out);

if(!out)

{

cout<<"cant open the file\n";

getch();

exit(0);

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 102

Page 103: BNMIT file structure lab manual

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<<"the list is empty\n";

return;

//getch();

//exit(0);

}

p=first;

while(p!=NULL)

{

cout<<p->name<<" "<<p->usn<<"->";

p=p->link;

}

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 103

Page 104: BNMIT file structure lab manual

void insert()

{

node *p,*q;

char name[20],usn[20];

cout<<"name=";

cin>>name;

cout<<"usn=";

cin>>usn;

p=new node;

strcpy(p->name,name);

strcpy(p->usn,usn);

p->link=NULL;

if(first==NULL)

{

first=p;

writefile();

display();

return;

}

for(q=first;q->link!=NULL;q=q->link);

q->link=p;

writefile();

display();

return;

}

void del1()

{

char usn[15];

BNMIT/ISE/2012/VI/ File Structure laboratory Page 104

Page 105: BNMIT file structure lab manual

node *prev,*curr,*del;

//char usn;

//int del;

if(first==NULL)

{

cout<<"list is empty\n";

return;

//getch();

//exit(0);

}

cout<<"enter the usn u want to delete\n";

cin>>usn;

if(strcmp(first->usn,usn)==0)

{

del=first;

delete del;

first=first->link;

writefile();

display();

return;

}

prev=NULL;

curr=first;

while((strcmp(curr->usn,usn)!=0)&&(curr!=NULL))

{

prev=curr;

curr=curr->link;

}

BNMIT/ISE/2012/VI/ File Structure laboratory Page 105

Page 106: BNMIT file structure lab manual

if(curr==NULL)

{

cout<<"the rec with the usn "<<usn<<"did not find";

getch();

exit(0);

}

prev->link=curr->link;

writefile();

display();

}

void main()

{

int ch;

clrscr();

for(;;)

{

cout<<"1:insert\n2:delete\n3:quit";

cout<<"enter your choice\n";

cin>>ch;

switch(ch)

{

case 1:insert();

break;

case 2:del1();

break;

case 3:exit(0);

break;

default:"invalid choice\n";

BNMIT/ISE/2012/VI/ File Structure laboratory Page 106

Page 107: BNMIT file structure lab manual

break;

}

}

}

Output:

1: insert

2. delete

3. exit

Enter your choice

1

Name=menatha

Usn=1bg09is048

Menatha 1bg09is048

1: insert

2. delete

3. exit

Enter your choice

1

Name=subash

Usn=1bg09is067

Menatha 1bg09is048 subash 1bg09is067

1: insert

2. delete

3. exit

Enter your choice

1

Name=ayush

Usn=1bg09is005

BNMIT/ISE/2012/VI/ File Structure laboratory Page 107

Page 108: BNMIT file structure lab manual

Menatha 1bg09is048 subash 1bg09is067 ayush 1bg09is005

1: insert

2. delete

3. exit

Enter your choice

2

Enter the usn you want to delete

1bg09is048

subash 1bg09is067 ayush 1bg09is005

1: insert

2. delete

3. exit

Enter your choice

1

Name=Samarth

Usn=1bg09is063

subash 1bg09is067 ayush 1bg09is005 Samarth 1bg09is063

1: insert

2. delete

3. exit

Enter your choice

3

BNMIT/ISE/2012/VI/ File Structure laboratory Page 108