DSREC4 - CopyCOMP2
DSREC4 - CopyCOMP2
MANAGED BY
THE INSTITUTE OF HUMAN RESOURCE DEVELOPMENT
Government of Kerala undertaking
Affiliated to Kannur University
LABORATARY RECORD
OF
PROGRAMMING WITH CPP AND DATASTRUCTRE
2022-23
NAME: ____________________
REG.NO: __________________
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
MANAGED BY
THE INSTITUTE OF HUMAN RESOURCES DEVELOPMENT
Government of Kerala Undertaking
Affiliated to Kannur University
REG.NO:____________________
Certified that this is a benefice Record of practical work done in computer Laboratory
By___________________ of fourth semester B.Sc computer science during the period
june 2022 to November 2022
Lecturer in charge
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
C++
&
DATA
STRUCTURE
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
SECTION-A
1
Implement dynamic memory allocation using new and
delete operators
8
Implement binary operator overloading
9
Implement inheritance
12
Implement sequential input and output
operations on file
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
SECTION-A
10
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
DOUBLY LINKEDLIST
#include<iostream.h>
#include<conio.h>
#include<process.h>
class dlinklist
{
struct dnode
{
dnode*prev;
int data;
dnode*next;
}*p;
public:
dlinklist();
void d_append();
void d_addatbeg();
void d_addafter();
void d_count();
void d_delet();
void d_display();
};
dlinklist::dlinklist()
{
p=NULL;
}
void dlinklist::d_append()
{
int num;
cout<<"\nEnter the data:";
cin>>num;
CAS CHEEMENI
CS_s4
dnode *r,*q;
q=p;
if(p==NULL)
{
q=new dnode;
q->prev=NULL;
q->data=num;
q->next=NULL;
p=q;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
r=new dnode;
r->data=num;
r->next=NULL;
r->prev=q;
q->next=r;
}
}
void dlinklist::d_addatbeg()
{
dnode *q;
int num;
cout<<"\nEnter the number:";
cin>>num;
q=new dnode;
q->prev=NULL;
q->data=num;
q->next=p;
p->prev=q;
p=q;
CAS CHEEMENI
CS_s4
}
void dlinklist::d_addafter()
{
dnode *q=p;
int num,loc;
cout<<"\nEnter the number:";
cin>>num;
cout<<"\nEnter the location:";
cin>>loc;
for(int i=0;i<loc;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"\nThey are less than"<<loc<<"elements.";
return;
}
}
dnode *temp=new dnode;
temp->data=num;
temp->prev=q;
temp->next=q->next;
temp->next->prev=temp;
q->next=temp;
}
void dlinklist::d_delet()
{
dnode *q=p;
int num;
cout<<"\nEnter the number to delete:";
cin>>num;
while(q!=NULL)
{
if(q->data==num)
{
CAS CHEEMENI
CS_s4
if(q==p)
{
p=p->next;
p->prev=NULL;
}
else
{
if(q->next==NULL)
{
q->prev->next=NULL;
}
else
{
q->prev->next=q->next;
q->next->prev=q->prev;
}
}
cout<<"\nThe element"<<q->data<<"is deleted.";
delete q;
return;
}
q=q->next;
}
}
void dlinklist::d_count()
{
int c=0;
dnode *temp=p;
while(temp!=NULL)
{
temp=temp->next;
c++;
}
cout<<"\nCount:"<<c;
CAS CHEEMENI
CS_s4
}
void dlinklist::d_display()
{
dnode *temp=p;
cout<<"\nThe double linklist is:";
while(temp!=NULL)
{
cout<<temp->data<<"\n";
temp=temp->next;
}
}
void main()
{
clrscr();
dlinklist dl;
int ch;
do
{
cout<<"\n1.APPEND.2.ADD.AT BEG.3.ADD
AFTER.4.COUNT.5.DELETE.6.DISPLAY.7.EXIT.";
cout<<"\nEnter the choice:";
cin>>ch;
switch(ch)
{
case 1:dl.d_append();
break;
case 2:dl.d_addatbeg();
break;
case 3:dl.d_addafter();
break;
case 4:dl.d_count();
break;
case 5:dl.d_delet();
break;
case 6:dl.d_display();
break;
CAS CHEEMENI
CS_s4
case 7:exit(0);
break;
default:cout<<"\nWrong choice.";
}
}while(ch>0);
getch();
}
CAS CHEEMENI
CS_s4
OUTPUT
CAS CHEEMENI
CS_s4
QUICK SORT
#include<iostream.h>
#include<conio.h>
class quick
{
public:
int l[10],i,n,temp;
int read();
static int split(int *,int,int);
void quick_sort(int first,int last);
void display();
};
int quick::read()
{
int count=0;
cout<<"\nEnter the limit:";
cin>>n;
cout<<"\nEnter the elements:";
for(i=0;i<n;i++)
{
count++;
cin>>l[i];
}
return count;
}
void quick::quick_sort(int first,int last)
{
if(first<last)
{
i=split(l,first,last);
quick_sort(first,i-1);
quick_sort(i+1,last);
CAS CHEEMENI
CS_s4
}
}
int quick::split(int *l,int first,int last)
{
int left,right,temp,loc,pivolt_elt;
left=first+1;
right=last;
pivolt_elt=l[first];
while(left<=right)
{
while(l[left]<pivolt_elt)
{
left=left+1;
}
while(l[right]>pivolt_elt)
{
right=right-1;
}
if(left<right)
{
temp=l[left];
l[left]=l[right];
l[right]=temp;
}
}
temp=l[first];
l[first]=l[right];
l[right]=temp;
loc=right;
return loc;
}
void quick::display()
{
for(i=0;i<n;i++)
CAS CHEEMENI
CS_s4
{
cout<<l[i]<<" ";
}
}
void main()
{
clrscr();
quick q;
int c=q.read();
cout<<"\nElements before sorting:";
q.display();
q.quick_sort(0,c-1);
cout<<"\nElements after sorting is:";
q.display();
getch();
}
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI
CS_s4
CAS CHEEMENI