1.
CONSTRUCTOR
AIM:
Write a program in C++ to implementing classes, object, constructors and member
functions for calculating area and perimeter of a circle.
ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as a,k,i in the prime class.
Step 3: Enter the value for find the prime number through object as prime() method from main
function.
Step 4: Calculate the value of Prime number of ‘n’ value.
Step 5: display the value of prime number in method of void show().
Step 6: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
using namespace std;
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<"\n"<<a<<" is Prime Number.";
else
cout<<"\n"<<a<<" is Not Prime Numbers.";
}
};
int main()
{
int a;
cout<<"Enter the Number:";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
return 0;
}
SAMPLE OUTPUT:
Enter the Number: 10
10 is Not Prime Numbers.
Enter the Number:7
7 is Prime Number.
RESULT:
Thus the program successfully completed and output verified.
2. FUNCTION OVERLOADING
AIM:
Write a program in C++ to implementing function overloading (Find area/volume of
rectangle, circle, sphere, cylinder, cone etc).
ALGORITHM:
Step 1: Start the program
Step 2: Define the pi value before class define.
Step 3: Initialize the variable and methods are area(int) with parameter in the class fn.
Step 4: Choose switch case condition for calculate the value of area of circle, triangle and
rectangle.
Step 5: Calculate the value of circle,triangle and rectangle in their methods.
Step 6: Methods call from main methods for all the methods with parameter value.
Step 7: Display the value on the screen of program.
Step 8: Stop the program.
PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
cout<<”Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
SAMPLE OUTPUT:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
RESULT:
Thus the program successfully completed and output verified.
OPERATOR OVER LOADING
AIM:
Write the program in C++ to implementing operator over loading ( Addition,
subtraction, multiplication of matrices)
ALGORITHM:
Step1: Start the program.
Step 2: Declare the data matrix variable, methods in matrix class getdata() method.
Step 3: Function to check whether the order of matrix are same or not
Step 4: function to read data for matrix.
Step 5: function to add two and subtraction matrix using operator+ and operator-.
Step 6: function to display the contents of the matrix value.
Step 7: stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class matrix
{
private:long m[5][5];
int row;int col;
public:void getdata();
int operator ==(matrix);
matrix operator+(matrix);
matrix operator-(matrix);
friend ostream & operator << (ostream &,matrix &);
};
/* function to check whether the order of matrix are same or not */
int matrix::operator==(matrix cm)
{
if(row==cm.row && col==cm.col)
{
return 1;
}
return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
cout<<"enter the number of rows\n";
cin>>row;
cout<<"enter the number of columns\n";
cin>>col;
cout<<"enter the elements of the matrix\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>m[i][j];
}
}
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]+am.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]-sm.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to display the contents of the matrix */
ostream & operator <<(ostream &fout,matrix &d)
{
for(int i=0;i<d.col;i++)
{
for(int j=0;j<d.col;j++)
{
fout<<d.m[i][j];
cout<<" ";
}
cout<<endl;
}
return fout;
}
/* main function */
void main()
{
matrix m1,m2,m3,m4;
clrscr();
m1.getdata();
m2.getdata();
if(m1==m2)
{
m3=m1+m2;
m4=m1-m2;
cout<<"Addition of matrices\n";
cout<<"the result is\n";
cout<<m3;
cout<<"subtraction of matrices\n";
cout<<"The result is \n";
cout<<m4;
}
else
{
cout<<"order of the input matrices is not identical\n";
}
getch();
}
OUTPUT:
RESULT:
Thus the program successfully completed and output verified.
4. (a). SINGLE INHERITANCE
AIM:
Write the program in C++ to Implementing single, multiple, hierarchical inheritance.
ALGORITHM:
Step1: Start the program.
Step 2: Take three classess enter the employee details like eno,ename withi in get() method.
Step 3: within a salary class calculate the bp,hra,da,pf,np etc…
Step 4: within the display() method class display the details of bp,hra,da,pf,np
Step 5:all the method will be executing in the main() method.
Step6: stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the employee number:";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the Humen Resource Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Profitablity Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<
<"\n";
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
SAMPLE OUTPUT:
Enter the Number of employee: 1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300
E.No E.name des BP HRA DA PF NP
150 ram Manager 5000 1000 500 300 6200
RESULT:
Thus the program successfully completed and output verified.
4.(b). MULTIPLE INHERITANCE
AIM:
Write the program in C++ to implementing multiple inheritance.
ALGORITHM:
Step1: Start the program.
Step 2: Declare the student details like rollno, mark1, mark2 with in student class.
Step 3: Enter the value rollno, mark1,mark2 within a get() method in student class.
Step 4: Get the sports mark within the sports class class.
Step 5: Calculate the marks and average of student with in statement class.
Step6: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No: "<<rno<<"\n\tTotal: "<<tot;
cout<<"\n\tAverage: "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
SAMPLE OUTPUT:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total: 260
Average: 86.66
RESULT:
Thus the program successfully completed and output verified.
4.(c). HIERARCHICAL INHERITANCE
AIM:
Write the program in C++ to implementing hierarchical inheritance.
ALGORITHM:
Step1: Start the program.
Step 2: Declare data member like height and weight and get the value through the parameter in
polygon class.
Step 3: Calculate the area of triangle through areaR() method in rectangle class.
Step 4: Calculate area of triangle though areaR() in the triangle class .
Step 5: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class polygon
{
protected:
int width, height;
public:
void input(int x, int y)
{
width = x;
height = y;
}
};
class rectangle : public polygon
{
public:
int areaR()
{
return (width * height);
}
};
class triangle : public polygon
{
public:
int areaT()
{
return (width * height / 2);
}
};
void main ()
{
clrscr();
rectangle rect;
triangle tri;
rect.input(6,8);
tri.input(6,10);
cout <<"Area of Rectangle: "<<rect.areaR()<< endl;
cout <<"Area of Triangle: "<<tri.areaT()<< endl;
getch();
SAMPLE OUTPUT:
Area of Rectangle: 48
Area of triangle: 30
RESULT:
Thus the program successfully completed and output verified.
5. SEQUENTIAL FILE OPERATIONS USING ERROR
HANDLING FUNCTIONS.
AIM:
Write the program in C++ to implementing sequential file operations using error
handling functions.
ALGORITHM:
Step1: Start the program.
Step 2: create the warning() method with address variable.
Step 3: open the file to store in text file, str.open(“Example.txt”, ios∷ noreplace)
Step 4: if (!str) available then return value else null.
Step 5: after complete str.close().
Step 6: warning(ofstream &str).
Step 7: Display the text file contents of Example.txt.
Step 8: stop the program.
PROGRAM:
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
void warning(ofstreame&);
ofstream str;
clrscr();
str.open(“Example.txt”, ios∷ noreplace);
if(!str)
{
warning(str);
exit(1);
}
else
{
str<<“This text is written into the file example.txt’”;
if(!str)
{
warning(str);
exit(2);
}
str.close();
}
void warning(ofstream &str)
{
cout << endl <<“Unable to open file example.txt”;
cout <<endl <<“Error state =” <<str.rdstate();
cout <<endl <<“Good =” <<str.good();
cout <<endI <<“EOF =” <<str.eof();
cout <<endl <<“Fail =” <<str.fail();
cout <<end] <<“Bad+” <<str.bad();
getch();
}
OUTPUT:
RESULT:
Thus the program successfully completed and output verified.
6. ARRAY IMPLEMENTION OF STACK ADT
AIM:
To write a C++ program for stack using array implementation.
ALGORITHM:
Step 1. Define a array which stores stack elements..
Step 2. The operations on the stack are
a. PUSH data into the stack
b. POP data out of stack
Step 3. PUSH DATA INTO STACK
a. Enter the data to be inserted into stack.
b. If TOP is NULL
i. The input data is the first node in stack.
ii. The link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. TOP points to that node.
Step 4. POP DATA FROM STACK
a. If TOP is NULL
i. the stack is empty
b. If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from stack.
Step 5. The stack represented by linked list is traversed to display its content
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
main()
{
int ch;
stack st;
clrscr();
cout <<"\n1.push \n2.pop \n3.display \n4.exit";
while(1)
{
cout<<"\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT:
RESULT:
Thus the program successfully completed and output verified.
7. QUEUE ADT USING ARRAY
AIM:
To write a program in C++ to implement for Queue using array implementation.
ALGORITHM:
Step1. Define a array which stores queue elements..
Step 2. The operations on the queue are
a. INSERT data into the queue
b. DELETE data out of queue
Step 3. INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL
i. The input data is the first node in queue.
ii. The link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. TOP points to that node.
Step 4. DELETE DATA FROM queue
a. If TOP is NULL
i. the queue is empty
b. If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from queue.
Step 5. The queue represented by linked list is traversed to display its content.
Step 6. Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
main()
{
int ch;
queue qu;
cout <<"\n1.insert 2.delete 3.display 4.exit";
while(1)
{
cout<<"\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet();
break;
case 3: qu.display();
break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT:
RESULT:
Thus the program successfully completed and output verified.
8. CONVERT A GIVEN INFIX EXPRESSION INTO ITS POSTFIX
EQUIVALENT
AIM:
Write the program in C++ to convert a given infix expression into its postfix Equivalent,
Implement the stack using an array.
ALGORITHM:
Step.1: Get an infix expression.
Step.2: Scan the expression from left to right.
Step.3: If any operands come display it.
Step.4: If the incoming symbol in a operator and has more priority then the symbol into the
stack.
Step.5: If the incoming operator has less priority than the stack symbol then copy the symbol at
the top of the stack and then print until the condition becomes false and push the
following operator on the stack.
Step.6: If the symbol is ‘)’ then copy operators from top of the stack. Deletion opening
parenthesis is from top of the stack.
Step.7: Stop the process.
PROGRAM:
#include<iostream.h>
#include<cstring.h>
#include<stack.h>
int getWeight(char ch) {
switch (ch) {
case '/':
case '*': return 2;
case '+':
case '-': return 1;
default : return 0;
}
}
// convert infix expression to postfix using a stack
void infix2postfix(char infix[], char postfix[], int size) {
stack<char> s;
int weight;
int i = 0;
int k = 0;
char ch;
// iterate over the infix expression
while (i < size) {
ch = infix[i];
if (ch == '(') {
// simply push the opening parenthesis
s.push(ch);
i++;
continue;
}
if (ch == ')') {
while (!s.empty() && s.top() != '(') {
postfix[k++] = s.top();
s.pop();
}
// pop off the opening parenthesis also
if (!s.empty()) {
s.pop();
}
i++;
continue;
}
weight = getWeight(ch);
if (weight == 0) {
postfix[k++] = ch;
}
else {
if (s.empty()) {
s.push(ch);
}
else {
while (!s.empty() && s.top() != '(' &&
weight <= getWeight(s.top())) {
postfix[k++] = s.top();
s.pop();
}
s.push(ch);
}
}
i++;
}
while (!s.empty()) {
postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0; // null terminate the postfix expression
}
// main
int main() {
char infix[] = "A*(B+C)/D";
int size = strlen(infix);
char postfix[size];
infix2postfix(infix,postfix,size);
cout<<"\nInfix Expression :: "<<infix;
cout<<"\nPostfix Expression :: "<<postfix;
cout<<endl;
return 0;
}
OUTPUT
RESULT:
Thus the program successfully completed and output verified.
9. BINARY SEARCH TREE AND TREE TRAVERSAL
AIM:
Write a program in C++ to implement Binary search tree recursive traversals (in-order,
pre-order, post-order).
ALGORITHM:
Step.1: Start the program
Step.2: Declare function create (), search (), delete (), Display ().
Step.3: Create a structure for a tree contains left pointer and right pointer.
Insert an element is by checking the top node and the leaf node and the operation
will be performed.
Step.4: Deleting an element contains searching the tree and deleting the item.
Step.5: Display the Tree elements.
Step.6: Stop the program
PROGRAM:
# include <iostream.h>
# include <cstdlib>
using namespace
std; struct node
{
int info;
struct node *left;
struct node *right;
}*root;
class
BST
{
public:
void find(int, node **, node **);
void insert(int);
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *); void
inorder(node *); void
postorder(node *); void
display(node *, int); BST()
{
root = NULL;
}
};
int main()
{
int choice, num;
BST bst; node
*temp; while (1)
{
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}}}
void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL){
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl; return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
free(location);
}
void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}
OUTPUT:
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 8
Root Node is Added
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
Root->: 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 9
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
9
Root->: 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 5
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
9
Root->:
8
5
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 11
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
11
9
Root->:
8
5
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 3
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 7
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
11
9
Root->:
8
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 10
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
11
10
9
Root->:
8
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 2
Enter the number to be deleted : 10
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
11
9
Root->:
8
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 3
Inorder Traversal of BST:
3 5 7 8 9 11
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 4
Preorder Traversal of BST:
8 5 3 7 9 11
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 5
Postorder Traversal of BST:
3 7 5 11 9 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 2
Enter the number to be deleted : 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
11
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 10
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
11
10
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 15
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
15
11
10
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 4
Preorder Traversal of BST:
9 5 3 7 11 10 15
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 5
Postorder Traversal of BST:
3 7 5 10 15 11 9
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:
15
11
10
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 7
RESULT:
Thus the program successfully completed and output verified.
10. POLYNOMIAL ADDITION USING LINKED LIST
AIM:
Write the program in C to implement Polynomial addition using linked list
ALGORITHM:
Step 1: start the program.
Step 2: loop around all values of linked list and follow step 2& 3.
Step 3: if the value of a node’s exponent. is greater copy this node to result node and head
towards the next node.
Step 4: if the values of both node’s exponent is same add the coefficients and then copy the
added value with node to the result.
Step 5: Print the resultant node.
Step 6: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
using namespace std;
struct Node{
int coeff;
int pow;
struct Node *next;
};
void create_node(int x, int y, struct Node **temp){
struct Node *r, *z;
z = *temp;
if(z == NULL){
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
} else {
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
void polyadd(struct Node *p1, struct Node *p2, struct Node *result){
while(p1->next && p2->next){
if(p1->pow > p2->pow){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
else if(p1->pow < p2->pow){
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
} else {
result->pow = p1->pow;
result->coeff = p1->coeff+p2->coeff;
p1 = p1->next;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
while(p1->next || p2->next){
if(p1->next){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
if(p2->next){
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
}
void printpoly(struct Node *node){
while(node->next != NULL){
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}
int main()
{
struct Node *p1 = NULL, *p2 = NULL, *result = NULL;
create_node(41,7,&p1);
create_node(12,5,&p1);
create_node(65,0,&p1);
create_node(21,5,&p2);
create_node(15,2,&p2);
printf("polynomial 1: ");
printpoly(p1);
printf("\npolynomial 2: ");
printpoly(p2);
result = (struct Node *)malloc(sizeof(struct Node));
polyadd(p1, p2, result);
printf("\npolynomial after adding p1 and p2 : ");
printpoly(result);
return 0;
}
OUTPUT:
polynomial 1: 41x^7 + 12x^5 + 65x^0
polynomial 2: 21x^5 + 15x^2
polynomial after adding p1 and p2 : 41x^7 + 33x^5 + 15x^2 + 65x^0
RESULT:
Thus the program successfully completed and output verified.