Final Lab Manual Fs 2017
Final Lab Manual Fs 2017
Solution:
main()
{
string rev,names,n2;
ofstream s1,s2;
ifstream s3;
int n,i,j;
s1.open("f1.txt");
s2.open("f2.txt");
cout<<"Enter the number of names you want to enter:";
cin>>n;
for(i=1; i<=n; i++)
{
cout<<"Enter the name "<<i<<" :";
cin>>names;
rev.erase();
cout<<"Reverse of the name "<<i<<" is : ";
for(j=names.length(); j>=0; j--)
{
rev.erase();
rev=rev+names[j];
cout<<rev;
s1<<rev;
cout<<endl;
s1<<endl;
Dept. of ISE, APSCE Page 1 2015
File Structures Lab Manual By: Abhijit Das
}
s1.close();
s3.open("f1.txt");
rev.erase();
cout<<"Re-reversing of names are:\n";
for(i=1; i<=n; i++)
{
s3>>n2;
for(j=n2.length(); j>=0; j--)
{
rev.erase();
rev=rev+n2[j];
cout<<rev;
s2<<rev;
}
cout<<endl;
s2<<endl;
}
s2.close();
s3.close();
}
Solution:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class student
{
public:
string USN;
string Name;
string Branch;
String Semester;
string buffer;
void read_data();
void pack();
void write_to_file();
void unpack();
int search(string);
int delete_from_file(string);
void modify(string);
};
temp.erase();
temp+=USN+'|'+Name+'|'+Branch+'|'+Semester;
for(; temp.size()<100;) temp+='$';
buffer+=temp+'\n';
void student::write_to_file()
{
fstream file;
file.open("1.txt",ios::out|ios::app);
file<<buffer;
file.close();
}
void student::unpack()
{
string sem;
int ch=1,i=0;
USN.erase();
while (buffer[i]!='|')
USN+=buffer[i++];
Name.erase();
i++;
while (buffer[i]!='|')
Name+=buffer[i++];
Branch.erase();
i++;
while (buffer[i]!='|')
Branch+=buffer[i++];
Semester.erase();
i++;
while (buffer[i]!='$')
Semester+=buffer[i++];
int main()
{
int count,choice,len,i;
student s1;
string key;
while(1)
{
cout <<"\nMain Menu\n 1.Add \n\n 2.Delete \n\n 3.Modify \n\n
4.Search \n\nEnter the choice:";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\n\nhow many records to insert?\n";
cin>>count;
for (i=0; i<count; i++)
{
cout<<"Data\n";
s1.read_data();
s1.pack();
}
s1.write_to_file();
break;
case 2:
cout <<"\n\nEnter the key";
cin>>key;
i=s1.delete_from_file(key);
break;
Dept. of ISE, APSCE Page 5 2015
File Structures Lab Manual By: Abhijit Das
case 3:
cout <<"\n\nEnter the key";
cin>>key;
s1.modify(key);
break;
case 4:
cout <<"\n\nEnter the key";
cin>>key;
i=s1.search(key);
break;
default:
cout<<"\n\nWrong Choice";
}
}
return 0;
}
Solution:
#include <iostream>
#include <fstream>
#include <string>
void read_data();
void pack();
void write_to_file();
void unpack();
int search(string);
int delete_from_file(string);
void modify(string);
};
void student::read_data()
{
cout<<"\nUsn:";
cin>>USN;
cout<<"\nName:";
cin>>Name;
cout<<"\nBranch:";
Dept. of ISE, APSCE Page 6 2015
File Structures Lab Manual By: Abhijit Das
cin>>Branch;
cout<<"\nSemster:";
cin>>Semester;
}
void student::pack()
{
buffer+=USN+'|'+Name+'|'+Branch+'|'+Semester+'$'+'\n';
}
void student::write_to_file()
{
fstream file;
file.open("1.txt",ios::out|ios::app);
file<<buffer;
file.close();
}
void student::unpack()
{
int ch=1,i=0;
USN.erase();
while (buffer[i]!='|')
USN+=buffer[i++];
Name.erase();
i++;
while (buffer[i]!='|')
Name+=buffer[i++];
Branch.erase();
i++;
while (buffer[i]!='|')
Branch+=buffer[i++];
Semester.erase();
i++;
while (buffer[i]!='$')
Semester+=buffer[i++];
}
void student::modify(string key)
{
int choice;
if (delete_from_file(key))
{
cout<<"\n What to modify?";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nUSN:";
cin>>USN;
break;
case 2:
cout<<"\nName:";
cin>>Name;
break;
case 3:
cout<<"\nBranch:";
cin>>Branch;
break;
case 4:
cout<<"\nSemster:";
cin>>Semester;
break;
default:
cout <<"Wrong Choice";
}
buffer.erase();
pack();
write_to_file();
}
}
int student::delete_from_file(string key)
{
fstream file;
char del_mark='*',t;
Dept. of ISE, APSCE Page 8 2015
File Structures Lab Manual By: Abhijit Das
int pos,flag=0;
pos=search(key);
if (pos)
{
file.open("1.txt");
pos-=2; //skip the $ and \n characters
t=file.get();
while (t!='$' && pos!=0)
{
pos--;
file.seekp(pos,ios::beg);
t=file.get();
}
if (pos!=0) file.seekp(pos+2,ios::beg);
else file.seekp(pos,ios::beg);
file.put(del_mark);
flag=1;
}
file.close();
if (!flag) return 0;
else return 1;
}
int main()
{
int count,choice,len,i,j;
student s1;
string key;
while(1)
{
Solution:
#include <iostream>
#include <fstream>
#include <string>
int rrn_list[100];
void read_data();
void pack();
void write_to_file();
void unpack();
void create_rrn();
void search_by_rrn(int);
};
void student::read_data()
{
cout<<"\nUsn:";
cin>>USN;
cout<<"\nName:";
cin>>Name;
cout<<"\nBranch:";
void student::create_rrn()
{
ifstream file;
int pos;
count=-1;
file.open("1.txt",ios::in);
while (!file.eof())
{
pos=file.tellg();
buffer.erase();
getline(file,buffer);
rrn_list[++count]=pos;
}
file.close();
}
int main()
Dept. of ISE, APSCE Page 11 2015
File Structures Lab Manual By: Abhijit Das
{
int choice,rrn;
student s1;
s1.create_rrn();
while(1)
{
cout <<"\nMain Menu\n 1.Add \n\n 2.Search 3.EXIT\n\nEnter
the choice:";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Data\n";
s1.read_data();
s1.pack();
s1.write_to_file();
break;
case 2:
cout <<"\n\nEnter the RRN";
cin>>rrn;
s1.search_by_rrn(rrn);
break;
case 3:
return 0;
default:
cout<<"\n\nWrong Choice";
break;
}
}
}
Solution:
#include<fstream>
#include<string>
#include<iostream>
#include<cstring>
#include<cstdlib>
#define MAX 50
int count=0;
using namespace std;
class student
{
public:
char usn[20];
char name[20];
}
}
}
if(!flag)
{
cout<<"Record not found\n";
return -1;
}
else
return j;
}
file2<<i<<"|";
file2<<temp.usn<<"|";
int ch=1,temp;
file1.open("i.txt",ios::out|ios::trunc);
file2.open("r.txt",ios::out|ios::trunc);
while(ch!=5)
{
cout<<"enter your choice\n";
cout<<"----MENU----\n";
cout<<"1.ADD\n2.SEARCH\n3.DELETE\n4.DISPLAY\n5.EXIT\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"how many students?\n";
cin>>n;
Dept. of ISE, APSCE Page 15 2015
File Structures Lab Manual By: Abhijit Das
for(int i=0;i<n;i++)
{
cout<<"record # "<<(i+1)<<"\n";
record[i]=add(i,record);
}
break;
case 2: search();
break;
case 3: del(record);
break;
case 4: display();
break;
case 5: file1.close();
file2.close();
exit(0);
default: cout<<"wrong choice\n";
}
}
return 0;
}
Solution:
#include<fstream>
#include<string>
#include<iostream>
#include<cstring>
#include<cstdlib>
#define MAX 50
int count=0;
using namespace std;
class student
{
public:
char usn[20];
char name[20];
char age[5];
char sem[5];
char branch[20];
};
void display()
{
fstream file1,file2;
char index[5];
char usn[20];
char name[20];
Dept. of ISE, APSCE Page 16 2015
File Structures Lab Manual By: Abhijit Das
char age[5];
char sem[5];
char branch[20];
file1.open("i.txt",ios::in);
file2.open("r.txt",ios::in);
for(int i=0; i<count; i++)
{
file2.getline(index,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,20,'\n');
cout<<"usn: "<<usn<<"\tname: "<<name<<"\tage:
"<<age<<"\tsem: "<<sem<<"\tbranch: "<<branch<<"\n";
}
}
int search()
{
fstream file1,file2,file3;
int flag=0,i,j,k;
char index1[5],index2[5];
char usn1[20],usn2[20],usn3[20],name1[20],name2[20];
char name[20];
char age[5];
char sem[5];
char branch[20];
file1.open("i.txt",ios::in);
file2.open("r.txt",ios::in);
file3.open("s.txt",ios::in);
{
file1.getline(index1,5,'|');
file1.getline(usn2,20,'\n');
if(strcmp(usn1,usn2)==0)
{
for(k=0; k<count; k++)
{
file2.getline(index2,5,'|');
file2.getline(usn3,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,20,'\n');
Dept. of ISE, APSCE Page 17 2015
File Structures Lab Manual By: Abhijit Das
if(strcmp(index1,index2)==0)
{
flag=1;
cout<<"Record found\n";
cout<<"usn: "<<usn3<<"\tname:
"<<name<<"\tage: "<<age<<"\tsem: "<<sem<<"\tbranch: "<<branch<<"\n";
break;
}
}
}
}
}
if(!flag)
{
cout<<"Record not found\n";
return -1;
}
else
return k;
}
int ch=1,temp;
file1.open("i.txt",ios::out|ios::trunc);
file2.open("r.txt",ios::out|ios::trunc);
file3.open("s.txt",ios::out|ios::trunc);
while(ch!=5)
{
cout<<"enter your choice\n";
cout<<"----MENU----\n";
cout<<"1.ADD\n2.SEARCH\n3.DELETE\n4.DISPLAY\n5.EXIT\n";
Dept. of ISE, APSCE Page 19 2015
File Structures Lab Manual By: Abhijit Das
cin>>ch;
switch(ch)
{
case 1:
cout<<"how many students?\n";
cin>>n;
Solution:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class coseq
{
public:
string list1[100], list2[100];
int count1, count2;
void load_list1();
void load_list2();
void sort_list();
void coseq::load_list1()
{
fstream file;
string name;
count1=-1;
file.open("name1.txt");
while (!file.eof()){
name.erase();
getline(file,name);
list1[++count1]=name;
}
file.close();
}
void coseq::load_list2()
{
count2=-1;
fstream file;
string name;
file.open("name2.txt");
while (!file.eof()) {
name.erase();
getline(file,name);
list2[++count2]=name;
}
file.close();
}
void coseq::sort_list()
{
int i,j;
string temp;
for (i=0;i<=count1;i++)
{
for (j=i+1;j<=count1;j++)
{
if (list1[i]>list1[j])
{
temp=list1[i];
list1[i]=list1[j];
list1[j]=temp;
} } }
for (i=0;i<=count2;i++)
{
for (j=i+1;j<=count2;j++)
{
if (list2[i]>list2[j])
Dept. of ISE, APSCE Page 21 2015
File Structures Lab Manual By: Abhijit Das
{
temp=list2[i];
list2[i]=list2[j];
list2[j]=temp;
}
}
}
}
void coseq::match()
{
int i=0,j=0;
if (list1[i]==list2[j])
{
cout<<"\n"<<list1[i];
i++;
j++;
}
if (list1[i] < list2[j]) i++;
}
}
int main()
{
coseq c1;
c1.load_list1();
c1.load_list2();
c1.sort_list();
c1.match();
return 0;
}
Solution:
#include <iostream>
#include <fstream>
#include <string>
void load_list();
void read_file(int);
void sort_list(int);
void merge();
};
void coseq::read_file(int i)
{
fstream file;
string name;
switch(i)
{
case 1: file.open("name1.txt");break;
case 2: file.open("name2.txt");break;
case 3: file.open("name3.txt");break;
case 4: file.open("name4.txt");break;
case 5: file.open("name5.txt");break;
case 6: file.open("name6.txt");break;
case 7: file.open("name7.txt");break;
case 8: file.open("name8.txt");break;
while (!file.eof())
{
name.erase();
getline(file,name);
list[i][++count[i]]=name;
}
file.close();
}
void coseq::load_list()
{
for (int i=1;i<=9;i++)
{
count[i]=-1;
read_file(i);
sort_list(i);
}
void coseq::sort_list(int k)
{
int i,j;
string temp;
for (i=0;i<=count[k];i++)
{
for (j=i+1;j<=count[k];j++)
{
if (list[k][i]>list[k][j])
{
temp=list[k][i];
list[k][i]=list[k][j];
list[k][j]=temp;
}
}
}
}
void coseq::merge()
{
string smallest;
int small_list, t=-1,start=1,avail[10],avail_lists=10;
for (int i=1;i<=9;i++)
{
avail[i]=1;
current[i]=1;
}
while (avail_lists>1)
{
if (!avail[start])
{
start++;
continue;
}
small_list=start;
smallest=list[start][current[start]];
for (int i=start+1;i<=7;i++)
{
if (!avail[i]) continue;
if (list[i][current[i]]<smallest)
{
smallest=list[i][current[i]];
small_list=i;
}
}
current[small_list]++;
if (current[small_list]>count[small_list])
{
avail[small_list]=0;
avail_lists--;
Dept. of ISE, APSCE Page 24 2015
File Structures Lab Manual By: Abhijit Das
}
outlist[++t]=smallest;
for (int j=1;j<=9;j++)
if (j!=small_list)
{
if (list[j][current[j]]==smallest)
{
current[j]++;
}
}
}
for (int i=1;i<=9;i++) if (avail[i])
{
for (int j=current[i];j<=count[i];j++)
outlist[++t]=list[i][j];
}
cout<<"\nThe Merged List:";
for (int i=0;i<=t+1;i++) cout<<"\n"<<outlist[i];
int main()
{
coseq c1;
c1.load_list();
c1.merge();
return 0;
}
Solution:
#include <iostream>
#include <cmath>
struct node
{
int ele[4];
int child[4];
};
class btree
{
public:
Dept. of ISE, APSCE Page 25 2015
File Structures Lab Manual By: Abhijit Das
node *tree[10][10];
int count[10];
int leaf;
int path[10];
btree();
node* create_node();
void insert(int);
void main_search(int);
void display_tree();
void insert_node(node*,int);
void search(int);
int search_node (node*,int);
int nodefull(node*);
void split(node*);
};
btree::btree()
{
leaf=-1;
for (int i=0;i<10;i++)
{
count[i]=-1;
path[i]=-1;
}
}
node* btree::create_node()
{
node *n;
n = new node;
for (int i=0;i<4;i++)
{
n->ele[i]=-1;
n->child[i]=-1;
}
return n;
}
if ( (nodefull(tree[leaf][n])) )
{
split(tree[leaf][n]);
insert(key);
}
else
insert_node(tree[leaf][n],key);
}
}
for (i=0;node1->ele[i]!=-1;i++)
if (node1->ele[i]==key)
{
flag=1;
break;
}
if (flag)
cout <<"\nElement Found";
else
cout<<"\nNot Found";
}
void btree::display_tree()
{
int i,j,k;
for (i=0;i<=leaf;i++)
{
cout<<"\n\nLevel------ " <<i<<"\n";
Dept. of ISE, APSCE Page 27 2015
File Structures Lab Manual By: Abhijit Das
for (j=0;j<=count[i];j++)
{
for (k=0;tree[i][j]->ele[k]!=-1;k++)
cout<<" "<<tree[i][j]->ele[k];
cout<<"\t";
}
}
}
for (i=0;i<=count+1;i++)
cout<<"\t\t"<<node1->ele[i];
}
t=count[leaf];
n=path[leaf];
for (i=t,j=t+1;i>n;i--,j--) //move the elements in
leaf level one place right
tree[leaf][j]=tree[leaf][i];
x=leaf;
if (count[leaf]+1==1) t=1;
else
{
t1=(double)t;
std::swap(tree[leaf][i],tree[x][i]);
}
for (i=t,j=i-1;i>0;i--,j--)
{
for (k=0;k<(count[i]+1)/2;k++)
{
n1=tree[i][2*k];
n2=tree[i][(2*k)+1];
newnode=create_node();
count[j]++;
tree[j][count[j]]=newnode;
newnode->ele[0]=n1->ele[x-1];
newnode->child[0]=2*k;
newnode->ele[1]=n2->ele[y-1];
newnode->child[1]=(2*k)+1;
}
int main()
{
btree bt;
int choice, key;
while(1)
{
cout<<"\n\n\nMain
Menu\n-------------------\n1.Insert\n2.Search\n3.Display
Tree\n4.Exit\n\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"\nEnter the element:";
cin>>key;
bt.insert(key);
break;
case 2:cout<<"Enter the key:";
cin>>key;
bt.main_search(key);
break;
case 3: bt.display_tree();
break;
case 4: return 0;
default: cout<<"\nEnter valid choice";
}
}
}
Solution:
#include <iostream>
#include <cmath>
struct node
{
int ele[4];
int child[4];
Dept. of ISE, APSCE Page 31 2015
File Structures Lab Manual By: Abhijit Das
node *next;
};
class bptree
{
public:
node *tree[10][10];
int count[10];
int leaf;
int path[10];
node *head;
bptree();
node* create_node();
void insert(int);
void main_search(int);
void display_tree();
void insert_node(node*,int);
void search(int);
int search_node (node*,int);
int nodefull(node*);
void split(node*);
void display_seqset();
};
bptree::bptree()
{
leaf=-1;
for (int i=0;i<10;i++)
{count[i]=-1;path[i]=-1;}
}
node* bptree::create_node()
{
node *n;
n = new node;
for (int i=0;i<4;i++)
{
n->ele[i]=-1;
n->child[i]=-1;
}
n->next=NULL;
return n;
}
void bptree::insert(int key)
{
int n, parent;
node *first_node;
if (leaf==-1)
{
first_node=create_node();
tree[0][0]=first_node;
leaf++;count[0]++;
first_node->ele[0]=key;
head=first_node; //header node of seq set
Dept. of ISE, APSCE Page 32 2015
File Structures Lab Manual By: Abhijit Das
}
else if (leaf==0)
{
if (nodefull(tree[0][0]))
{
path[leaf]=0;
split(tree[0][0]);
insert(key);
}
else insert_node(tree[0][0],key);
}
else
{
search(key);
n=path[leaf];
parent=path[leaf-1];
if ( (nodefull(tree[leaf][n])) )
{
split(tree[leaf][n]);
insert(key);
}
else
insert_node(tree[leaf][n],key);
}
}
for (i=0;node1->ele[i]!=-1;i++)
if (node1->ele[i]==key)
{
flag=1;
break;
}
void bptree::display_tree()
{
int i,j,k;
for (i=0;i<=leaf;i++)
{
Dept. of ISE, APSCE Page 33 2015
File Structures Lab Manual By: Abhijit Das
cout<<"\n\nLevel------ " <<i<<"\n";
for (j=0;j<=count[i];j++)
{
if (i!=leaf)
k=1;
else
k=0; //print first element only at leaf
level
for (;tree[i][j]->ele[k]!=-1;k++)
cout<<" "<<tree[i][j]->ele[k];
cout<<"\t";
}
}
}
if (node1->ele[0]==key) //new
element is the lowest, hence propogate this till root
{
for (i=leaf-1;i>=0;i--)
{
x=path[i+1];
if (tree[i][path[i]]->ele[x] > key) tree[i]
[path[i]]->ele[x]=key;
else insert_node(tree[i][x],key);
}
}
for (i=0;i<=count+1;i++)
cout<<"\t\t"<<node1->ele[i];
}
t=count[leaf];
n=path[leaf];
for (i=t,j=t+1;i>n;i--,j--) //move the elements in
leaf level one place right
tree[leaf][j]=tree[leaf][i];
x=leaf;
if (count[leaf]+1==1)
t=1;
else
{
t1=(double)t;
t1=log(count[leaf]+1)/log(2); //how many levels does
the tree need?
t=(int)t1;
}
for (i=t,j=i-1;i>0;i--,j--)
{
for (k=0;k<=count[i]/3;k++)
{
n1=tree[i][2*k];
n2=tree[i][(2*k)+1];
newnode=create_node();
count[j]++;
tree[j][count[j]]=newnode;
newnode->ele[0]=n1->ele[0];
newnode->child[0]=2*k;
newnode->ele[1]=n2->ele[0];
newnode->child[1]=(2*k)+1;
}
void bptree::display_seqset()
{
node *t;
int k;
t=head;
cout<<"\n\nThe sequence set is:";
while (t)
{
for (k=0;t->ele[k]!=-1;k++)
cout<<" "<<t->ele[k];
cout<<"\t";
t=t->next;
}
}
int main()
{
bptree bt;
int choice, key;
while(1)
{
cout<<"\n\n\nMain
Menu\n-------------------\n1.Insert\n2.Search\n3.Display
Tree\n4.Display Sequence Set\n5.Exit\n\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"\nEnter the element:";
cin>>key;
bt.insert(key);
Dept. of ISE, APSCE Page 37 2015
File Structures Lab Manual By: Abhijit Das
break;
case 2:cout<<"Enter the key:";
cin>>key;
bt.main_search(key);
break;
case 3: bt.display_tree();
break;
case 4: bt.display_seqset();
break;
case 5: return 0;
default: cout<<"\nEnter valid choice";
}
}
}
11. Write a C++ program to store and retrieve student data from
file using hashing. Use any collision resolution technique.
Solution:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student
{
public:
string USN;
string Name;
string Branch;
string Semester;
string buffer;
void read_data();
void pack();
void write_to_file();
void unpack(int);
void search(string);
int hash(string);
};
void student::read_data()
{
void student::write_to_file()
{
fstream file;
string temp;
int count, pos;
pos=hash(USN);
pos--;
pos=pos*304;
file.open("1.txt");
file.seekp(pos,ios::beg);
getline(file,temp);
file.close();
count=temp[0]-48;
file.open("1.txt");
if (count<0)
{
file.seekp(pos,ios::beg);
file.put('1');
pos=pos+1;
}
else if (count==1)
{
file.seekp(pos,ios::beg);
file.put('2');
pos=pos+102;
}
else if (count==2)
Dept. of ISE, APSCE Page 39 2015
File Structures Lab Manual By: Abhijit Das
{
file.seekp(pos,ios::beg);
file.put('3');
pos=pos+203;
}
cout<<"\nInserting at:"<<pos;
file.seekp(pos,ios::beg);
file<<buffer;
file.close();
string sem;
int ch=1,i=0;
USN.erase();
while (buffer[i]!='|')
USN+=buffer[i++];
Name.erase();
i++;
while (buffer[i]!='|')
Name+=buffer[i++];
Branch.erase();
i++;
while (buffer[i]!='|')
Branch+=buffer[i++];
Semester.erase();
i++;
while (buffer[i]!='$')
Semester+=buffer[i++];
}
pos=hash(key);
pos--;
pos=pos*304;
count=temp[0]-48;
file.seekp(pos,ios::beg);
while (i<=count)
{
buffer.erase();
getline(file,buffer);
unpack(i++);
if (key==USN)
{
cout<<"\nThe Record details are-";
cout<<"\nUSN:"<<USN<<"\nName:"<<Name<<"\nBranch:"<<Branch<<"\nSemest
er:"<<Semester;
flag=1;
}
file.close();
}
int main()
{
int choice;
student s1;
string key;
while (1)
{
cout <<"\nMain Menu\n 1.Add \n\n 2.Search \n\n
3.Exit\n\nEnter the choice:";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Data\n";
s1.read_data();
s1.pack();
s1.write_to_file();
break;
case 2:
cout <<"\n\nEnter the key";
cin>>key;
s1.search(key);
break;
default:
cout<<"\n\nWrong Choice";
}
}
12. Write a C++ program to reclaim the free space resulting from
the deletion of records using linked lists.
Solution:
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<fstream>
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";
exit(0);
}
p=first;
while(p!=NULL)
{
strcpy(buffer,p->name);
strcat(buffer,"|");
strcat(buffer,p->usn);
strcat(buffer,"\n");
Dept. of ISE, APSCE Page 42 2015
File Structures Lab Manual By: Abhijit Das
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 Delete()
{
char usn[15];
node *curr,*prev,*del;
if(first==NULL)
{
printf("\nThe list is empty. Deletion is not possible");
Dept. of ISE, APSCE Page 43 2015
File Structures Lab Manual By: Abhijit Das
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
}
int main()
{
int ch;
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;
}
}
}