CPP and Ds Rec 4sem
CPP and Ds Rec 4sem
MANAGED BY
THE INSTITUTE OF HUMAN RESOURCE DEVELOPMENT
Government of Kerala undertaking
Affiliated to Kannur University
LABORATARY RECORD
OF
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 PAGE NO:01
SECTION-A
1
Implement dynamic memory allocation
6
operator overloading
7
Overloading unary minus
8
Implement sequential input and output
operations on file
9
Implement virtual base class
Implement inheritance
12
CAS CHEEMENI
CS_s4 PAGE NO:02
CAS CHEEMENI
CS_s4 PAGE NO:03
SECTION-B
3
Implement insertion sort
4
Implement bubble sort
5
Implement selection sort
6 Extract substring of given dimension from given
string
7
Add two polynomial
8
Circular queue
9
Stack operation
10
Doubly linked list
11
Quick sort
13 Postfix expression
14 Queue operation
15 Polynomial evaluation
CAS CHEEMENI
CS_s4 PAGE NO:04
CAS CHEEMENI
CS_s4 PAGE NO:05
CAS CHEEMENI
CS_s4 PAGE NO:06
CAS CHEEMENI
CS_s4 PAGE NO:07
CAS CHEEMENI
CS_s4 PAGE NO:08
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:09
AIM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int *p,n,i;
cout<<"Enter the limit";
cin>>n;
p=new int[n];
cout<<"Enter integer values";
for(i=0;i<n;i++)
{
cin>>p[i];
}
cout<<"\nEntered elements are";
for(i=0;i<n;i++)
{
cout<<p[i]<<endl;
}
delete []p;
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:10
OUTPUT
1 SQUARE
2 CUBE
3 POWER
4 EXIT
ENTER A CHOICE: 1
ENTER A NUMBER: 4
SQUARE IS : 16
ENTER A CHOICE: 2
ENTER A NUMBER: 6
SQUARE IS : 216
ENTER A CHOICE: 3
ENTER A NUMBER: 2
ENTER THE POWER: 2
SQUARE IS : 4
ENTER A CHOICE: 4
CAS CHEEMENI
CS_s4 PAGE NO:11
INLINE FUNCTIONS
AIM:
Write a C++ program to find the square, cube and power of a number
using inline functions. The program should ask the user to enter their
options for different operations and should exit only when the user
given the value for exit.(Menu driven)
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
inline void square()
{
int n,s;
cout<<"\n Enter a number";
cin>>n;
s=n*n;
cout<<"\n Square is "<<s;
}
inline void cube()
{
int n,q;
cout<<"\n Enter a number";
cin>>n;
q=n*n*n;
cout<<"\n Cube is "<<q;
}
inline void power()
{
int n,p,r;
cout<<"\n Enter a number";
cin>>n;
cout<<"\n Enter the power";
cin>>p;
CAS CHEEMENI
CS_s4 PAGE NO:12
CAS CHEEMENI
CS_s4 PAGE NO:13
r=pow(n,p);
cout<<"\n Power is "<<r;
}
int main()
{
clrscr();
int ch;
while(1)
{
cout<<"\n 1.Square";
cout<<"\n 2.Cube";
cout<<"\n 3.Power";
cout<<"\n 4.Exit";
cout<<"Enter your choice";
cin>>ch;
if(ch==1)
{
square();
}
else if(ch==2)
{
cube();
}
else if(ch==3)
{
power();
}
else if(ch==4)
{
break;
}
else
{
cout<<"\n Invalid choice";
}
}
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:14
OUTPUT
AREA OF SQUARE IS 16
AREA OF CIRCLE IS 28.26
AREA OF RECTANGLE IS 15
CAS CHEEMENI
CS_s4 PAGE NO:15
FUNCTION OVERLOADING
AIM:
Write a C++ program to find the area of any three shapes using function
overloading.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class shape
{
public:
void area(int s)
{
int a;
a=s*s;
cout<<"\n Area of square is "<<a;
}
void area(double r)
{
double a;
a=3.14*r*r;
cout<<"\n Area of circle is "<<a;
}
void area(int l, int b)
{
int a;
a=l*b;
cout<<"\n Area of rectangle is "<<a;
}
};
CAS CHEEMENI
CS_s4 PAGE NO:16
CAS CHEEMENI
CS_s4 PAGE NO:17
int main()
{
clrscr();
shape ob;
int s,l,b;
double r;
cout<<"\n Enter the side of a square";
cin>>s;
cout<<"\n Enter the radius of a circle";
cin>>r;
cout<<"\n Enter the length and breadth of a
rectangle";
cin>>l>>b;
ob.area(s);
ob.area(r);
ob.area(l,b);
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:18
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:19
FRIEND FUNCTION
AIM:
Write a C++ program to swap the integer data of two classes using friend
function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class B;
class A
{
int x;
public:
void readx()
{
cout<<"Enter value for x ";
cin>>x;
}
void printx()
{
cout<<"\n x= "<<x;
}
friend void swap(A &ob1,B &ob2);
};
class B
{
int y;
public:
void ready()
{
cout<<"Enter value for y ";
cin>>y;
}
void printy()
CAS CHEEMENI
CS_s4 PAGE NO:20
CAS CHEEMENI
CS_s4 PAGE NO:21
{
cout<<"\n y= "<<y;
}
friend void swap(A &ob1,B
&ob2);
};
void swap(A &ob1,B &ob2)
{
int t;
t=ob1.x;
ob1.x=ob2.y;
ob2.y=t;
}
int main()
{
clrscr();
A ob1;
B ob2;
ob1.readx();
ob2.ready();
cout<<"\n Before swapping:
";
ob1.printx();
ob2.printy();
swap(ob1,ob2);
cout<<"\n After swapping: ";
ob1.printx();
ob2.printy();
getch();
return 0;
};
CAS CHEEMENI
CS_s4 PAGE NO:22
OUTPUT
X=4
Y=5
X=4
Y=5
CAS CHEEMENI
CS_s4 PAGE NO:23
CONSTRUCTOR
AIM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
class Test
{
int x,y;
public:
Test(int a, int b)
{
x=a;
y=b;
}
Test(Test &ob)
{
x=ob.x;
y=ob.y;
}
void show()
{
cout<<"\n x= "<<x;
cout<<"\n y= "<<y;
}
};
CAS CHEEMENI
CS_s4 PAGE NO:24
CAS CHEEMENI
CS_s4 PAGE NO:25
int main()
{
clrscr();
int a,b;
cout<<"Enter value for a
and b";
cin>>a>>b;
Test ob1(a,b);
Test ob2(ob1);
ob1.show();
ob2.show();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:26
OUTPUT
AREA OF SQUARE IS 9
AREA OF CIRCLE IS 50.24
AREA OF RECTANGLE IS 15
CAS CHEEMENI
CS_s4 PAGE NO:27
OPERATOR OVERLOADING
PROGRAM:
#include<iostream.h>
#include<conio.h>
class shape
{
public:
void area(int s)
{
int a;
a=s*s;
cout<<"\n area of square is"<<a;
}
void area(double r)
{
double a;
a=3.14*r*r;
cout<<"\n area of circle is"<<a;
}
void area(int l,int b)
{
int a;
a=l*b;
cout<<"\n area of rectangle is"<<a;
}
CAS CHEEMENI
CS_s4 PAGE NO:28
CAS CHEEMENI
CS_s4 PAGE NO:29
};
int main()
{
shape ob;
int s,l,b;
double r;
cout<<"\n enter the side of square";
cin>>s;
cout<<"\n enter the radius of circle";
cin>>r;
cout<<"\n enter the length and breadth of
rectangle";
cin>>l>>b;
ob.area(s);
ob.area(r);
ob.area(l,b);
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:30
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:31
PROGRAM:
#include<iostream.h>
#include<conio.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator -();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<"x="<<x<<"";
cout<<"y="<<y<<"";
cout<<"z="<<z<<"";
CAS CHEEMENI
CS_s4 PAGE NO:32
CAS CHEEMENI
CS_s4 PAGE NO:33
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
clrscr();
space s;
s.getdata(10,-20,30);
cout<<"s:";
s.display();
-s;
cout<<"-s:";
s.display();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:34
PROGRAM:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
ofstream fout;
char ch;
int line=0,i;
clrscr();
fout.open("aschars",ios::app);
if(!fout)
{
cout<<"the file cannot be opened\n";
cout<<"press a key to exit\n";
getch();
exit(1);
}
for(i=33;i<128;i++)
{
fout.put((char)(i));
}
CAS CHEEMENI
CS_s4 PAGE NO:35
CAS CHEEMENI
CS_s4 PAGE NO:36
fout.close();
ifstream fin;
fin.open("aschars",ios::in);
fin.seekg(0);
for(i=33;i<128;i++)
{
fin.get(ch);
cout<<i<<"=";
cout.put((char)(i));
cout<<"\t";
if(!(i%8))
{
cout<<"\n";
line++;
}
if(line>22)
{
system("pause");
line=0;
}
}
cout<<"\npress a key to exit";
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:37
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:38
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollnumber;
public:
void getnumber(int a)
{
rollnumber=a;
}
void putnumber(void)
{
cout<<"roll no:"<<rollnumber<<"\n";
}
};
class test:virtual public student
{
protected:
float part1,part2 ;
public:
void getmark(float x,float y)
CAS CHEEMENI
CS_s4 PAGE NO:39
CAS CHEEMENI
CS_s4 PAGE NO:40
{
part1=x;
part2=y;
}
void putmark(void)
{
cout<<"marks obtained:"<<"\n";
cout<<"part1="<<part1<<"\n";
cout<<"part2"<<part2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sports wt:"<<score<<"\n";
}
};
class result:public test,public sports
CAS CHEEMENI
CS_s4 PAGE NO:41
CAS CHEEMENI
CS_s4 PAGE NO:42
{
float total;
public:
void display(void)
{
total=part1+part2+score;
putnumber();
putmark();
putscore();
cout<<"total score:"<<total<<"\n";
}
};
int main()
{
result student1;
student1.getnumber(678);
student1.getmark(30.5,25.5);
student1.getscore(7.0);
student1.display();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:43
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:44
VIRTUAL FUNCTION
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived:";
}
void show()
CAS CHEEMENI
CS_s4 PAGE NO:45
CAS CHEEMENI
CS_s4 PAGE NO:46
{
cout<<"\n show derived";
}
};
int main()
{
base b;
derived d;
base*bptr;
cout<<"bptr points to base";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n bptr points to derived";
bptr=&d;
bptr->display();
bptr->show();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:47
OUTPUT
Value of n=1
Value of n = 2
Value of number=2
CAS CHEEMENI
CS_s4 PAGE NO:48
PROGRAM:
#include<iostream.h>
#include<conio.h>
class Example
{
static int Number;
int n;
public:
void set_n()
{
n=++Number;
}
void show_n()
{
cout<<"value of n="<<n<<endl;
}
static void show_Number()
{
cout<<"value of number="<<Number<<endl;
}
};
int Example::Number;
CAS CHEEMENI
CS_s4 PAGE NO:49
CAS CHEEMENI
CS_s4 PAGE NO:50
int main()
{
clrscr();
Example example1,example2;
example1.set_n();
example2.set_n();
example1.show_n();
example2.show_n();
Example::show_Number();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:51
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:52
SINGLE INHERITANCE
PROGRAM:
#include<iostream.h>
class bank
{
protected:
int accno;
char name[20];
float balance;
public:
void getinfo()
{
cout<<"\n enter accno,name and balance";
cin>>accno>>name>>balance;
cout<<"\n account created....\n";
}
void putinfo()
{
cout<<"\n account no:"<<accno;
cout<<"\n name:"<<name;
cout<<"\n balance:"<<balance;
}};
class depositer:public bank
{
int amt;
public:
CAS CHEEMENI
CS_s4 PAGE NO:53
CAS CHEEMENI
CS_s4 PAGE NO:54
void deposit()
{
cout<<"\n enter the amount that you want to
deposit";
cin>>amt;
balance=balance+amt;
cout<<"\n your current balance
is"<<balance;
}
void withdraw()
{
cout<<"\n enter the amount that you want to
withdraw";
cin>>amt;
if(amt<=balance)
{
balance=balance-amt;
cout<<"\n your current balance
is"<<balance;
}
else
{
cout<<"\n you have no sufficient balance...";
}
}
};
CAS CHEEMENI
CS_s4 PAGE NO:55
CAS CHEEMENI
CS_s4 PAGE NO:56
int main()
{
int op;
depositer ob;
ob.getinfo();
ob.putinfo();
do
{
cout<<"\n 1.deposit";
cout<<"\n 2.withdraw";
cout<<"\n 3.exit";
cout<<"\n enter your choice";
cin>>op;
if(op==1)
{
ob.deposit();
}
else if(op==2)
{
ob.withdraw();
}
else if(op==3)
{
break;
}
CAS CHEEMENI
CS_s4 PAGE NO:57
CAS CHEEMENI
CS_s4 PAGE NO:58
else
{
cout<<"\n invalid choice...";
}
}
while(1);
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:59
DATA STRUCTURE
CAS CHEEMENI
CS_s4 PAGE NO:60
CAS CHEEMENI
CS_s4 PAGE NO:61
CAS CHEEMENI
CS_s4 PAGE NO:62
OUTPUT
ENTER 5 ELEMENTS: 1 2 3 4 5
ENTER 3 ELEMENTS: 1 2 3
CAS CHEEMENI
CS_s4 PAGE NO:63
PROGRAM:
#include<iostream.h>
#include<conio.h>
class linear
{
private:
int key,a[25],n,flag,i;
public:
void read();
void search();
};
void linear::read()
{
cout<<"Enter the limit ";
cin>>n;
cout<<"Enter "<<n<<" Elements ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the Item to search ";
cin>>key;
}
void linear::search()
{
flag=0;
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
CAS CHEEMENI
CS_s4 PAGE NO:64
CAS CHEEMENI
CS_s4 PAGE NO:65
if(flag==1)
cout<<"Key Found ";
else
cout<<"Key Not Found ";
}
int main()
{
linear ob;
clrscr();
ob.read();
ob.search();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:66
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:67
PROGRAM:
#include<iostream.h>
#include<conio.h>
class binary
{
private:
int i,key,a[25],n,low,high,mid;
public:
void read();
void search();
};
void binary::read()
{
cout<<"Enter the limit ";
cin>>n;
cout<<"Enter "<<n<<" Elements in Ascending order ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the Item to search ";
cin>>key;
}
void binary::search()
{
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
break;
else if(key<a[mid])
high=mid-1;
CAS CHEEMENI
CS_s4 PAGE NO:68
CAS CHEEMENI
CS_s4 PAGE NO:69
else
low=mid+1;
}
if(low>high)
{
cout<<"Key Not Found ";
}
else
{
cout<<"Key Found ";
}
}
int main()
{
binary ob;
clrscr();
ob.read();
ob.search();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:70
OUTPUT
ENTER 5 ELEMENTS: 3 8 5 7 1
CAS CHEEMENI
CS_s4 PAGE NO:71
PROGRAM:
#include<iostream.h>
#include<conio.h>
class insertion
{
private:
int i,j,key,a[25],n,flag;
public:
void read();
void sort();
void print();
};
void insertion::read()
{
cout<<"Enter the limit ";
cin>>n;
cout<<"Enter "<<n<<" Elements ";
for(i=0;i<n;i++)
cin>>a[i];
}
void insertion::sort()
{
flag=0;
for(i=1;i<n;i++)
{
flag++;
key=a[i];
j=i-1;
while(j>=0&&a[j]>key)
{
flag++;
CAS CHEEMENI
CS_s4 PAGE NO:72
CAS CHEEMENI
CS_s4 PAGE NO:73
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
cout<<"\nNumber of comprison is "<<flag;
}
void insertion::print()
{
for(i=0;i<n;i++)
cout<<"\n"<<a[i];
}
int main()
{
insertion ob;
clrscr();
ob.read();
ob.sort();
ob.print();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:74
OUTPUT
ENTER 5 ELEMENTS: 3 8 5 7 1
1
3
5
7
8
CAS CHEEMENI
CS_s4 PAGE NO:75
PROGRAM:
#include<iostream.h>
#include<conio.h>
class bubble
{
private:
int i,j,key,a[25],n,temp;
public:
void read();
void sort();
void print();
};
void bubble::read()
{
cout<<"Enter the limit ";
cin>>n;
cout<<"Enter "<<n<<" Elements ";
for(i=0;i<n;i++)
cin>>a[i];
}
void bubble::sort()
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
CAS CHEEMENI
CS_s4 PAGE NO:76
CAS CHEEMENI
CS_s4 PAGE NO:77
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void bubble::print()
{
for(i=0;i<n;i++)
cout<<"\n"<<a[i];
}
int main()
{
bubble ob;
clrscr();
ob.read();
ob.sort();
ob.print();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:78
:
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:79
PROGRAM:
#include<iostream.h>
#include<conio.h>
class selectionsort
{
public:
int i,j,n,loc,temp,min,a[30];
void read();
void sort();
void display();
};
void selectionsort::read()
{
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void selectionsort::sort()
{
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
CAS CHEEMENI
CS_s4 PAGE NO:80
CAS CHEEMENI
CS_s4 PAGE NO:81
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
void selectionsort::display()
{
cout<<"\nSorted list is as follows\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
void main()
{
selectionsort ob;
clrscr();
ob.read();
ob.sort();
ob.display();
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:82
OUTPUT
MPUTE
CAS CHEEMENI
CS_s4 PAGE NO:83
#include<iostream.h>
#include<conio.h>
#include<string.h>
class extra
{
public:
char str[20],substr[20];
int start,length,len,endlen;
void substring();
};
void extra::substring()
{
int i, j;
cout<<"Enter a string: ";
cin>>str;
len=strlen(str);
cout<<"\n Enter starting index : ";
cin>>start ;
cout<<"\n Enter length of substring: " ;
cin>>endlen;
i=start;
if(start >= 0 && start < len && endlen<len )
{
for(j = 0; str[i] !='\0' && endlen > 0; i++, j++)
{
substr[j] = str[i];
endlen--;
}
substr[j] = '\0';
for(int k=0;substr[k]!='\0';k++)
CAS CHEEMENI
CS_s4 PAGE NO:84
CAS CHEEMENI
CS_s4 PAGE NO:85
cout<<substr[k];
}
else
{
cout<<"Please enter properly ";
}
}
int main()
{
clrscr();
extra ob;
ob.substring();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:86
:
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:87
PROGRAM:
#include<iostream.h>
#include<conio.h>
class poly
{
int order;
int coeff[10];
public:
void initialize();
void getorder();
void create();
void poly3order(poly,poly);
void polyadd(poly,poly);
void display();
};
void poly::initialize()
{
int i;
for(i=0;i<10;i++)
coeff[i]=0;
}
CAS CHEEMENI
CS_s4 PAGE NO:88
CAS CHEEMENI
CS_s4 PAGE NO:89
void poly::getorder()
{
cout<<"Highest Order:";
cin>>order;
}
void poly::create()
{
int i;
for(i=order;i>-1;i--)
{
cout<<"exponent:"<<i<<" ";
cout<<"coefficient:";
cin>>coeff[i];
}
}
void poly::poly3order(poly f,poly s)
{
if(f.order==s.order)
order=f.order;
else
if(f.order>s.order)
order=f.order;
else if(s.order>f.order)
order=f.order;
}
CAS CHEEMENI
CS_s4 PAGE NO:90
CAS CHEEMENI
CS_s4 PAGE NO:91
CAS CHEEMENI
CS_s4 PAGE NO:92
CAS CHEEMENI
CS_s4 PAGE NO:93
clrscr();
poly p1,p2,p3;
cout<<"polynomial1:\n";
p1.initialize();
p1.getorder();
p1.create();
cout<<"polynomial2:\n";
p2.initialize();
p2.getorder();
p2.create();
p3.initialize();
p3.poly3order(p1,p2);
p3.polyadd(p1,p2);
cout<<"first polynomial:";
p1.display();
cout<<"second polynomial:";
p2.display();
cout<<"third polynomial:";
p3.display();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:94
OUTPUT
1. INSERT
2.REMOVE
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE[1/2/3/4]: 1
ENTER THE DATA :45
INSERTED
1. INSERT
2.REMOVE
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE[1/2/3/4]:3
45
1. INSERT
2.REMOVE
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE[1/2/3/4]: 2
REMOVED 45
1. INSERT
2.REMOVE
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE[1/2/3/4]: 4
CAS CHEEMENI
CS_s4 PAGE NO:95
CIRCULAR QUEUE
PROGRAM:
#include<iostream.h>
#include<conio.h>
class queue
{
int item;
struct node
{
int data;
node*link;
}
*front,*rear,*temp,*t,*null;
public:
queue();
void insert();
void remove();
void disp();
};
queue::queue()
{
rear=front=null;
}
CAS CHEEMENI
CS_s4 PAGE NO:96
CAS CHEEMENI
CS_s4 PAGE NO:97
void queue::insert()
{
cout<<"enter the data";
cin>>item;
temp=new node;
temp->data=item;
if(front==null)
{
front=rear=temp;
rear->link=front;
}
else
{
rear->link=temp;
rear=temp;
rear->link=front;
}
cout<<"\n inserted";
}
void queue::remove()
{
if(front==null)
{
cout<<"\n queue empty";
}
else
CAS CHEEMENI
CS_s4 PAGE NO:98
CAS CHEEMENI
CS_s4 PAGE NO:99
{
cout<<"\n queue elements are..";
temp=front;
while(temp!=rear)
{
cout<<"\n"<<temp->data;
temp=temp->link;
}
cout<<"\n"<<temp->data;
}
}
void main()
{
queue ob;
int ch;
clrscr();
do
{
cout<<"\n[1].insert";
cout<<"\n[2].remove";
cout<<"\n[3].disp";
cout<<"\n[4].exit";
cout<<"\n enter your choice[1/2/3/4]";
cin>>ch;
if(ch==1)
ob.insert();
CAS CHEEMENI
CS_s4 PAGE NO:100
CAS CHEEMENI
CS_s4 PAGE NO:101
else if(ch==2)
ob.remove();
else if(ch==3)
ob.disp();
}
while(ch!=4);
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:102
OUTPUT
1.PUSH
2.POP
3.PEEP
4.EXIT
1
ENTER THE ELEMENT: 4
ELEMENT INSERTED
CAS CHEEMENI
CS_s4 PAGE NO:103
STACK OPERATION
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stack
{
int s[20],top,n,i;
public:
stack();
void push();
void pop();
void peep();
};
stack::stack()
{
top=-1;
cout<<"\n enter the limit";
cin>>n;
}
void stack::push()
{
int item;
if(top==n-1)
{
CAS CHEEMENI
CS_s4 PAGE NO:104
CAS CHEEMENI
CS_s4 PAGE NO:105
cout<<"\nstack is full";
}
else
{
top++;
cout<<"\nenter the element";
cin>>item;
s[top]=item;
cout<<"\n element inserted";
}
}
void stack::pop()
{
if(top==-1)
{
cout<<"\nstack empty";
}
else
{
top--;
cout<<"\ndeleted";
}
}
void stack::peep()
{
if(top==-1)
{
CAS CHEEMENI
CS_s4 PAGE NO:106
CAS CHEEMENI
CS_s4 PAGE NO:107
CAS CHEEMENI
CS_s4 PAGE NO:108
CAS CHEEMENI
CS_s4 PAGE NO:109
else if(ch==2)
ob.pop();
else if(ch==3)
ob.peep();
}
while(ch!=4);
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:110
OUTPUT
1.APPEND.2.ADD.AT BEG.3.ADD
AFTER.4.COUNT.5.DELETE.6.DISPLAY.7.EXIT.
Enter the choice:1
1.APPEND.2.ADD.AT BEG.3.ADD
AFTER.4.COUNT.5.DELETE.6.DISPLAY.7.EXIT.
Enter the choice:3
CAS CHEEMENI
CS_s4 PAGE NO:111
#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 PAGE NO:112
CAS CHEEMENI
CS_s4 PAGE NO:113
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 PAGE NO:114
CAS CHEEMENI
CS_s4 PAGE NO:115
}
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 PAGE NO:116
CAS CHEEMENI
CS_s4 PAGE NO:117
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 PAGE NO:118
CAS CHEEMENI
CS_s4 PAGE NO:119
}
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 PAGE NO:120
CAS CHEEMENI
CS_s4 PAGE NO:121
case 7:exit(0);
break;
default:cout<<"\nWrong choice.";
}
}while(ch>0);
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:122
OUTPUT
CAS CHEEMENI
CS_s4 PAGE NO:123
CAS CHEEMENI
CS_s4 PAGE NO:124
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 PAGE NO:125
CAS CHEEMENI
CS_s4 PAGE NO:126
}
}
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 PAGE NO:127
CAS CHEEMENI
CS_s4 PAGE NO:128
{
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 PAGE NO:129
OUTPUT
1.APPEND.2.ADD.AT BEG.3.ADD
AFTER.4.COUNT.5.DELETE.6.DISPLAY.7.EXIT.
Enter the choice:1
1.APPEND.2.ADD.AT BEG.3.ADD
AFTER.4.COUNT.5.DELETE.6.DISPLAY.7.EXIT.
Enter the choice:3
CAS CHEEMENI
CS_s4 PAGE NO:130
#include<iostream.h>
#include<conio.h>
#include<process.h>
class linklist
{
public:
struct node
{
int data;
node*link;
}*p;
linklist();
void append();
void addatbeg();
void addafter();
void delet();
void count();
void display();
};
linklist::linklist()
{
p=NULL;
CAS CHEEMENI
CS_s4 PAGE NO:131
CAS CHEEMENI
CS_s4 PAGE NO:132
}
void linklist::append()
{
node*temp,*r;
int n;
cout<<"\nEnter the number:";
cin>>n;
if(p==NULL)
{
temp=new node;
temp->data=n;
temp->link=NULL;
p=temp;
}
else
{
temp=p;
while(temp->link!=NULL)
{
temp=temp->link;
}
r=new node;
r->data=n;
r->link=NULL;
temp->link=r;
}
}
CAS CHEEMENI
CS_s4 PAGE NO:133
CAS CHEEMENI
CS_s4 PAGE NO:134
void linklist::addatbeg()
{
int num;
node*temp;
temp=new node;
cout<<"\nEnter the number:";
cin>>num;
temp->data=num;
temp->link=p;
p=temp;
}
void linklist::addafter()
{
node*temp,*r;
temp=p;
int num,loc;
cout<<"\nEnter the number to add:";
cin>>num;
cout<<"\nEnter the location:";
cin>>loc;
for(int i=1;i<loc;i++)
{
temp=temp->link;
}
r=new node;
r->data=num;
CAS CHEEMENI
CS_s4 PAGE NO:135
CAS CHEEMENI
CS_s4 PAGE NO:136
r->data=num;
r->link=temp->link;
temp->link=r;
}
void linklist::delet()
{
int num;
node*old,*temp;
temp=p;
cout<<"\nEnter the number to be deleted:";
cin>>num;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==p)
{
p=temp->link;
}
else
{
old->link=temp->link;
}
delete temp;
return;
}
CAS CHEEMENI
CS_s4 PAGE NO:137
CAS CHEEMENI
CS_s4 PAGE NO:138
else
{
old=temp;
temp=temp->link;
}
}
cout<<"\nElement"<<num<<"not found";
}
void linklist::count()
{
int count=0;
node*temp;
temp=p;
while(temp!=NULL)
{
if(p==NULL)
{
cout<<"\nList is empty.";
}
else
{
count++;
temp=temp->link;
}
}
cout<<"\ncount:"<<count;
}
CAS CHEEMENI
CS_s4 PAGE NO:139
CAS CHEEMENI
CS_s4 PAGE NO:140
void linklist::display()
{
cout<<"\nThe linklist is:";
for(node*temp=p;temp!
=NULL;temp=temp->link)
{
cout<<"\n"<<temp->data;
}
}
void main()
{
clrscr();
linklist l;
int ch;
do
{
cout<<"\n1.append.2.add at beg.3.add
after.4.delete.5.count.6.display.7.exit.";
cout<<"\nEnter the choice:";
cin>>ch;
switch(ch)
{
case 1:l.append();
break;
case 2:l.addatbeg();
break;
CAS CHEEMENI
CS_s4 PAGE NO:141
CAS CHEEMENI
CS_s4 PAGE NO:142
case 3:l.addafter();
break;
case 4:l.delet();
break;
case 5:l.count();
break;
case 6:l.display();
break;
case 7:exit(0);
break;
default:cout<<"\Wrong choice.";
}
}while(ch<8);
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:143
CAS CHEEMENI
CS_s4 PAGE NO:144
OUTPUT
Result:40
CAS CHEEMENI
CS_s4 PAGE NO:145
POSTFIX EXPRESSION
#include<iostream.h>
#include<ctype.h>
#define size 25
inteval(char);
class stack
{
int s[size],top,i;
public:
stack()
{
top=0;
}
void push(int);
int pop();
intisempty();
intisfull();
};
int stack::isempty()
{
if(top==0)
return 1;
else
return 0;
CAS CHEEMENI
CS_s4 PAGE NO:146
CAS CHEEMENI
CS_s4 PAGE NO:147
}
int stack::isfull()
{
if(top==-1)
return 1;
else
return 0;
}
void stack::push(int i)
{
if(!isfull())
{
s[top]=i;
top++;
}
else
cout<<"\nStack Overflow\n";
}
int stack::pop()
{
if(!isempty())
{
return(s[--top]);
}
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:148
CAS CHEEMENI
CS_s4 PAGE NO:149
inteval(char e[])
{
int j=0,result;
stackob;
while(e[j]!='$')
{
if(isalpha(e[j]))
ob.push(e[j]);
else if(isdigit(e[j]))
ob.push(e[j]-48);
else
{
int y2=ob.pop();
int y1=ob.pop();
switch(e[j])
{
case '+':
result=y1+y2;
break;
case '-':
result=y1-y2;
break;
case '*':
result=y1*y2;
break;
case '/':
CAS CHEEMENI
CS_s4 PAGE NO:150
CAS CHEEMENI
CS_s4 PAGE NO:151
result=y1/y2;
break;
}
ob.push(result);
}
j++;
}
return(ob.pop());
}
int main()
{
char e[size];
cout<<"\nEnter postfix string : ";
cin>>e;
cout<<"Output = "<<eval(e);
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:152
OUTPUT
queue is
3
DELETED ELEMENT IS
CAS CHEEMENI
CS_s4 PAGE NO:153
QUEUE OPERATION
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{
int q[30],front,rear,limit,item;
public:
queue()
{
front=0;
rear=0;
}
void read();
void enqueue();
void dequeue();
void display();
};
void queue::read()
{
cout<<"\n enter the limit\n";
cin>>limit;
}
void queue::enqueue()
{
CAS CHEEMENI
CS_s4 PAGE NO:154
CAS CHEEMENI
CS_s4 PAGE NO:155
if(rear==limit)
{
cout<<"queue is full";
}
else
{
cout<<"\n enter the elements";
cin>>item;
q[rear]=item;
rear++;
}
}
void queue::dequeue()
{
if(front==rear)
{
cout<<"\n queue is empty \n";
}
else
{
item=q[front];
front++;
cout<<"\n DELETED ELEMENT IS\t"<<item;
cout<<"\n";
}
}
CAS CHEEMENI
CS_s4 PAGE NO:156
CAS CHEEMENI
CS_s4 PAGE NO:157
void queue::display()
{
if(front==rear)
{
cout<<"\n queue is empty \n";
}
else
{
cout<<"\n queue is \n";
for(int i=front;i<rear;i++)
{
cout<<"\t"<<q[i];
}
cout<<"\n";
}
}
void main()
{
clrscr();
int ch;
queue a;
a.read();
CAS CHEEMENI
CS_s4 PAGE NO:159
cin>>ch;
switch(ch)
{
case 1:a.enqueue();
break;
case 2:a.dequeue();
break;
case 3:a.display();
break;
case 4:exit(0);
break;
default:cout<<"invalid choice\n";
break;
}
}while(ch<5);
getch();
}
CAS CHEEMENI
CS_s4 PAGE NO:160
OUTPUT
2
1
Enter the value of x 3
CAS CHEEMENI
CS_s4 PAGE NO:161
POLYNOMIAL EVALUATION
#include<iostream.h>
#include<conio.h>
#include<math.h>
class polynomial
{
int a[10],n,x,i;
public:
void read();
voideval();
};
void polynomial::read()
{
cout<<"enter highest power of polynomial ";
cin>>n;
cout<<"enter co-efficient of polynomial ";
for(i=0;i<=n;i++)
cin>>a[i];
cout<<"Enter the value of x ";
cin>>x;
cout<<endl;
}
void polynomial::eval()
{
int result=0;
CAS CHEEMENI
CS_s4 PAGE NO:162
CAS CHEEMENI
CS_s4 PAGE NO:163
:
for(i=0;i<=n;i++)
result=result+a[i]*pow(x,i);
cout<<"\nGiven Polynomial is ";
for(i=n;i>0;i--)
cout<<a[i]<<"x^"<<i<<"+";
cout<<a[0]<<"x^0"<<endl;
cout<<"Result = "<<result<<endl;
}
int main()
{
polynomial p;
clrscr();
p.read();
p.eval();
getch();
return 0;
}
CAS CHEEMENI
CS_s4 PAGE NO:164
CAS CHEEMENI
CS_s4 PAGE NO:165
CAS CHEEMENI
CS_s4 PAGE NO:166
CAS CHEEMENI