177
PRACTICAL FILE PRESENTED BY:- RINKY SACHDEVA

C++ Programs

Embed Size (px)

Citation preview

Page 1: C++ Programs

PRACTICAL FILE

PRESENT ED BY:-

RINKY SACHDEVA BSc. Comp. Sc.(H) Ist year(IInd

sem) Roll No. :-

Page 2: C++ Programs

206-Software Lab based on 201 (C++ and Data Structures)

Questions

Ques.1: WAP to perform the following operations on integer:i) To check if number is prime or not.ii) To print the sum and product of the number’s digits.iii) To reverse the number.

Ques.2: Write a function that checks if a string is Palindrome or not. Use this function to check user entered strings.

Ques.3: WAP to perform the following on user entered array:i) Print the even valued elements.ii) Print the odd valued elements.iii) Calculate and print the sum and average of the

elements of the array.iv) Print the maximum and minimum elements of the

array.v) Sort the array.vi) Remove the duplicates from the ordered array.vii) Compact the array.viii) Print the array in reverse order.

The program should present a menu to user and ask for one of the options. The menu should also include options to re-enter the array and quit the program.

Ques.4: Create Matrix class. Write a menu-driven program to perform the following operations(2-D array implementation):

i) Sumii) Differenceiii) Productiv) Transpose

Ques.5: Write a program to search an element from a list. Give user the option to perform Linear or Binary Search. Use Template functions.

Ques.6: WAP to sort a list of elements. Give user the option to perform sorting using Insertion sort, Bubble sort or Selection sort.

Page 3: C++ Programs

Ques.7: WAP that prints a table indicating the no. of occurrences of each letter of alphabet in the text entered as command line arguments.

Ques.8: Write a macro that calculates the area of a circle. WAP to use it.

Ques.9: Write a macro that swaps two numbers. WAP to use it.

Ques.10: WAP to perform the following operations on strings:i) Show address of each character in string.ii) Concatenate 2 string without using strcat function.iii) Concatenate 2 string using strcat function.iv) Compare 2 strings.v) Calculate the length of the string(use pointers).vi) To convert all lowercase characters to uppercase.vii) To convert all uppercase characters to lowercase.viii) Calculate number of vowels.ix) To reverse the string.

Ques.11: Create a class Rational for performing arithmetic with rational nos.Use integer variables to represent numerator and denominator. Write a constructor method that enables an object of class to be initialized when it is declared. Provide an argument constructor with default values in case no intializers are provided.Provide a copy constructor also. Write methods for:

i) Addition, overload ‘+’ operatorii) Subtraction, overload ‘-’ operatoriii) Multiply, overload ‘*’ operatoriv) Division, overload ‘/’ operatorv) Increment, overload ‘++’ operator(both prefix &

postfix)vi) Decrement, overload ‘--’ operator(both prefix &

postfix)vii) Overload operator ‘==’ (to check equality of two

rational nos.) as a friend functionviii) Overload assignment operatorix) Printing in the form a/b.

WAP to test the above class.

Page 4: C++ Programs

Ques.12: Create a class Triangle. Include overloaded functions for calculating area. Overload assignment operator and equality operator.

Ques.13: Write a macro to test whether a year is leap or not.

Ques.14: Create the Person class. Create some objects of this class (by taking information from the user). Inherit the class Person to create Employee class. Maintain Employee information, i.e. create, display and delete employee objects and calculate their wages. (Show Runtime Polymorphism).

Ques.15: Create a class Box containing length, breadth and height. Include following methods in it:

i) Calculate surface area.ii) Calculate Volume.iii) Increment, overload ‘++’ operator(both prefix &

postfix)iv) Decrement, overload ‘--’ operator(both prefix &

postfix)v) Overload operator ‘==’ (to check the equality of two

boxes)vi) Overload assignment operatorvii) Check if it is a cube or a cuboid.

WAP to test the above class.

Ques.16: Implement Linked List using templates. Include functions for insertion, deletion and search of a number, reverse the list and concatenate two linked lists (include a function and also overload operator +).

Ques.17: Implement doubly linked list using templates. Include functions for insertion, deletion and search of a number, reverse the list.

Ques.18: Implement circular linked list using templates. Include functions for insertions, deletion and search of a number, reverse the list.

Page 5: C++ Programs

Ques.19: Perform Stack operations using Linked List implementation.

Ques.20: Perform Stack operations using Array implementation, use templates.

Ques.21: Perform Queues operations using Circular Array implementation, use templates.

Ques.22: Create and perform the different operations on double ended queues using linked list implementation.

Ques.23: WAP to scan a polynomial using linked list and add two polynomials.

Ques.24: WAP to calculate factorial and to compute the factors of a given no.

i) using iterationii) using recursion

Ques.25: WAP to display fibonaccei series i) using iterationii) using recursion

Ques.26: WAP to calculate GCD of 2 numbers iii) using iterationiv) using recursion

Ques.27: WAP to create a Binary Search Tree and include following operations on tree:

i) Insertionii) Deletion by copyingiii) Deletion by Mergingiv) Serach a no. in BSTv) Display its preorder, postorder and inorder traversals.vi) Display its level by level traversals.vii) Count the non-leaf nodes and leaf nodes.viii) Display height of tree.ix) Create a mirror image of tree.

Page 6: C++ Programs

Ques.28: WAP to convert the Sparse Matrix into non-zero form and vice-versa.

Quse.29: WAP to reverse the order of the elements in the stack using additional stack.

Ques.30: WAP to reverse the order of the elements in the stack using additional queue.

Ques.31: WAP to implement Diagonal Matrix using one-dimensional array.

Ques.32: WAP to implement Lower triangular matrix using one-dimensional array.

Ques.33: WAP to implement Upper triangular Matrix using one-dimensional array.

Ques.34: WAP to implement Symmetric Matrix using one-dimensional array.

Page 7: C++ Programs

/*Question no.1

WAP to perform following operations on integer created as Commamnd Line Arguments: (i) To check if a number is prime or not. (ii) To print the sum and product of the number's digits. (iii) To reverse the number.

----------------------------------------------------------------------------------------------------------*/

#include<iostream.h>#include<conio.h>

class a{

public:void prime(int);void sum(int);void reverse(int);

};

void a::prime(int n){

int j,r;j=n;for(int i=0;i<=j;i++){

if(j%i==0)r=0;

elser=1;

}if(r==0)

cout<<"The number is not prime";else

if(r==1)cout<<"The number is prime";

}void a::sum(int n){

int j;j=n;int s=0,no,p=1;

do{

Page 8: C++ Programs

no=j%10;s=s+no;p=p*no;j=j/10;

}while(j!=0);cout<<"The sum is "<<s<<endl<<"The product is "<<p;

}void a::reverse(int n){

int j;j=n;int no,rev=0;do{

no=j%10;rev=(rev*10)+no;j=j/10;

}while(j!=0);cout<<"The reversed number is : "<<rev;

}void main(){

clrscr();int num;cout<<"Enter a number"<<endl;cin>>num;a obj;obj.prime(num);obj.sum(num);obj.reverse(num);getch();

}//----------------------------------------------------------------------------------------------------------/*OUTPUT

1234567

is not prime

Sum IS:28Product:5040Reversed Number:7654321----------------------------------------------------------------------------------------------------------*/

Page 9: C++ Programs

/*no.2 program to check whether user given string is paliandrome or not

----------------------------------------------------------------------------------------------------------*/#include<iostream.h>#include<string.h>#include<conio.h>class Test{

char c[50],c1[50];public:void read();void check();

}ob;void Test::read(){

cout<<"Enter string:";gets(c);//cout<<c;

}void Test::check(){

int l=strlen(c),e=0;for(int i=l-1;i>=0;i--)c1[e++]=c[i];c1[e]='\0';if(strcmp(c,c1))

cout<<"String is not Palindrome";elsecout<<"String is Palindrome";

}int main(){

ob.read();ob.check();getch();

}/*----------------------------------------------------------------------------------------------------------OUTPUT

Enter string:malayalamString is Palindrome

Enter string:god is greatString is not Palindrome----------------------------------------------------------------------------------------------------------*/

Page 10: C++ Programs

//Q3.WAP to perform certain operations on user entered array.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<string.h>#include<cctype>

//class definition

class Test{

int arr[100],size;public:

void read();void print_even();void print_odd();void print_Sum_Avg();void sort();void print_max_min();void remove_duplicate();void compact(int,int);void reverse_array();void show();

}ob;

//function definitions

void Test::read(){

cout<<"Enter size of array:";cin>>size;cout<<"Enter elements of array\n";for(int i=0;i<size;i++){

cout<<"Element "<<i+1<<":";cin>>arr[i];

}}

void Test::print_even(){

cout<<"Even elements are\n";for(int i=0;i<size;i++)

if(arr[i]%2==0) cout<<arr[i]<<"\n";

Page 11: C++ Programs

}

void Test::print_odd(){

cout<<"Odd elements are\n";for(int i=0;i<size;i++)

if(arr[i]%2!=0) cout<<arr[i]<<"\n";}

void Test::print_Sum_Avg(){

int sum=0;float avg=0.0;for(int i=0;i<size;i++)

sum+=arr[i];avg=(float)sum/size;cout<<"Sum of elements of array:"<<sum<<"\n";cout<<"Average of elements of array:"<<avg<<"\n";

}

void Test::sort(){

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

for(int j=size-1;j>=i;j--){

if(arr[j-1]>arr[j]){

flag=arr[j-1];arr[j-1]=arr[j];arr[j]=flag;

}}

cout<<"Sorted array\n";for(int i=0;i<size;i++)

cout<<arr[i]<<"\n";}

void Test::print_max_min(){

int max=arr[0],min=arr[0];for(int i=0;i<size;i++){

if(arr[i]>max) max=arr[i];if(arr[i]<min)min=arr[i];

}

Page 12: C++ Programs

cout<<"Maximum element:"<<max<<"\n";cout<<"Minimum element:"<<min<<"\n";

}

void Test::remove_duplicate(){

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

if(arr[i]==arr[j]){

size-=1;for(int k=j;k<size;k++)arr[k]=arr[k+1];

}cout<<"Duplicate element removed\n";

}

void Test::compact(int start,int end){

register int i;for(i=end+1;i<size;i++,start++)arr[start]=arr[i];

for(;start<size;start++)arr[start]=0;

}

void Test::reverse_array(){

cout<<"Reversed array\n";for(int i=size-1;i>=0;i--)

cout<<arr[i]<<"\n";}

void Test::show(){

for (int i=0;i<size;i++)cout<<"Element :"<<i+1<<": "<<arr[i]<<"\n";

}

int main(){

int a,choice;ob.read();char ch='c';do{

Page 13: C++ Programs

cout<<"\n***----------** MENU **----------***\n\n";cout<<"1 :Re-Enter array\n";cout<<"2 :Print Even elements of array\n";cout<<"3 :Print Odd elements of array\n";cout<<"4 :Print Sum and Average of array elements\n";cout<<"5 :Print Maximum and Minimum element of array\n";cout<<"6 :Sort Array\n";cout<<"7 :Remove Duplicate elements from array\n";cout<<"8 :Compact array\n";cout<<"9 :Print Reverse of Array\n";cout<<"10 :To quit the program\n";cout<<"\nEnter Your Choice:";cin>>choice;

switch(choice) {

case 1:{

ob.read();break;

} case 2: {

ob.print_even();break;

}case 3:

{ob.print_odd();break;

}case 4:{

ob.print_Sum_Avg();break;

}case 5:{

ob.print_max_min();break;

}case 6:{

ob.sort();break;

}

Page 14: C++ Programs

case 7: {

ob.remove_duplicate(); ob.show();

break;}case 8:{

int start, end; cout<<" Enter starting and end point of compactoin:"; cin>>start>>end;

ob.compact(start,end); ob.show();

break; }

case 9:{

ob.reverse_array();break;

}case 10:{

cout<<"Final array\n";ob.show();cout<<"To Quit the program press 'q' and 'c' to

continue\n"; cin>>ch; if(ch=='q') { cout<<"You Have Quit the Program!!\n"; break; } if(ch=='c')

break; } default: {

cout<<"\n You Have Entered a Wrong Choice pls Try Again!!!!!";

}}

}while(ch!='q');getch();return 0;

}

Page 15: C++ Programs

/*----------------------------------------------------------------------------------------------------------OUTPUTEnter size of array:6Enter elements of arrayElement 1:1Element 2:3Element 3:2Element 4:5Element 5:4Element 6:6

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Enter Your Choice:2Even elements are246

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Enter Your Choice:3Odd elements are1

Page 16: C++ Programs

35

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Enter Your Choice:4Sum of elements of array:21Average of elements of array:3.5

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Enter Your Choice:5Maximum element:6Minimum element:1

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array

Page 17: C++ Programs

8 :Compact array9 :Print Reverse of Array10 :To quit the programEnter Your Choice:6Sorted array123456

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Enter Your Choice:7Duplicate element removedElement :1: 1Element :2: 2Element :3: 3Element :4: 4Element :5: 5Element :6: 6

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Page 18: C++ Programs

Enter Your Choice:8 Enter starting and end point of compactoin:23Element :1: 1Element :2: 2Element :3: 5Element :4: 6Element :5: 0Element :6: 0

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the program

Enter Your Choice:9Reversed array006521

***----------** MENU **----------***

1 :Re-Enter array2 :Print Even elements of array3 :Print Odd elements of array4 :Print Sum and Average of array elements5 :Print Maximum and Minimum element of array6 :Sort Array7 :Remove Duplicate elements from array8 :Compact array9 :Print Reverse of Array10 :To quit the programEnter Your Choice:10

Page 19: C++ Programs

Final arrayElement :1: 1Element :2: 2Element :3: 5Element :4: 6Element :5: 0Element :6: 0To Quit the program press 'q' and 'c' to continueqYou Have Quit the Programme!!!!!!!!!!!!

--------------------------------------------------------------------------------------------------------- */

Page 20: C++ Programs

//Q4 Perform Matrix Operations like ADD, SUBTRACT, MULTIPLY And TRANSPOSE

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<stdio.h>

//class definitionclass Test{

int a[50][50],b[50][50],c[50][50],m,n,p,q;public:

void read();void add();void difference();void multiply();void transpose();

}ob;

//outline function definitions

void Test::read(){

cout<<"Enter Dimension of Ist matrix\n"; cout<<"Dimension 1:";

cin>>m;cout<<"Dimension 2:";cin>>n;cout<<"\nEnter elements of Ist Matrix\n";int s=m*n;for(int i=0,s=1;i<m;i++)

for(int j=0;j<n;j++,s++){

cout<<"Element "<<s<<":";cin>>a[i][j];

}cout<<"Enter Dimension of IInd Matrix\n";cout<<"Dimension 1:";cin>>p;cout<<"Dimension 2:";cin>>q;cout<<"\nEnter elements of IInd Matrix\n";s=p*q;for(int i=0,s=1;i<p;i++)

Page 21: C++ Programs

for(int j=0;j<q;j++,s++){

cout<<"Element "<<s<<":"; cin>>b[i][j];

}cout<<"\nIst Matrix\n";for(int i=0;i<m;i++){

for(int j=0;j<n;j++) cout<<a[i][j]<<" "; cout<<"\n"; }

cout<<"\nIInd Matrix\n"; for(int i=0;i<p;i++) { for(int j=0;j<q;j++) cout<<b[i][j]<<" "; cout<<"\n"; }}

void Test::add(){

if(m==p&&n==q)for(int i=0;i<m;i++)

for(int j=0;j<n;j++)c[i][j]=a[i][j]+b[i][j];

cout<<"Sum Matrix\n";for(int i=0;i<m;i++){

for(int j=0;j<n;j++)cout<<c[i][j]<<" ";

cout<<"\n";}else

cout<<"Matrix are not compatible for Addition\n";}

void Test::difference(){

if(m==p&&n==q){

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

c[i][j]=a[i][j]-b[i][j];cout<<"Difference Matrix\n";

Page 22: C++ Programs

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

for(int j=0;j<n;j++)cout<<c[i][j]<<" ";

cout<<"\n";}

}else

cout<<"Matrix are not compatible for Subtraction\n";}

void Test::multiply(){

if(n==p){

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

for(int k=0;k<n;k++){

c[i][k]=0;c[i][k]=c[i][k]+(a[i][k]*b[k][j]);

}cout<<"Product Matrix\n";for(int i=0;i<m;i++){

for(int j=0;j<q;j++)cout<<c[i][j]<<" ";

cout<<"\n";}

}else

cout<<"Matrix are not compatible for Multiplication\n";}

void Test::transpose(){

cout<<"Transpose Of Ist Matrix\n";for(int i=0;i<n;++i){

for(int j=0;j<m;++j)cout<<a[i][j]<<" ";

cout<<"\n";}cout<<"Transpose Of IInd Matrix\n";for(int i=0;i<p;++i){

Page 23: C++ Programs

for(int j=0;j<q;++j)cout<<b[j][i]<<" ";

cout<<"\n";}

}

//Main function definition

int main(){

char choice,ch;ob.read();do{

cout<<"\n\n***----------** MENU **----------***\n\n";cout<<"1 :Re-Enter Matrix\n";cout<<"2 :Add the Matrix\n";cout<<"3 :Subtract the Matrix\n";cout<<"4 :Multiply the Matrix\n";cout<<"5 :Transpose the Matrix\n";cout<<"6 :To quit the program\n";cout<<"\nEnter Your Choice:";cin>>choice;clrscr();switch(choice){

case '1':{

ob.read();break;

}case '2':{

ob.add();break;

}case '3':{

ob.difference();break;

}

case '4':{

ob.multiply();

Page 24: C++ Programs

break; }

case ’5':{

ob.transpose();break;

}case '6':

{ cout<<"Enter 0 to quit the program and any key to

continue:"; cin>>ch;

break; }

default:cout<<"\n You Have Entered a Wrong Choice pls Try

Again!!!!!";

}

}while(ch!='0');cout<<"\nYou Have Quit the Program!!\n";getch();return 0;

}

/*----------------------------------------------------------------------------------------------------------OUTPUT

Enter Dimension of Ist matrixDimension 1:3Dimension 2:2

Enter elements of Ist Matrix

Element 1:1Element 2:2Element 3:3Element 4:4Element 5:5Element 6:6Enter Dimension of IInd MatrixDimension 1:2Dimension 2:3

Page 25: C++ Programs

Enter elements of IInd MatrixElement 1:6Element 2:5Element 3:4Element 4:3Element 5:2Element 6:1

Ist Matrix1 23 45 6

IInd Matrix6 5 43 2 1

***----------** MENU **----------***

1 :Re-Enter Matrix2 :Add the Matrix3 :Subtract the Matrix4 :Multiply the Matrix5 :Transpose the Matrix6 :To quit the program

Enter Your Choice:2Matrix are not compatible for Addition

***----------** MENU **----------***

1 :Re-Enter Matrix2 :Add the Matrix3 :Subtract the Matrix4 :Multiply the Matrix5 :Transpose the Matrix6 :To quit the program

Enter Your Choice:3

Page 26: C++ Programs

Matrix are not compatible for Subtraction

***----------** MENU **----------***

1 :Re-Enter Matrix2 :Add the Matrix3 :Subtract the Matrix4 :Multiply the Matrix5 :Transpose the Matrix6 :To quit the program

Enter Your Choice:4Product Matrix4 2 12 4 20 6

***----------** MENU **----------***

1 :Re-Enter Matrix2 :Add the Matrix3 :Subtract the Matrix4 :Multiply the Matrix5 :Transpose the Matrix6 :To quit the program

Enter Your Choice:5Transpose Of Ist Matrix1 3 52 4 6Transpose Of IInd Matrix6 3 5 2 4 1

***----------** MENU **----------***

1 :Re-Enter Matrix2 :Add the Matrix3 :Subtract the Matrix4 :Multiply the Matrix

Page 27: C++ Programs

5 :Transpose the Matrix6 :To quit the program

Enter Your Choice:6Enter 0 to quit the program and any key to continue:0

You Have Quit the Program!!----------------------------------------------------------------------------------------------------------*/

Page 28: C++ Programs

//Q5 Perform Search Using Linear and Binary Search

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

class search{

int item,sz,arr[30];public:

void Lsrch();void Bsrch();void readarray();

}ob;

void search::Lsrch(){

cout<<"\n";cout<<"\n\t Enter the item to be searched:-";cin>>item;for(int i=0;i<sz;i++)

cout<<arr[i]<<" ";cout<<"\n\t Proceeding with Lsearch ";int i,flag=0;for(i=0;i<sz;i++){

if(arr[i]==item){

cout<<"\n\t The item "<<item<<" is at index: "<<i<<" & position:"<<i+1;

flag=1; }

}if(flag==0)

cout<<"\n\t No match is found.";}

void search::Bsrch(){

cout<<"\n";cout<<"\n\t Enter the item to be searched:-";cin>>item;for(int i=0;i<sz;i++)

Page 29: C++ Programs

cout<<arr[i]<<" ";cout<<"\n\t Proceeding with Bsearch ";int beg=0,mid=0,last=0,temp=0,flag=0;last=sz-1;for(int i=0;i<sz-1;i++) //To sort the given array.

for(int j=i+1;j<sz;j++)if(arr[i]>arr[j]){

temp=arr[i];arr[i]=arr[j];arr[j]=temp;

}out<<"\nSorted array\n";for(int i=0;i<sz;i++)

cout<<arr[i]<<" "; while(beg<=last) {

mid=((beg+last)/2);if(item==arr[mid]){

int i=mid;flag=1;cout<<"\n\t The item "<<item<<" is at index: "<<i<<" &

position: "<<i+1; break; }

if(item>arr[mid])beg=mid+1;

elselast=mid-1;

}if(flag==0)

cout<<"\n\t No match is found.";}

void search::readarray(){

cout<<"Size of array:";cin>>sz;cout<<"Enter Elements of array\n";for(int i=0;i<sz;i++)

cin>>arr[i];}

Page 30: C++ Programs

void main(){

clrscr();int ch=0;ob.readarray();while(ch!=3){

cout<<"\n\t ************************************************** ";cout<<"\n\t Select the technique by which you want to proceed: ";cout<<"\n\t ************************************************** ";cout<<"\n\t 1.Read Array ";cout<<"\n\t 2.Linear Search ";cout<<"\n\t 3.Binary Search ";cout<<"\n\t 4.Exit ";cout<<"\n\t ************************************************** ";cout<<"\n\t ************************************************** ";cout<<"\n";cout<<"\n\t Enter your choice: :";cin>>ch;clrscr();switch(ch){

case 1:{

ob.readarray(); break;

}case 2:{

ob.Lsrch();break;

}case 3:{

ob.Bsrch();break;

}case 4:{

cout<<"U have Exit the programme.........."; getch();

exit(0);break;

}default:

cout<<"\n\t Wrong choice.";

Page 31: C++ Programs

}getch();clrscr();

} }

/*----------------------------------------------------------------------------------------------------------OUTPUT

Size of array:6Enter Elements of array321546

************************************************** Select the technique by which you want to proceed: ************************************************** 1.Read Array 2.Linear Search 3.Binary Search 4.Exit ************************************************** **************************************************

Enter your choice: :2

Enter the item to be searched:-23 2 1 5 4 6 Proceeding with Lsearch The item 2 is at index: 1 & position: 2

************************************************** Select the technique by which you want to proceed: ************************************************** 1.Read Array 2.Linear Search 3.Binary Search 4.Exit ************************************************** **************************************************

Page 32: C++ Programs

Enter your choice: :3

Enter the item to be searched:-63 2 1 5 4 6 Proceeding with BsearchSorted array1 2 3 4 5 6 The item 6 is at index: 5 & position: 6

----------------------------------------------------------------------------------------------------------*/

Page 33: C++ Programs

//Q6 Perform Array sort using Bubble,Selection and Insertion Sort

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

class sort{

int i,size,arr[100];public:

void bubsrt();void selsrt();void inssrt();void readarray();

}ob;

void sort::bubsrt(){

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

for(int j=0;j<size-i-1;j++)if(arr[j]>arr[j+1]){

temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;

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

cout<<"\n"<<arr[i];}

void sort::selsrt(){

int temp=0;int min=0;for(i=0;i<size;i++){

min=arr[i];for(int j=i+1;j<size;j++)

if(arr[j]<min) { temp=arr[j]; arr[j]=min; min=temp;

Page 34: C++ Programs

} cout<<"\n"<<min;}

}

void sort::inssrt(){

int temp,j=0;for(i=1;i<size;i++){

temp=arr[i];j=i-1;while((temp<arr[j])&&(j>=0)){

arr[j+1]=arr[j];j--;

}arr[j+1]=temp;

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

cout<<"\n"<<arr[i];}

void sort::readarray(){

cout<<"\n Enter the size of array ";cin>>size;cout<<"\n Enter the array elements ";cout<<"\n";for(i=0;i<size;i++)

cin>>arr[i]; cout<"Array Entered is:"; for(i=0;i<size;i++) cout<<arr[i]<<" ";}

void main(){

clrscr();int ch;clrscr();ob.readarray();do{

cout<<"\n\n\t********************************************* ";cout<<"\n\t Select the technique You want to proceed with ";

Page 35: C++ Programs

cout<<"\n\t ********************************************* ";cout<<"\n\t 1.Read Array ";cout<<"\n\t 2.Bubble sort ";cout<<"\n\t 3.Selection sort ";cout<<"\n\t 4.Insertion sort ";cout<<"\n\t 5.Exit ";cout<<"\n\t ********************************************* ";cout<<"\n\t ********************************************* ";cout<<"\n";cout<<"\n\t Enter your choice(1-5) ";cin>>ch;switch(ch)

{ case 1:

{ ob.readarray(); break;

}case 2:{

cout<<"\n"; cout<<"\n The array sorted through Bubble sort is: "; ob.bubsrt(); break;

}

case 3:{

cout<<"\n"; cout<<"\n The array sorted through Selection sort is:

"; ob.selsrt(); break;

}case 4:{

cout<<"\n";cout<<"\n The array sorted through Insertion sort is: ";ob.inssrt();break;

}case 5:{

cout<<"U Have Quit the program!!!!";getch();exit(0);

Page 36: C++ Programs

break;}default:

cout<<"\n\t Wrong Selection .Try again(1-4)";}

}while(ch!=5);

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

Enter the size of array 6

Enter the array elements1325641 3 2 5 6 4

********************************************* Select the technique You want to proceed with ********************************************* 1.Read Array 2.Bubble sort 3.Selection sort 4.Insertion sort 5.Exit ********************************************* *********************************************

Enter your choice(1-5)2

The array sorted through Bubble sort is:1234

Page 37: C++ Programs

56

********************************************* Select the technique You want to proceed with ********************************************* 1.Read Array 2.Bubble sort 3.Selection sort 4.Insertion sort 5.Exit ********************************************* *********************************************

Enter your choice(1-5) 1

Enter the size of array 6

Enter the array elements23212565311123 21 25 65 31 11

********************************************* Select the technique You want to proceed with ********************************************* 1.Read Array 2.Bubble sort 3.Selection sort 4.Insertion sort 5.Exit ********************************************* *********************************************

Enter your choice(1-5) 3

The array sorted through Selection sort is:11

Page 38: C++ Programs

2123253165

********************************************* Select the technique You want to proceed with ********************************************* 1.Read Array 2.Bubble sort 3.Selection sort 4.Insertion sort 5.Exit ********************************************* *********************************************

Enter your choice(1-5) 1

Enter the size of array 5

Enter the array elements134132413 41 3 2 4

********************************************* Select the technique You want to proceed with ********************************************* 1.Read Array 2.Bubble sort 3.Selection sort 4.Insertion sort 5.Exit ********************************************* *********************************************

Enter your choice(1-5) 4

The array sorted through Insertion sort is:2

Page 39: C++ Programs

341341

********************************************* Select the technique You want to proceed with ********************************************* 1.Read Array 2.Bubble sort 3.Selection sort 4.Insertion sort 5.Exit ********************************************* *********************************************

Enter your choice(1-5)5U Hav Quit the programme!!!----------------------------------------------------------------------------------------------------------*/

Page 40: C++ Programs

//Q7.To print table indicating number of occurences of each character......

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<string.h>

class Test{

char a[100];public:

void readstring();void displaylist_lowercase();void displaylist_uppercase();

}ob;

void Test::readstring(){

cout<<"Enter String :";gets(a);cout<<"Entered String:";puts(a);

}

void Test::displaylist_lowercase(){

int l=strlen(a);for(int i=97;i<=122;i++){

int flag=0,ch;for(int j=0;j<l;j++){

ch=(int)a[j];if(i==ch)flag++;

}cout<<"\n"<<(char)i<<" "<<": "<<flag;flag=0;

}cout<<"\n";

}

void Test::displaylist_uppercase(){

int l=strlen(a);

Page 41: C++ Programs

for(int i=65;i<=90;i++){

int flag=0,ch;for(int j=0;j<l;j++){

ch=(int)a[j];if(i==ch)

flag++;}cout<<"\n"<<(char)i<<" "<<": "<<flag;flag=0;

}getch();

}

void main (){

clrscr();ob.readstring();ob.displaylist_lowercase();ob.displaylist_uppercase();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

Enter String :my name is XYANSJHS AJMAKEntered String:my name is XYANSJHS AJMAK

a : 1b : 0c : 0d : 0e : 1f : 0g : 0h : 0i : 1j : 0k : 0l : 0m : 2n : 1o : 0p : 0

Page 42: C++ Programs

q : 0r : 0s : 1t : 0u : 0v : 0w : 0x : 0y : 1z : 0

A : 3B : 0C : 0D : 0E : 0F : 0G : 0H : 1I : 0J : 2K : 1L : 0M : 1N : 1O : 0P : 0Q : 0R : 0S : 2T : 0U : 0V : 0W : 0X : 1Y : 1Z : 0

----------------------------------------------------------------------------------------------------------*/

Page 43: C++ Programs

//Q8.MACRO PROGRAM................... Finding Area of a circle

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#define AREA(a,b) (b)*(a)*(a)

void main(){

clrscr();int a;cout<<"\nENTER THE RADIUS OF THE CIRCLE:";cin>>a;cout<<"AREA OF CIRCLE IS:"<<AREA(a,3.14);getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

ENTER THE RADIUS OF THE CIRCLE:10AREA OF CIRCLE IS:314

----------------------------------------------------------------------------------------------------------*/

Page 44: C++ Programs

//Q9.SWAP TWO NUMBERS USING MACRO............................

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#define SWAP(a,b) (a=b)

void main(){

clrscr();int a,b,temp;cout<<"\n ENTER TWO NUMBERS:";cin>>a>>b;cout<<"\nNUMBERS BEFORE SWAP IS:"<<a<<" "<<b;temp=a;SWAP(a,b);SWAP(b,temp);cout<<"\nNUMBERS AFTER SWAP IS:"<<a<<" "<<b;getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

ENTER TWO NUMBERS:1023

NUMBERS BEFORE SWAP IS:10 23NUMBERS AFTER SWAP IS:23 10

----------------------------------------------------------------------------------------------------------*/

Page 45: C++ Programs

//Q10.WAP to perform certain operations on user entered STRING.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<string.h>

class strings{

char c[110],c1[50],c2[50];int l1,l2,l3;public:

void getstring();void concat1();void concat2();void compare();void lengthcal();void lower();void upper();void vowelcount();void reverse();

}ob;

void strings::getstring(){

cout<<"\nString 1:";gets(c1);cout<<"\nString 2:";gets(c2);

}void strings::concat1(){

int n=0,l=strlen(c1); for(int i=0;i<l;i++) { c[i]=c1[i]; n++; }

l=strlen(c2);for(int i=0;i<l;i++)c[n++]=c2[i];c[n]='\0';cout<<"\nConcatenated Strings without using Strcat :";puts(c);

}

Page 46: C++ Programs

void strings::concat2(){

int n=0,l=strlen(c1);for(int i=0;i<l;i++){

c[i]=c1[i];n++;

}c[n]='\0';strcat(c,c2);cout<<"\nConcatenated Strings using Strcat :";puts(c);

}

void strings::compare(){

int cp=strcmp(c1,c2);if(cp==0)

cout<<"\nStrings are same";else

cout<<"\nStrings are not same";}

void strings::lengthcal(){

char *p;int i=0,l=0;p=&c1[0];while(*(p+i)!='\0'){

i++;l++;

}cout<<"\nLength of Ist String :"<<l;p=&c2[0];while(*(p+i)!='\0'){

i++;l++;

}cout<<"\nLength of IInd String :"<<l;

}

void strings::vowelcount(){

int l=strlen(c1),flag=0;

Page 47: C++ Programs

for(int i=0;i<l;i++){

if(c1[i]=='a'||c1[i]=='A'||c1[i]=='e'||c1[i]=='E'||c1[i]=='i'|| c1[i]=='I'||c1[i]=='o'||c1[i]=='O'||c1[i]=='u'||c1[i]=='U') flag++; } cout<<"\nNumber of Vowels in the string I :"<<flag;

l=strlen(c2),flag=0;for(int i=0;i<l;i++){

if(c2[i]=='a'||c2[i]=='A'||c2[i]=='e'||c2[i]=='E'||c2[i]=='i'|| c2[i]=='I'||c2[i]=='o'||c2[i]=='O'||c2[i]=='u'||c2[i]=='U')

flag++;}cout<<"\nNumber of Vowels in the string II :"<<flag;

}

void strings::lower(){

int l=strlen(c1);cout<<"\nString 1 in Lower case:";for(int i=0;i<l;i++){if(c1[i]>=65&&c1[i]<=90)

c1[i]=c1[i]+32;cout<<c1[i];

}l=strlen(c2);cout<<"\nString 2 in Lower case:";for(int i=0;i<l;i++){

if(c2[i]>=65&&c2[i]<=90)c2[i]=c2[i]+32;

cout<<c2[i];}

}void strings::upper(){

int l=strlen(c1);cout<<"\nString 1 in Upper case:";for(int i=0;i<l;i++){

if(c1[i]>=97&&c1[i]<=122)c1[i]=c1[i]-32;

cout<<c1[i];}

Page 48: C++ Programs

l=strlen(c2);cout<<"\nString 2 in Upper case:";for(int i=0;i<l;i++){

if(c2[i]>=97&&c2[i]<=122)c2[i]=c2[i]-32;

cout<<c2[i];}

}

void strings::reverse(){

int j=0,i=0,l=strlen(c1);for(i=l-1;i>=0;i--)c[j++]=c1[i];c[j]='\0';cout<<"\nReversed String :";puts(c);j=0;l=strlen(c2);for(i=l-1;i>=0;i--)

c[j++]=c2[i];c[j]='\0';cout<<"\nReversed String :";puts(c);

}

void main(){

int a,choice;ob.read();char ch='c';do {

cout<<"\n***----------** MENU **----------***\n\n";cout<<"1 :Read Strings\n";cout<<"2 :Concatnate without STRCAT\n";cout<<"3 :Concatenate with STRACAT\n";cout<<"4 :Length of Strings\n";cout<<"5 :Convert 2 Lower\n";cout<<"6 :Convert 2 Upper\n";cout<<"7 :Compare The Strings\n";cout<<"8 :Count The Vowels\n";cout<<"9 :Reverse\n";cout<<"10 :To quit the program\n";

Page 49: C++ Programs

cout<<"\nEnter Your Choice:";cin>>choice;switch(choice){

case 1:{

ob.getstring();break;

}case 2:

{ob.concat1();break;

}case 3:

{ob.concat2();break;

}case 4:{

ob.lengthcal();break;

}case 5:{

ob.lower();break;

}case 6:{

ob.upper();break;

}case 7:{

b.compare();break;

}case 8:{

ob.vowelcount(); break;

}case 9:{

Page 50: C++ Programs

ob.reverse();break;

}case 10:{

cout<<"You Have Quit the Program!!\n";getch();exit(0);

}default:

{cout<<"\n You Have Entered a Wrong Choice pls Try

Again!!!!!";}

}}while(ch!='q');getch();

}

/*---------------------------------------------------------------------------

OUTPUT

String 1:god

String 2:GOD

***----------** MENU **----------***

1 :Read Strings2 :Concatnate without STRCAT3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:2

Concatenated Strings without using Strcat :godGOD

***----------** MENU **----------***

Page 51: C++ Programs

1 :Read Strings2 :Concatnate without STRCAT3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:3

Concatenated Strings using Strcat :godGOD

***----------** MENU **----------***

1 :Read Strings2 :Concatnate without STRCAT3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:4

Length of Ist String :3Length of IInd String :3***----------** MENU **----------***

1 :Read Strings2 :Concatnate without STRCAT3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper

Page 52: C++ Programs

7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:5

String 1 in Lower case:godString 2 in Lower case:god***----------** MENU **----------***

1 :Read Strings2 :Concatnate without STRCAT3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:6

String 1 in Upper case:GODString 2 in Upper case:GOD***----------** MENU **----------***

1 :Read Strings2 :Concatnate without STRCAT3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:7

Strings are same***----------** MENU **----------***

1 :Read Strings2 :Concatnate without STRCAT

Page 53: C++ Programs

3 :Concatenate with STRACAT4 :Length of Strings5 :Convert 2 Lower6 :Convert 2 Upper7 :Compare The Strings8 :Count The Vowels9 :Reverse10 :To quit the program

Enter Your Choice:10

You Have Quit the Program!!

----------------------------------------------------------------------------------------------------------*/

Page 54: C++ Programs

//Q11 Operator Overloading

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>

class rational{

int num,den;public:

rational(){

num=0; den=1; }

rational(int n,int d){

num=n;den=d;

}rational(rational &o) //copy constructor.............{

num=o.num;den=o.den;

}

friend rational operator+(rational,rational);friend rational operator-(rational,rational);friend rational operator*(rational,rational);friend rational operator/(rational,rational);rational operator++();rational operator--();rational operator+=(rational);void operator==(rational);void show();

void result();};

rational operator+(rational r3,rational r4){

rational r2;r2.num=((r3.num*r4.den)+(r4.num*r3.den));r2.den=r3.den*r4.den;return r2;

}

Page 55: C++ Programs

rational operator-(rational r3,rational r4){

rational r2;r2.num=((r3.num*r4.den)-(r4.num*r3.den));r2.den=r3.den*r4.den;return r2;

}

rational operator*(rational r3,rational r4){

rational r2;r2.num=r3.num*r4.num;r2.den=r3.den*r4.den;return r2;

}

rational operator/(rational r3,rational r4){

rational r2;r2.num=r3.num*r4.den;r2.den=r3.den*r4.num;return r2;

}

rational rational::operator++(){

num++;den++;return *this;

}

rational rational::operator--(){

num--;den--;return *this;

}

rational rational::operator+=(rational r5){

num=r5.num+num;den=r5.den+den;return *this;

}

Page 56: C++ Programs

void rational::operator==(rational r5){

if((num==r5.num) && (den==r5.den))cout<<"EQUAL";

elsecout<<"UNEQUAL";

}

void rational::show(){

cout<<num<<"/"<<den;}

void main(){

clrscr();int a,b;cout<<"Enter 2 Nos:";cin>>a>>b;rational r1(a,b),r3;rational r(r1);cout<<"\n CHECKING EQUALITY :";r==r1;r3= r+r1;cout<<"\nADDITION OF TWO RATIONAL NUMBERS:";r3.show();r3=r-r1;cout<<"\nSUBRACTION OF TWO RATIONAL NUMBERS:";r3.show();r3=r*r1;cout<<"\nMULTIPLICATION OF TWO RATIONAL NUMBERS:";r3.show();r3=r/r1;cout<<"\nDIVISION OF TWO RATIONAL NUMBERS:";r3.show();++r;cout<<"\n OVERLOAD ++ FOR FIRST RATIONAL NUMBERS:";r.show();--r;cout<<"\n OVERLOAD -- FOR FIRST RATIONAL NUMBERS:";r.show();r+=r1;cout<<"\n OVERLOAD += FOR FIRST RATIONAL NUMBERS:";r.show();

getch(); }

Page 57: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT

Enter 2 Nos:1033

CHECKING EQUALITY :EQUALADDITION OF TWO RATIONAL NUMBERS:660/1089SUBRACTION OF TWO RATIONAL NUMBERS:0/1089MULTIPLICATION OF TWO RATIONAL NUMBERS:100/1089DIVISION OF TWO RATIONAL NUMBERS:330/330 OVERLOAD ++ FOR FIRST RATIONAL NUMBERS:11/34 OVERLOAD -- FOR FIRST RATIONAL NUMBERS:10/33 OVERLOAD += FOR FIRST RATIONAL NUMBERS:20/66

----------------------------------------------------------------------------------------------------------*/

Page 58: C++ Programs

//Q12........(operator overloading & function overloading)............

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>

class triangle{

int base,height;public:triangle(){ }triangle(int b,int h){

base=b;height=h;

}void area();void area(float,float);void show();triangle operator=(triangle &r){

base=r.base;height=r.height;return *this;

}triangle operator-=(triangle r){

base=base-r.base;height=height-r.height;return *this;

}

};

void triangle::area(){

int temp=(base*height)/2; cout<<temp;}

void triangle::area(float l,float m){

cout<<l*m;}

Page 59: C++ Programs

void triangle::show(){

cout<<"\nBASE IS:"<<base<<"\nHEIGHT IS:"<<height;}void main(){

clrscr();triangle t1(10,20),t2(5,6),t3;float l3,m3;cout<<"\nENTER THE BASE & HEIGHT OF 3rd TRIANGLE:";cin>>l3>>m3;cout<<"AREA OF TRIANGLE 3 IS:";t3.area(l3,m3);cout<<"\nAREA OF TRIANGLE ONE IS:";t1.area();cout<<"\nAFTER OVERLOADING OPERATOR -=::TRIANGLE ONE

changed:";t1-=t2;t1.show();cout<<"\nAREA OF TRIANGLE ONE IS:";t1.area();cout<<"\nAFTER OVERLOADING OPERATOR =::TRIANGLE ONE

changed:";t1=t2;t1.show();cout<<"\nAREA OF TRIANGLE ONE IS:";t1.area();getch();

}/*----------------------------------------------------------------------------------------------------------OUTPUT

ENTER THE BASE & HEIGHT OF 3rd TRIANGLE:10.521.9AREA OF TRIANGLE 3 IS:229.95AREA OF TRIANGLE ONE IS:100AFTER OVERLOADING OPERATOR -=::TRIANGLE ONE changed:BASE IS:5HEIGHT IS:14AREA OF TRIANGLE ONE IS:35AFTER OVERLOADING OPERATOR =::TRIANGLE ONE changed:BASE IS:5HEIGHT IS:6AREA OF TRIANGLE ONE IS:15

----------------------------------------------------------------------------------------------------------*/

Page 60: C++ Programs

//Q13 MACRO IMPLEMENTATION....................Finding Leap Year

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#define LEAP(year) (year)%4

void main(){

clrscr();int leap,year;cout<<"\nENTER THE YEAR:";cin>>year;leap=LEAP(year);if(leap==0)

cout<<"\nLEAP YEAR";else

cout<<"\nNOT A LEAP YEAR";getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

ENTER THE YEAR:2009

NOT A LEAP YEAR

----------------------------------------------------------------------------------------------------------*/

Page 61: C++ Programs

//Q14 Implementing Inheritance and Virtual Function

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<stdio.h>

class person{

public:char name[80],age[5],sex[10],addr[150];virtual void input()=0;virtual void display()=0;

};

class employee:public person{

char department[50],post[50],salary[10];public:

void input();void display();

};

void employee::input(){

cout<<"\nENTER THE NAME OF PERSON:";gets(name);cout<<"\nENTER THE SEX OF THE PERSON:";gets(sex);cout<<"\nENTER THE AGE OF THE PERSON:";gets(age);cout<<"\nENTER THE ADDRESS OF THE PERSON:";gets(addr);cout<<"\nENTER THE DEPARTMENT IN WHICH PERSON WORKS:";gets(department);cout<<"\nENTER THE POST OF THE PERSON:";gets(post);cout<<"\nENTER THE MONTHLY SALARY OF THE PERSON:";gets(salary);

}

Page 62: C++ Programs

void employee::display(){

cout<<"\nNAME OF PERSON:"; puts(name);

cout<<"\nAGE OF THE PERSON:";puts(age);cout<<"\nSEX OF THE PERSON:";

puts(sex);cout<<"\nADDRESS OF THE PERSON:";

puts(addr);cout<<"\nDEPARTMENT IN WHICH PERSON WORKS:";

puts(department);cout<<"\nPOST OF THE PERSON:";

puts(post);cout<<"\nMONTHLY SALARY OF THE PERSON:";

puts(salary);}

void main(){

clrscr();person *b;employee e,e1;b=&e;b->input();getch();b=&e1;b->input();getch();clrscr();b=&e;b->display();getch();b=&e1;b->display();getch();

}

Page 63: C++ Programs

/*----------------------------------------------------------------------------------------------------------OUTPUT

ENTER THE NAME OF PERSON:xyz

ENTER THE SEX OF THE PERSON:male

ENTER THE AGE OF THE PERSON:22

ENTER THE ADDRESS OF THE PERSON:flat-22,abc st. new york

ENTER THE DEPARTMENT IN WHICH PERSON WORKS:stores

ENTER THE POST OF THE PERSON:sr. executive

ENTER THE MONTHLY SALARY OF THE PERSON:11000

ENTER THE NAME OF PERSON:aaa

ENTER THE SEX OF THE PERSON:female

ENTER THE AGE OF THE PERSON:21

ENTER THE ADDRESS OF THE PERSON:falt-102,bcc st. washington

ENTER THE DEPARTMENT IN WHICH PERSON WORKS:house keeping

ENTER THE POST OF THE PERSON:sr. manager

ENTER THE MONTHLY SALARY OF THE PERSON:21000

NAME OF PERSON:xyz

AGE OF THE PERSON:22

SEX OF THE PERSON:male

ADDRESS OF THE PERSON:flat-22,abc st. new york

DEPARTMENT IN WHICH PERSON WORKS:stores

POST OF THE PERSON:sr. executive

MONTHLY SALARY OF THE PERSON:11000

Page 64: C++ Programs

NAME OF PERSON:aaa

AGE OF THE PERSON:21

SEX OF THE PERSON:female

ADDRESS OF THE PERSON:falt-102,bcc st. washington

DEPARTMENT IN WHICH PERSON WORKS:house keeping

POST OF THE PERSON:sr. manager

MONTHLY SALARY OF THE PERSON:21000

----------------------------------------------------------------------------------------------------------*/

Page 65: C++ Programs

//Q15.concept of operator overloading...................

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>

class box{

public:int len,breadth,height;box(int l,int b,int h){

len=l;breadth=b;height=h;

}void surface();void volume();void check();box operator++(){

len++;breadth++;height++;return *this;

}box operator++(int x){

++len;++breadth;++height;return *this;

}box operator--(int x){

--len;--breadth;--height;return *this;

}box operator--(){

len--;breadth--;height--;

Page 66: C++ Programs

return *this;}box operator=(box b4){

len=b4.len;breadth=b4.breadth;height=b4.height;return *this;

}box operator==(box b2){

if((len==b2.len) && (breadth==b2.breadth) && (height==b2.height))cout<<"TWO BOXES ARE EQUAL\n";

elsecout<<"\n\nTWO BOXES ARE NOT EQUAL";

}};

void box::surface(){

int sur=0; if(len==breadth && len==height)

sur=(6*len*len); else

sur=((4*len*breadth)+(2*breadth*height)); cout<<" SURFACE AREA:"<<sur;}

void box::volume(){

cout<<" VOLUME IS:"<<(len*breadth*height);}

void box::check(){

if(len==breadth && len==height)cout<<"\nIT IS A CUBE";

elsecout<<"\nIT IS A CUBOID";

}

Page 67: C++ Programs

void main(){

clrscr();int a,d,c;cout<<"Enter Dimension of Box1:";cin>>a>>c>>d;box b(a,c,d);cout<<"Enter Dimension of Box2:";cin>>a>>c>>d;box b1(a,c,d);cout<<"\nCHECKING EQUALITY:";b==b1;cout<<"\nBOX I's ";b.surface();cout<<"\nBOX II's ";b1.surface();cout<<"\nBOX I's ";b.volume();cout<<"\nBOX II's ";b1.volume();cout<<"\nOVERLOADED ++(prefix) OPERATOR:";++b;b.volume();cout<<"\nOVERLOADED ++(postfix) OPERATOR:";b++;b.volume();cout<<"\nOVERLOADED --(prefix) OPERATOR:";--b;b.volume();cout<<"\nOVERLOADED --(postfix) OPERATOR:";b--;b.volume();cout<<"\nGIVEN BOX IS A CUBE OR CUBOID(ON FIRST BOX):";b.check();cout<<"\nGIVEN BOX IS A CUBE OR CUBOID(ON SECOND BOX):";b1.check();cout<<"\nOVERLOADING ASSIGNMENT OPERATOR:";b=b1;b.volume();getch();

}

Page 68: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT

Enter Dimension of Box1:123Enter Dimension of Box2:234

CHECKING EQUALITY:

TWO BOXES ARE NOT EQUALBOX I's SURFACE AREA:20BOX II's SURFACE AREA:48BOX I's VOLUME IS:6BOX II's VOLUME IS:24OVERLOADED ++(prefix) OPERATOR: VOLUME IS:24OVERLOADED ++(postfix) OPERATOR: VOLUME IS:60OVERLOADED --(prefix) OPERATOR: VOLUME IS:24OVERLOADED --(postfix) OPERATOR: VOLUME IS:6GIVEN BOX IS A CUBE OR CUBOID(ON FIRST BOX):IT IS A CUBOIDGIVEN BOX IS A CUBE OR CUBOID(ON SECOND BOX):IT IS A CUBOIDOVERLOADING ASSIGNMENT OPERATOR: VOLUME IS:24

----------------------------------------------------------------------------------------------------------*/

Page 69: C++ Programs

//Q16. Linked List Implemention

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

template<class T>class stud{

T roll;int count,i;stud *next;stud *last,*first,*x,*loc,*x1;public:

void insert(T& ,int ,int);void insert_loc(T&);void delete_loc();void delete_end();void delete_beg();void reverse();void search();void show();

};template<class T>void stud<T>::search(){

int Roll,found;cout<<"\nENTER THE INFORMATION YOU WANT TO SEARCH:";cin>>Roll;found=0;x=first;while(x->next!=NULL && found!=1){

if(x->roll==Roll)found=1;

elsex=x->next;

}if(found==1)

cout<<"\nNUMBER FOUND";else

cout<<"\nNOT FOUND";

}

Page 70: C++ Programs

template<class T>void stud<T>::reverse(){

x1=last;do{

x=first;while(x->next!=x1)x=x->next;x1->next=x;x1=x;

}while(x1!=first);first->next=NULL;last=first;first=last;

}

template<class T>void stud<T>::show(){

x=first;count=0;while(x!=NULL){

count+=1;cout<<"\n\nINFORMATION OF STUDENT "<<count;cout<<"\nROLL NO. :"<<x->roll;x=x->next;

}}

template<class T>void stud<T>::delete_beg(){

cout<<"\nDELETING FROM THE BEGINNING:";loc=first->next;x1=first;x1->next=NULL;delete x1;first=loc;

}

Page 71: C++ Programs

template<class T>void stud<T>::delete_end(){

cout<<"\nDELETING FROM THE END:";x=first;count=0;while(x!=NULL){

count+=1;x=x->next;

}x=first;for(i=1;i<count-1;i++)

x=x->next;x->next=NULL;delete last;last=x;

}

template<class T>void stud<T>::delete_loc(){

int l;cout<<"\n\n\nENTER THE POSITION FROM WHICH YOU WANT TO

DELETE:";cin>>l;loc=first;for(i=1;i<l;i++)

loc=loc->next;x1=loc->next;x=first;for(i=1;i<l-1;i++)

x=x->next;x->next=x1;delete loc;

}

template<class T>void stud<T>::insert_loc(T& info){

int l;cout<<"\nINSERTING NODE AT A PARTICULAR LOCATION:";cout<<"\n\nENTER THE LOCATION:";cin>>l;x=new stud<T>;x->roll=info;

Page 72: C++ Programs

loc=first;for(i=1;i<l;i++)

loc=loc->next;x->next=loc->next;loc->next=x;

}

template<class T>void stud<T>::insert(T& info,int n=0,int mode=0){

x=new stud<T>;x->next=NULL;x->roll=info;if(mode==0){

if(n==1)first=last=x;

else{

last->next=x;last=x;

}}else{

x->next=first;first=x;

}}

void main(){

clrscr();int ch;stud<int> s;int info,n;cout<<"\nHOW MANY NODES YOU WANT TO CREATE:";cin>>n;cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,1,0);for(int i=1;i<n;i++){

cout<<"\nENTER THE INFORMATION:";cin>>info;

Page 73: C++ Programs

s.insert(info,n,0);}do{

do{

cout<<"\nMENU:";cout<<"\n1.CREATING A LINKED LIST:";cout<<"\n2.INSERTING AT THE END:";cout<<"\n3.INSERTING AT THE BEGINNING:";cout<<"\n4.INSERTING AT A LOCATION:";cout<<"\n5.DELETING AT THE END:";cout<<"\n6.DELETING AT THE BEGINNING:";cout<<"\n7.DELETING AT A LOCATION:";cout<<"\n8.SHOW LIST:";cout<<"\n9.REVERSING THE LIST:";cout<<"\n10.SEARCHING:";cout<<"\n11.EXIT:";cout<<"\nENTER YOUR OPTION:";cin>>ch;

}while(ch<1 || ch>11);switch(ch){case 1:{

cout<<"\nHOW MANY NODES YOU WANT TO CREATE:";cin>>n;cout<<"\nENTER THE INFORMATION:";cin>>info;

s.insert(info,1,0);for(int i=1;i<n;i++){

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,n,0);

}break;

}case 2:{

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,0,0);break;

}

Page 74: C++ Programs

case 3:{

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,0,1);break;

}case 4:{

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert_loc(info);break;

}case 5:{

s.delete_end();break;

}case 6:{

s.delete_beg();

break;}case 7:{

s.delete_loc();break;

}case 8:{

s.show();break;

}case 9:{

s.reverse(); break;

}case 10:{

s.search();break;

} case 11:

Page 75: C++ Programs

exit(0);

}cout<<"\nWANT TO CONTINUE(1&0):";cin>>ch;

}while(ch!=0);getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

HOW MANY NODES YOU WANT TO CREATE:3

ENTER THE INFORMATION:10

ENTER THE INFORMATION:22

ENTER THE INFORMATION:9

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:8

INFORMATION OF STUDENT 1ROLL NO. :10

INFORMATION OF STUDENT 2ROLL NO. :22

INFORMATION OF STUDENT 3ROLL NO. :9WANT TO CONTINUE(1&0):1

Page 76: C++ Programs

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:2

ENTER THE INFORMATION:13

WANT TO CONTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:3

ENTER THE INFORMATION:11

WANT TO CONTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:

Page 77: C++ Programs

9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:8

INFORMATION OF STUDENT 1ROLL NO. :11

INFORMATION OF STUDENT 2ROLL NO. :10

INFORMATION OF STUDENT 3ROLL NO. :22

INFORMATION OF STUDENT 4ROLL NO. :9

INFORMATION OF STUDENT 5ROLL NO. :13WANT TO CONTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:11

----------------------------------------------------------------------------------------------------------*/

Page 78: C++ Programs

//Q17.Doubly Linked List

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

template<class T>class stud{

T roll;int count,i;stud *next;stud *last,*first,*x,*loc,*x2,*x1,*pre;public:

void insert(T& ,int ,int);void insert_loc(T&);void delete_loc();void delete_end();void delete_beg();void reverse();void search();void show();

};

template<class T>void stud<T>::search(){

int Roll,found;cout<<"\nENTER THE INFORMATION YOU WANT TO SEARCH:";cin>>Roll;found=0;x=first;while(x!=NULL && found!=1){

if(x->roll==Roll)found=1;

elsex=x->next;

}if(found==1)

cout<<"\nNUMBER FOUND";else

cout<<"\nNOT FOUND";}

Page 79: C++ Programs

template<class T>void stud<T>::reverse(){

x=last;do{

x1=last->pre;x2=last->next;last->next=x1;last->pre=x2;last=x1;

}while(last!=first);first->next=NULL;last=first;first=x;

}

template<class T>void stud<T>::show(){

x=first;count=0;while(x!=NULL){

count+=1;cout<<"\n\nINFORMATION OF STUDENT "<<count;cout<<"\nROLL NO. :"<<x->roll;x=x->next;

}}

template<class T>void stud<T>::delete_beg(){

cout<<"\nDELETING FROM THE BEGINNING:";loc=first->next;x1=first;x1->next=NULL;delete x1;loc->pre=NULL;first=loc;

}

Page 80: C++ Programs

template<class T>void stud<T>::delete_end(){

cout<<"\nDELETING FROM THE END:";x=last->pre;x->next=NULL;delete last;last=x;

}

template<class T>void stud<T>::delete_loc(){

int l;cout<<"\n\n\nENTER THE POSITION FROM WHICH YOU WANT TO

DELETE:";cin>>l;loc=first;for(i=1;i<l;i++)

loc=loc->next;x1=loc->next;x=loc->pre;x1->pre=loc->pre;x->next=x1;delete loc;

}

template<class T>void stud<T>::insert_loc(T& info){

int l;cout<<"\nINSERTING NODE AT A PARTICULAR LOCATION:";cout<<"\n\nENTER THE LOCATION:";cin>>l;x=new stud<T>;x->roll=info;loc=first;for(i=1;i<l;i++)

loc=loc->next;loc->next->pre=x;x->next=loc->next;x->pre=loc;loc->next=x;

}

Page 81: C++ Programs

template<class T>void stud<T>::insert(T& info,int n=0,int mode=0){

x=new stud<T>;x->next=NULL;x->pre=NULL;x->roll=info;if(mode==0){

if(n==1)first=last=x;

else{

last->next=x;x->pre=last;last=x;

}}else{

x->next=first;first->pre=x;x->pre=NULL;first=x;

}}

void main(){

clrscr();int ch;stud<int> s;int info,n;cout<<"\nHOW MANY NODES YOU WANT TO CREATE:";cin>>n;cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,1,0);for(int i=1;i<n;i++){

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,n,0);

}

Page 82: C++ Programs

do{

do{

cout<<"\nMENU:";cout<<"\n1.CREATING A LINKED LIST:";cout<<"\n2.INSERTING AT THE END:";cout<<"\n3.INSERTING AT THE BEGINNING:";cout<<"\n4.INSERTING AT A LOCATION:";cout<<"\n5.DELETING AT THE END:";cout<<"\n6.DELETING AT THE BEGINNING:";cout<<"\n7.DELETING AT A LOCATION:";cout<<"\n8.SHOW LIST:";cout<<"\n9.REVERSING THE LIST:";cout<<"\n10.SEARCHING:";cout<<"\n11.EXIT:";cout<<"\nENTER YOUR OPTION:";cin>>ch;

}while(ch<1 || ch>11);switch(ch){

case 1:{

cout<<"\nHOW MANY NODES YOU WANT TO CREATE:";cin>>n;cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,1,0);for(int i=1;i<n;i++){

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,n,0);

} break;}

case 2:{

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,0,0);break;

}case 3:{

Page 83: C++ Programs

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info,0,1);break;

} case 4:

{cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert_loc(info);break;

}case 5:{

s.delete_end();break;

} case 6:

{s.delete_beg();

break;}case 7:{

s.delete_loc();break;

}case 8:{

s.show();break;

} case 9:

{s.reverse();break;

}case 10:{

s.search();break;

}case 11:

exit(0);}cout<<"\nWANT TO CONTINUE(1&0):";

Page 84: C++ Programs

cin>>ch;}while(ch!=0);getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT HOW MANY NODES YOU WANT TO CREATE:3

ENTER THE INFORMATION:11

ENTER THE INFORMATION:22

ENTER THE INFORMATION:33

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:2

ENTER THE INFORMATION:44

WANT TO COMTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:

Page 85: C++ Programs

10.SEARCHING:11.EXIT:ENTER YOUR OPTION:3

ENTER THE INFORMATION:10

WANT TO COMTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:8

INFORMATION OF STUDENT 1ROLL NO. :10

INFORMATION OF STUDENT 2ROLL NO. :11

INFORMATION OF STUDENT 3ROLL NO. :22

INFORMATION OF STUDENT 4ROLL NO. :33

INFORMATION OF STUDENT 5ROLL NO. :44WANT TO COMTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:

Page 86: C++ Programs

7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:5

DELETING FROM THE END:WANT TO COMTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:6

DELETING FROM THE BEGINNING:WANT TO COMTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING AT THE END:3.INSERTING AT THE BEGINNING:4.INSERTING AT A LOCATION:5.DELETING AT THE END:6.DELETING AT THE BEGINNING:7.DELETING AT A LOCATION:8.SHOW LIST:9.REVERSING THE LIST:10.SEARCHING:11.EXIT:ENTER YOUR OPTION:11

----------------------------------------------------------------------------------------------------------*/

Page 87: C++ Programs

//Q18.Circular Linked List

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>template<class T>class stud{

T roll;int count,i,found;stud *next;stud *first,*x,*loc,*x1,*tail;public:

void create(T& ,int);void insert(T&);void delete_loc();void search();void show();

};template<class T>void stud<T>::search(){

int Roll,found;cout<<"\nENTER THE INFORMATION YOU WANT TO SEARCH:";cin>>Roll;found=0;x=first;while(x!=NULL && found!=1){

if(x->roll==Roll)found=1;

elsex=x->next;

}if(found==1)

cout<<"\nNUMBER FOUND";else

cout<<"\nNOT FOUND";}

template<class T>void stud<T>::show(){

count=1;

Page 88: C++ Programs

cout<<"\n\nINFORMATION "<<1;cout<<"\nROLL NO. :"<<tail->roll;x=tail;x=x->next;while(x!=tail){

count+=1;cout<<"\n\nINFORMATION "<<count;cout<<"\nROLL NO. :"<<x->roll;x=x->next;

}}template<class T>void stud<T>::insert(T& info){

int Roll;found=0;cout<<"\n\nENTER THE ROLL NO. AFTER WHICH YOU WANT TO

INSERT:";cin>>Roll;x1=tail;if(x1->roll==Roll){

found=1;loc=x1;

}else{

x=x1->next;while(x!=x1 && found!=1){

if(x->roll==Roll){

found=1;loc=x;

}else

x=x->next;}

}if(found==1){

x=new stud<T>;x->next=loc->next;loc->next=x;x->roll=info;

Page 89: C++ Programs

}else

cout<<"\nERROR:";}template<class T>void stud<T>::delete_loc(){

int Roll;cout<<"\n\n\nENTER THE ROLL NUMBER WHICH YOU WANT TO

DELETE:";cin>>Roll;x1=tail;if(x1->roll==Roll){

loc=x1;x1=x1->next;while(x1->next!=tail)x1=x1->next;x=loc->next;x1->next=x;delete loc;tail=x1;

}else{

x=x1->next;while(x!=x1 && found!=1){

if(x->roll==Roll){

found=1;loc=x;

}else{

first=x;x=x->next;

}}if(found==1){

first->next=loc->next;delete loc;

}else

cout<<"\nERROR:";

Page 90: C++ Programs

}found=0;

}template<class T>void stud<T>::create(T& info,int n){

x=new stud<T>;x->roll=info;if(n==0){

x->next=x;first=x1=x;

}else{

x->next=first;x1->next=x;x1=x;

}tail=x;

}

void main(){

clrscr();int ch;stud<int> s;int info,n;cout<<"\nHOW MANY NODES YOU WANT TO CREATE:";cin>>n;cout<<"\nENTER THE INFORMATION:";cin>>info;s.create(info,0);for(int i=1;i<n;i++){

cout<<"\nENTER THE INFORMATION:";cin>>info;s.create(info,n);

} do { do { cout<<"\nMENU:";

cout<<"\n1.CREATING A LINKED LIST:";cout<<"\n2.INSERTING:";

Page 91: C++ Programs

cout<<"\n3.DELETING :";cout<<"\n4.SHOW LIST:";cout<<"\n5.SEARCHING:";cout<<"\n6.EXIT:";cout<<"\nENTER YOUR OPTION:";cin>>ch;

}while(ch<1 || ch>6);switch(ch){

case 1:{

cout<<"\nHOW MANY NODES YOU WANT TO CREATE:";

cin>>n;cout<<"\nENTER THE INFORMATION:";cin>>info;s.create(info,0);for(int i=1;i<n;i++)

{cout<<"\nENTER THE INFORMATION:";cin>>info;s.create(info,n);

} break;

}case 2:{

cout<<"\nENTER THE INFORMATION:";cin>>info;s.insert(info);break;

}case 3:{

s.delete_loc();break;

}case 4:{

s.show();break;

}case 5:{

s.search();

Page 92: C++ Programs

break;}case 6:

exit(0);

} cout<<"\nWANT TO CONTINUE(1&0):"; cin>>ch;

}while(ch!=0);getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT HOW MANY NODES YOU WANT TO CREATE:3

ENTER THE INFORMATION:11

ENTER THE INFORMATION:22

ENTER THE INFORMATION:33

MENU:1.CREATING A LINKED LIST:2.INSERTING:3.DELETING :4.SHOW LIST:5.SEARCHING:6.EXIT:ENTER YOUR OPTION:2

ENTER THE INFORMATION:15

ENTER THE ROLL NO. AFTER WHICH YOU WANT TO INSERT:11

WANT TO CONTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING:3.DELETING :4.SHOW LIST:

Page 93: C++ Programs

5.SEARCHING:6.EXIT:ENTER YOUR OPTION:4

INFORMATION 1ROLL NO. :33

INFORMATION 2ROLL NO. :11

INFORMATION 3ROLL NO. :15

INFORMATION 4ROLL NO. :22WANT TO CONTINUE(1&0):3

MENU:1.CREATING A LINKED LIST:2.INSERTING:3.DELETING :4.SHOW LIST:5.SEARCHING:6.EXIT:ENTER YOUR OPTION:3

ENTER THE ROLL NUMBER WHICH YOU WANT TO DELETE:22

WANT TO CONTINUE(1&0):1

MENU:1.CREATING A LINKED LIST:2.INSERTING:3.DELETING :4.SHOW LIST:5.SEARCHING:6.EXIT:ENTER YOUR OPTION:6

----------------------------------------------------------------------------------------------------------*/

Page 94: C++ Programs

//Q19.Stack Operations

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

class stack{

int data; stack *next,*top;

public:void push(int,int);void pop();void show();

};void stack::push(int info,int n){

stack *x;x=new stack;x->data=info;if(n==0){

top=x;top->next=NULL;

}else{

x->next=top;top=x;

}}

void stack::pop(){

if(top==NULL)cout<<"\nLIST IS EMPTY:";

else{

stack *x;x=top->next;cout<<"\nELEMENT DELETED IS:"<<top->data;delete top;top=x;

}

Page 95: C++ Programs

}void stack::show(){

stack *x;int count;count=0;x=top;while(x!=NULL){

count+=1;cout<<"\nINFORMATION " <<count<<": "<<x->data;x=x->next;

}}

void main(){

clrscr();stack s;int info,ch,n;do{

cout<<"\n1.INSERT:";cout<<"\n2.DELETION:";cout<<"\n3.DISPLAY:";cout<<"\n4.EXIT:";cout<<"\nENTER YOUR CHOICE:";cin>>ch;switch(ch){case 1:{

cout<<"\nHOW MANY ELEMENT YOU WANT TO PUSH:";cin>>n;cout<<"\nENTER THE ELEMENT TO BE PUSHED:";

cin>>info; s.push(info,0); for(int i=1;i<n;i++) {

cout<<"\nENTER THE ELEMENT TO BE PUSHED:";cin>>info;s.push(info,n);

}break;

}

Page 96: C++ Programs

case 2:{

s.pop();break;

}case 3:{

s.show();break;

}case 4:

exit(0);}cout<<"\nWANT TO CONTINUE(1 &0):";cin>>ch;

}while(ch!=0);getch();}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

1.INSERT:2.DELETION:3.DISPLAY:4.EXIT:ENTER YOUR CHOICE:1

HOW MANY ELEMENT YOU WANT TO PUSH:5

ENTER THE ELEMENT TO BE PUSHED:11

ENTER THE ELEMENT TO BE PUSHED:33

ENTER THE ELEMENT TO BE PUSHED:22

ENTER THE ELEMENT TO BE PUSHED:44

ENTER THE ELEMENT TO BE PUSHED:55

WANT TO CONTINUE(1 &0):1

Page 97: C++ Programs

1.INSERT:2.DELETION:3.DISPLAY:4.EXIT:ENTER YOUR CHOICE:2

ELEMENT DELETED IS:55WANT TO CONTINUE(1 &0):1

1.INSERT:2.DELETION:3.DISPLAY:4.EXIT:ENTER YOUR CHOICE:3

INFORMATION 1: 44INFORMATION 2: 22INFORMATION 3: 33INFORMATION 4: 11WANT TO CONTINUE(1 &0):1

1.INSERT:2.DELETION:3.DISPLAY:4.EXIT:ENTER YOUR CHOICE:4.

----------------------------------------------------------------------------------------------------------/*

Page 98: C++ Programs

//Q20.Queue Implementation//----------------------------------------------------------------------------------------------------------#include<iostream.h>#include<conio.h>template<class T>class stack{

int top;T stck[100];public:

stack(){

top=-1;}void push(T& item){

if(top==100)cout<<"\nSTACK IS FULL:";

elsestck[++top]=item;

}T pop(){

if(top<0)cout<<"\nUNDERFLOW:";

elsereturn stck[top--];

}};void main(){

clrscr();int n, item;stack<int> s;cout<<"\nHOW MANY ITEMS YOU WANT TO PUSH:";cin>>n;for(int j=0;j<n;j++){

cout<<"\nENTER THE ELEMENT YOU WANT TO PUSH:";cin>>item;s.push(item);

}for(int j=0;j<n;j++)

cout<<"\nELEMENT POPPED IS:"<<s.pop();getch();

}

Page 99: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT HOW MANY ITEMS YOU WANT TO PUSH:5

ENTER THE ELEMENT YOU WANT TO PUSH:1

ENTER THE ELEMENT YOU WANT TO PUSH:2

ENTER THE ELEMENT YOU WANT TO PUSH:3

ENTER THE ELEMENT YOU WANT TO PUSH:4

ENTER THE ELEMENT YOU WANT TO PUSH:5

ELEMENT POPPED IS:5ELEMENT POPPED IS:4ELEMENT POPPED IS:3ELEMENT POPPED IS:2ELEMENT POPPED IS:1

----------------------------------------------------------------------------------------------------------*/

Page 100: C++ Programs

//Q21.CIRCULAR QUEUE ARRAY IMPLEMENTION .........

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#define MAX 100template<class T>class array_queue{

int front,rear;int arr[MAX];public:array_queue(){

front=rear=-1;}void add(T& info);void del();void display();

};template<class T>void array_queue<T>::add(T& info){

if((rear==MAX-1 && front==0)||rear+1==front){

cout<<"\nLIST FULL:";return;

}if(rear==MAX-1)

rear=0;else

rear++;arr[rear]=info;if(front==-1)

front=0;}template<class T>void array_queue<T>::del(){

if(front==-1){

cout<<"\nUNDERFLOW:";return;

}cout<<"\nELEMENT DELETED IS:"<<arr[front];

Page 101: C++ Programs

arr[front]=0;if(front==rear)

front=rear=-1;else

front++;}template<class T>void array_queue<T>::display(){

cout<<"\nQUEUE IS:";for(int i=front;i<=rear;i++)

cout<<arr[i]<<" ";}void main(){

clrscr();int t;int n;array_queue<int> q;cout<<"\nHOW MANY ELEMENT YOU WANT TO ADD:";cin>>n;for(int i=0;i<n;i++){

cout<<"\nENTER THE ELEMENT:"; cin>>t; q.add(t); }

q.del();cout<<"\n";q.display();getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUTHOW MANY ELEMENT YOU WANT TO ADD:5ENTER THE ELEMENT:1 2 3 4 5ELEMENT DELETED IS:1QUEUE IS:2 3 4 5

----------------------------------------------------------------------------------------------------------*/

Page 102: C++ Programs

//Q22.Implement Duoble ended Queue using Linked List

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<stdio.h>#include<conio.h>#include<process.h>

class que{

public:que *prev,*next;int num;void create();void inslef();void insrig();void dellef();void delrig();void trav();

}*head,*front,*rear,*t1,*t2,ob;

void que::create(){

char ch; front=NULL; do { head=new(que); cout<<"Enter Data:"; cin>>head->num; if(front!=NULL) { t1->next=head; t1=head; t1->prev=t2; t2=head; } else { front=rear=t1=t2=head; head->prev=NULL; }

fflush(stdin); cout<<"\n Want 2 continue(y/n):"; cin>>ch;

Page 103: C++ Programs

}while(ch=='y');

rear=head; rear->next=NULL;}

void que::inslef(){

head=new(que); cout<<"Enter Data:"; cin>>head->num; head->next=front; front->prev=head; head->prev=NULL; front=head;}

void que::insrig(){

head=new(que); cout<<"Enter Data:"; cin>>head->num; rear->next=head; head->prev=rear; head->next=NULL; rear=head;}

void que::dellef(){

t1=front->next; t1->prev=NULL; front->next=NULL; front=t1;}

void que::delrig(){

t2=rear->prev; t2->next=NULL; rear->prev=NULL; rear=t2;}

Page 104: C++ Programs

void que::trav(){

int ch; cout<<"\n1:Traverse from Left"; cout<<"\n2:Traverse from Right\n"; cout<<"Enter choice:"; cin>>ch; switch (ch) {

case 1:{

t1=front; while(t1!=NULL) { cout<<t1->num<<" "; t1=t1->next; } break;

}case 2:{

t2=rear;while(t2!=NULL)

{ cout<<t2->num<<" "; t2=t2->prev; } break;

} default:

cout<<"U HAve Entered Wrong Choice!!!!!!!"; }}

void main(){

clrscr();int choice,ch;do{

cout<<"\n***----------** MENU **----------***\n\n";cout<<"1 :Create Queue\n";cout<<"2 :Insert At Left\n";cout<<"3 :Insert At Right\n";cout<<"4 :Delete At Left\n";cout<<"5 :Delete At Right\n";

Page 105: C++ Programs

cout<<"6 :Display Queue\n";cout<<"7 :Quit the Programme\n";

cout<<"\nEnter Your Choice:";cin>>choice;switch(choice){

case 1:{

ob.create();break;

}

case 2: {

ob.inslef();break;

}case 3:

{ob.insrig();break;

}case 4:{

ob.dellef();break;

}case 5:{

ob.delrig();break;

}case 6:{

ob.trav();break;

}case 7:{

cout<<"U Hav Quit The Programme!!!"; getch(); exit(0);

} default :

cout<<"\n You Have Entered a Wrong Choice pls Try Again!!!!!";

Page 106: C++ Programs

}}while(ch!=0);getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:1Enter Data:11

Want 2 continue(y/n):yEnter Data:22

Want 2 continue(y/n):yEnter Data:33

Want 2 continue(y/n):n

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:2

Page 107: C++ Programs

Enter Data:10

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:3Enter Data:44

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:6

1:Traverse from Left2:Traverse from RightEnter choice:110 11 22 33 44

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:6

Page 108: C++ Programs

1:Traverse from Left2:Traverse from RightEnter choice:244 33 22 11 10***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:4

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:5

***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:6

Page 109: C++ Programs

1:Traverse from Left2:Traverse from RightEnter choice:111 22 33***----------** MENU **----------***

1 :Create Queue2 :Insert At Left3 :Insert At Right4 :Delete At Left5 :Delete At Right6 :Display Queue7 :Quit the Programme

Enter Your Choice:7U Hav Quit The Program!!!

----------------------------------------------------------------------------------------------------------*/

Page 110: C++ Programs

//Q23.Scan a polynomial using linked list and Add them

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>

class stud{

int roll;int count,i;stud *next;stud *last,*first,*x,*loc,*x1;public:

void show();void insert(int ,int);void process(stud,stud);

};void stud::insert(int info,int n){

x=new stud;x->next=NULL;x->roll=info;if(n==0)

first=last=x;else{

last->next=x;last=x;

}}void stud:: process(stud s1,stud s2){

int Aa;stud f;x=s1.first;x1=s2.first;for(i=0;i<3;i++){

Aa=((x->roll)+(x1->roll));f.insert(Aa,i);x=x->next;x1=x1->next;

}f.show();

}

Page 111: C++ Programs

void stud::show(){

cout<<"\nNEW POLYNOMIAL IS:";cout<<"\n"<<first->roll<<"X^2 + "<<first->next->roll;cout<<"X + "<<last->roll;

}

void main(){

clrscr();stud s,s1,s2;int info,n;cout<<"\nENTER THE VALUE OF A,B & C:";cout<<"IN Ax^2+Bx+C:";for(int i=0;i<3;i++){

cout<<"\nENTER THE INFORMATION:";cin>>info;s1.insert(info,i);

}cout<<"\nENTER THE VALUE OF a,b & c:";cout<<"IN ax^2+bx+c:";for(int i=0;i<3;i++){

cout<<"\nENTER THE INFORMATION:"; cin>>info;

s2.insert(info,i);}s.process(s1,s2);getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT

ENTER THE VALUE OF A,B & C:IN Ax^2+Bx+C:ENTER THE INFORMATION:2

ENTER THE INFORMATION:2

ENTER THE INFORMATION:2

ENTER THE VALUE OF a,b & c:IN ax^2+bx+c:ENTER THE INFORMATION:3

Page 112: C++ Programs

ENTER THE INFORMATION:3

ENTER THE INFORMATION:3

NEW POLYNOMIAL IS:5X^2 + 5X + 5

----------------------------------------------------------------------------------------------------------*/

Page 113: C++ Programs

//Q24.This program prints the FACTORIAL of a number.using RECURSION & ITERATION.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>

class Fact{

int b;public:

void fcal(int a,int c=1){

if(a>1){

b=a-1;c=(a*b)*c;b--;

fcal(b,c); } else cout<<"\nFACTORIAL IS:"<<c; }};void main(){

clrscr();int c,a;Fact v;cout<<"\nENTER THE NUMBER:";cin>>a;cout<<"\nFACTORIAL WITH RECURSION:";v.fcal(a);cout<<"\n\n\nFACTORIAL WITHOUT RECURSION:";for(int i=a-1;i>=1;i--){

c=a*i;a=c;

}cout<<"\nFACTORIAL IS:"<<c;getch();

}

Page 114: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT

ENTER THE NUMBER:15

FACTORIAL WITH RECURSION:FACTORIAL IS:2004310016

FACTORIAL WITHOUT RECURSION:FACTORIAL IS:2004310016

----------------------------------------------------------------------------------------------------------*/

Page 115: C++ Programs

//Q25.This program prints the fibonnaci series till n th term by the recursion method.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>class Fib{

int c;public:void fcal(int a,int b,int n){

if(n>1){

c=a+b;a=b;b=c;cout<<c<<" ";fcal(a,b,--n);

}}

};void main(){

clrscr();int n,c,a,b;Fib v;cout<<"\nENTER THE VALUE OF N:";cin>>n;cout<<"\nFIBONNACI SERIES WITH RECURSION:";cout<<"0 1 ";v.fcal(0,1,n-1);cout<<"\n\n\nFIBONNACI SERIES WITHOUT RECURSION:";a=0;b=1;cout<<a<<" "<<b;for(int i=1;i<n-1;i++){

c=a+b;a=b;b=c;cout<<" "<<c;

}getch();

}

Page 116: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT

ENTER THE VALUE OF N:15

FIBONNACI SERIES WITH RECURSION:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

FIBONNACI SERIES WITHOUT RECURSION:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

----------------------------------------------------------------------------------------------------------*/

Page 117: C++ Programs

//Q26. WAP to calculate GCD of 2 numbers a)using iteration b)using recursion.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>int GCD(int,int);void main(){

clrscr();int a,b,rem;cout<<"\nEnter two numbers: ";cin>>a>>b;cout<<"\nGCD of two numbers using recursion is: "<<GCD(a,b);cout<<"\nGCD of two numbers without using recursion is: ";while(b%a!=0){

rem=b%a;b=a;a=rem;

}cout<<a;getch();

}int GCD(int a,int b){

int rem;if(b%a==0)

return a;else{

rem=b%a;b=a;a=rem;GCD(a,b);

}}

/*----------------------------------------------------------------------------------------------------------

OUTPUTEnter two numbers: 20 45

GCD of two numbers using recursion is: 5GCD of two numbers without using recursion is: 5----------------------------------------------------------------------------------------------------------*/

Page 118: C++ Programs

//Q27. WAP to create a Binary Search tree and to perform various operations on the tree.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#define max 100

class queue{

public:int front,rear;int arr[max];queue(){

front=rear=-1;}void add(int info);int del();

};

void queue::add(int info){

if((rear==max-1) && (front==0) || rear+1==front){

cout<<"\nList Full";return;

}if(rear==max-1)

rear=0;else

rear++;arr[rear]=info;if(front==-1)

front=0;}int queue::del(){

int f;if(front==-1){

return 0;}else{

Page 119: C++ Programs

f=arr[front];if(front==rear)

front=rear=-1;else

front++;return f;

}}

class binary:public queue{

int data,i;binary *root,*p,*q,*q1,*p1;binary *left,*right;int count,count1;public:

binary(){

root=NULL;count=count1=0;

}void insert();void traverse();void breath(binary *);void inorder(binary *);void preorder(binary *);void postorder(binary *);void mirror();binary* search(int,binary **);void remove();void leaf();

};

void binary::insert(){

cout<<"\nEnter value & Enter 0 to quit:";cin>>i;while(i!=0){

if(root==NULL){

root=new binary;root->left=NULL;root->right=NULL;root->data=i;p=root;

Page 120: C++ Programs

}else{

p=root;q=new binary;q->left=NULL;q->right=NULL;

while(p!=NULL){

if(i<p->data){

p1=p;p=p->left;

}else if(i>=(p->data))

{p1=p;p=p->right;

}}if(i<p1->data)

p1->left=q;else

p1->right=q;

p=q;p->data=i;p->left=NULL;p->right=NULL;

}cout<<"\nEnter next value:";cin>>i;

}}void binary::traverse(){

cout<<"\nInorder Traversal:";inorder(root);cout<<"\nPreorder Traversal:";preorder(root);cout<<"\nPostorder Traversal:";postorder(root);cout<<"\nBreadth Traversal:";breath(root);

}

Page 121: C++ Programs

void binary::inorder(binary *p){

if(p!=NULL){

inorder(p->left);cout<<p->data<<" ";inorder(p->right);

}else

return;}

void binary::postorder(binary *p){

if(p!=NULL){

postorder(p->left);postorder(p->right);cout<<p->data<<" ";

}else

return;}void binary::breath(binary *p){

queue Queue;int i;if(p!=NULL){

Queue.add(p->data);while(i!=0){

i=Queue.del();if(i!=0)

cout<<i<<" ";p1=NULL;//searching the numberp=search(i,&p1);if(p->left!=NULL)

Queue.add(p->left->data);if(p->right!=NULL)

Queue.add(p->right->data);}

}}

Page 122: C++ Programs

void binary::preorder(binary *p){

if(p!=NULL){

cout<<p->data<<" ";if((p->left!=NULL)||(p->right!=NULL))

count++;if((p->left==NULL)&&(p->right==NULL))

count1++;preorder(p->left);preorder(p->right);

}else

return;}

void binary::mirror(){

queue Queue;int i;p=root;if(p!=NULL){

Queue.add(p->data);while(i!=0){

i=Queue.del();if(i!=0)

cout<<i;p1=NULL;//searching the numbersp=search(i,&p1);if(p->right!=NULL)

Queue.add(p->right->data);if(p->left!=NULL)

Queue.add(p->left->data);}

}}

binary* binary::search(int i,binary **pre){

q=root;int found=0;while(q!=NULL){

Page 123: C++ Programs

if(q->data==i){

found=1;break;

}*pre=q;if(q->data>i)

q=q->left;else

q=q->right;

}if(found==0)

q=NULL;return q;

}

void binary::remove(){

if(root==NULL){

cout<<"\nTree is empty";return;

}q=root;cout<<"\nEnter the node want to delete:";cin>>i;p1=NULL;

//searching the numberq=search(i,&p1);if(q==NULL){

cout<<"\nElement not found";return;

}getch();clrscr();

//node to be deleted has no childif((q->right==NULL)&&(q->left==NULL)){

if(p1->right==q)p1->right=NULL;

elsep1->left=NULL;

delete q;}

Page 124: C++ Programs

//node to be deleted has no left childif((q->right!=NULL)&&(q->left==NULL)){

if(p1->right==q)p1->right=q->right;

elsep1->left=q->right;

delete q;}

//node to be deleted has no right childif((q->right==NULL)&&(q->left!=NULL)){

if(p1->right==q)p1->right=q->left;

elsep1->left=q->left;

delete q;}int ch;if((q->right!=NULL)&&(q->left!=NULL)){

cout<<"\n1.Copying";cout<<"\n2.Merging";cin>>ch;if(ch==1){

cout<<"\nDeletion by copying\n";p=q->left;q1=q;while(p->right!=NULL){

q1=p;p=p->right;

}

q->data=p->data;if(q1==q)

q1->left=p->left;else

q1->right=p->left;delete p;

}else{

cout<<"\n\nDeletion by merging";p=q->left;

Page 125: C++ Programs

while(p->right!=NULL)p=p->right;

p->right=q->right;p=q;if(p1->left==q)

p1->left=q->left;if(p1->right==q)

p1->right=q->left;if(q==root)

root=q->left;q=q->left;delete p;

}}traverse();

}void binary::leaf(){

cout<<"\nTotal number of leaf nodes:";cout<<count1;cout<<"\nTotal number of non leaf nodes:" ;cout<<count;cout<<"\nTotal number of nodes:";cout<<(count+count1);

}void main(){

clrscr();binary b1;int ch;char ch1;cout<<"\n1.Insert";cout<<"\n2.Traverse";cout<<"\n3.Calculate leaf";cout<<"\n4.Mirror Image";cout<<"\n5.Delete";do{

cout<<"Enter your choice:";cin>>ch;switch(ch){

case 1:{

b1.insert();

Page 126: C++ Programs

break;}case 2:{

b1.traverse();break;

}case 3:{

b1.leaf();break;

}case 4:{

cout<<"\nMirror image using Breadth first\n";b1.mirror();break;

}case 5:{

b1.remove();break;

}}cout<<"\nWant to continue?";cin>>ch1;

}while((ch1=='y')||(ch1=='Y'));getch();

}/*----------------------------------------------------------------------------------------------------------

OUTPUT:1.Insert2.Traverse3.Calculate leaf4.Mirror Image5.DeleteEnter your choice:1

Enter value & Enter 0 to quit:15

Enter next value:20

Enter next value:5

Page 127: C++ Programs

Enter next value:10

Enter next value:2

Enter next value:1

Enter next value:21

Enter next value:50

Enter next value:0

Want to continue?yEnter your choice:2

Inorder Traversal:1 2 5 10 15 20 21 50Preorder Traversal:15 5 2 1 10 20 21 50Postorder Traversal:1 2 10 5 50 21 20 15Breadth Traversal:15 5 20 2 10 21 1 50Want to continue?yEnter your choice:3

Total number of leaf nodes:3Total number of non leaf nodes:5Total number of nodes:8Want to continue?yEnter your choice:4

Mirror image using Breadth first1520521102501Want to continue?yEnter your choice:5

Enter the node want to delete:5

1.Copying2.Merging1

Deletion by copying

Inorder Traversal:1 2 10 15 20 21 50Preorder Traversal:15 2 1 10 20 21 50Postorder Traversal:1 10 2 50 21 20 15Breadth Traversal:15 2 20 1 10 21 50Want to continue?n----------------------------------------------------------------------------------------------------------*/

Page 128: C++ Programs

//no.28 conversion of sparse matrix into non-zero form and vice-versa...

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

struct cheadnode{

int colno;struct node *down;cheadnode *next;

};

struct rheadnode{

int rowno;struct node *right;rheadnode *next;

};

struct node{

int col;int row;int val;node *down;node *right;

};

struct spmat{

cheadnode *firstcol;rheadnode *firstrow;int noofrows;int noofcols;

};

class sparse{

private:int row;spmat *smat;cheadnode *chead[3];rheadnode *rhead[3];

Page 129: C++ Programs

node *nd;public:

sparse();void call(int,int,int);void insert(spmat *smat,int,int,int);void show_list();

};

sparse::sparse(){

for(int i=0;i<3;i++)rhead[i]=new rheadnode;

for(i=0;i<3;i++){

rhead[i]->next=rhead[i+1];rhead[i]->right=NULL;rhead[i]->rowno=i;

}rhead[i]->right=NULL;rhead[i]->next=NULL;for(i=0;i<3;i++)

chead[i]=new cheadnode;for(i=0;i<3;i++){

chead[i]->next=chead[i+1]; chead[i]->down=NULL; chead[i]->colno=i;

}chead[i]->down=NULL;chead[i]->next=NULL;smat=new spmat;smat->firstrow=rhead[0];smat->firstcol=chead[0];smat->noofrows=3;smat->noofrows=3;

}

void sparse::call(int r,int c,int v){

insert(smat,r,c,v);}

void sparse::insert(spmat *smat,int r,int c,int v){

nd=new node;nd->col=c;

Page 130: C++ Programs

nd->row=r;nd->val=v;node *temp1,*temp2;rheadnode *rh=smat->firstrow;for(int i=0;i<r;i++)

rh=rh->next;temp1=rh->right;if(temp1==NULL){

rh->right=nd;nd->right=NULL;

}else{

while((temp1 !=NULL)&& (temp1->col<c)){

temp2=temp1;temp1=temp1->right;

}temp2->right=nd;nd->right=NULL;

}cheadnode *ch=smat->firstcol;for(int j=0;j<c;j++)

ch=ch->next;temp1=ch->down;if(temp1==NULL){

ch->down=nd;nd->down=NULL;

}else{

while((temp1!=NULL) && (temp1->col<c)){

temp2=temp1;temp1=temp1->down;

}temp2->down=nd;nd->down=NULL;

}}void sparse::show_list(){

int r=smat->noofcols;node *temp;

Page 131: C++ Programs

cout<<"\n";for(int i=0;i<r;i++){

temp=chead[i]->down;if(temp!=NULL){

while(temp!=NULL){

cout<<"row:"<<temp->row;cout<<" col:"<<temp->col;cout<<"val:"<<temp->val<<"\n";temp=temp->down;

} }

}}

void main(){

clrscr();int a[3][3];int i,j,count=0;cout<<"\n ENTER THE ELEMENTS:";for(i=0;i<3;i++)

for(j=0;j<3;j++)cin>>a[i][j];

cout<<"\nMATRIX IS:";for(i=0;i<3;i++){

for(j=0;j<3;j++)cout<<a[i][j]<<" ";

cout<<"\n";}for(i=0;i<3;i++)

for(j=0;j<3;j++)if(a[i][j]==0)

count++;if(count>9/2){

sparse s;for(i=0;i<3;i++){

for(j=0;j<3;j++){

if(a[i][j]!=0)s.call(i,j,a[i][j]);

Page 132: C++ Programs

} } cout<<"\n CONVERTED INTO NON ZERO FORM:"; s.show_list();

}getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUTENTER THE ELEMENTS:1 2 3 4 5 6 7 8 9 MATRIX IS:1 2 3 4 5 67 8 9

----------------------------------------------------------------------------------------------------------*/

Page 133: C++ Programs

//Q29. WAP to reverse the order of the elements in the stack using additional stack.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#include<process.h>

class stack{

int data;stack *next,*top;public:void push(int,int);int pop();void show();void process(stack,int);

};

void stack::push(int info,int n){

stack *x;x=new stack;x->data=info;if(n==0){

top=x;top->next=NULL;

}else{

x->next=top;top=x;

}}int stack::pop(){

int inf;stack *x;x=top->next;inf=top->data;delete top;top=x;return inf;

}

Page 134: C++ Programs

void stack::show(){

stack *x;int count;count=0;x=top;while(x!=NULL){

count+=1;cout<<"\nInformation"<<count<<": "<<x->data;x=x->next;

}}void stack::process(stack s,int n){

stack s2,*x1;int info;cout<<"\nOriginal stack is";s.show();for(int k=0;k<n;k++){

info=s.pop();s2.push(info,k);

}cout<<"\nReversed stack is";s2.show();

}void main(){

clrscr();stack s,s1;int info,ch,n;cout<<"\nHow many elements do u want to push";cin>>n;for(int i=0;i<n;i++){

cout<<"\nEnter the element to be pushed";cin>>info;s.push(info,i);

}s1.process(s,n);getch();

}

Page 135: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT:

How many elements do u want to push5

Enter the element to be pushed2

Enter the element to be pushed4

Enter the element to be pushed6

Enter the element to be pushed8

Enter the element to be pushed10

Original stack isInformation1: 10Information2: 8Information3: 6Information4: 4Information5: 2Reversed stack isInformation1: 2Information2: 4Information3: 6Information4: 8Information5: 10

----------------------------------------------------------------------------------------------------------*/

Page 136: C++ Programs

//Q30. WAP to reverse the order of the elements in the stack using additional queue.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>#define max 100

class array_queue{

int front,rear;int arr[max];public:array_queue(){

front=rear=-1;}void add(int);void display();int del();

};

int array_queue::del(){

int item;if(front==-1){

cout<<"\nUnderflow";return 0;

}item=arr[front];if(front==rear)

front=rear=-1;else

front++;return item;

}

void array_queue::add(int info){

if((rear==max-1 && front==0)||(rear+1==front)){

cout<<"\nList full";return;

}

Page 137: C++ Programs

if(rear==max-1)rear=0;

elserear++;

arr[rear]=info;if(front==-1)

front=0;}

class stack: public array_queue{

int top;int stck[max];public:stack(){

top=-1;}void push(int item){

if(top==max)cout<<"\nStack is full";

elsestck[++top]=item;

}int pop(){

if(top<0)cout<<"\nUnderflow";

elsereturn stck[top--];

}void show(){for(int i=0;i<=top;i++)

cout<<stck[i]<<" ";}

};

void main(){

clrscr();int n;int item;stack s,s1;array_queue a;

Page 138: C++ Programs

cout<<"\nHow many items you want to push: ";cin>>n;for(int j=0;j<n;j++){

cout<<"\nEnter the element you want to push: ";cin>>item;s.push(item);

}cout<<"\nOriginal stack: ";s.show();for(j=0;j<n;j++){

item=s.pop();a.add(item);

}for(j=0;j<n;j++){

item=a.del();s1.push(item);

}cout<<"\nReversed stack: ";s1.show();getch();

}

/*----------------------------------------------------------------------------------------------------------

OUTPUT:

How many items you want to push: 5

Enter the element you want to push: 1

Enter the element you want to push: 2

Enter the element you want to push: 3

Enter the element you want to push: 4

Enter the element you want to push: 5

Original stack: 1 2 3 4 5Reversed stack: 5 4 3 2 1

----------------------------------------------------------------------------------------------------------*/

Page 139: C++ Programs

//Q31. WAP to implement Diagonal Matrix using one-dimensional array.//----------------------------------------------------------------------------------------------------------#include<iostream.h>#include<conio.h>#define n 3class d_matrix{

int t;public:

void pr_row(int j,int row_dim,int *p){

p=p+(j*row_dim);for(t=0;t<row_dim;++t)

if(j==t)cout<<*(p+t)<<" ";

}};void main(){

clrscr();int num[3][3];cout<<"\nEnter the values";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

if(i==j)cin>>num[i][j];

elsenum[i][j]=0;

}}cout<<"\nThe matrix is:\n";for(i=0;i<n;i++){

for(int j=0;j<n;j++)cout<<num[i][j]<<" ";

cout<<"\n";}d_matrix ob;cout<<"\nDiagonal matrix implemented using 1-d is\n";for(i=0;i<n;i++)

ob.pr_row(i,n,(int *)num);getch();

}

Page 140: C++ Programs

/*----------------------------------------------------------------------------------------------------------

/OUTPUT:

Enter the values2 3 4

The matrix is:2 0 00 3 00 0 4

Diagonal matrix implemented using 1-d is2 3 4

----------------------------------------------------------------------------------------------------------*/

Page 141: C++ Programs

//Q32. WAP to implement Lower triangular matrix using one-dimensional array.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>class lt_matrix{

int t;public:

void pr_row(int j,int row_dim,int *p){

p=p+(j*row_dim);for(t=0;t<row_dim;++t){

if(t<j)cout<<*(p+t)<<" ";

elsecout<<"\n";

}}

};void main(){

clrscr();int n,num[3][3];cout<<"\nEnter the size of the matrix";cin>>n;cout<<"\nEnter the values";for(int i=0;i<n;i++)

for(int j=0;j<n;j++)cin>>num[i][j];cout<<"\nMatrix is:\n";

for(i=0;i<n;i++){

for(j=0;j<n;j++)cout<<num[i][j]<<" ";

cout<<"\n";}lt_matrix ob;cout<<"\nLower triangular matrix implemented using 1-d is\n";for(i=0;i<n;i++)

ob.pr_row(i,n,(int *)num);getch();

}

Page 142: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT:

Enter the size of the matrix: 3

Enter the values: 9 8 7 6 5 4 3 2 1

Matrix is:9 8 76 5 43 2 1

Lower triangular matrix implemented using 1-d is

6

3 2

----------------------------------------------------------------------------------------------------------*/

Page 143: C++ Programs

//Q33. WAP to implement Upper triangular Matrix using one-dimensional array.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>#include<conio.h>class ut_matrix{

int t;public:

void pr_row(int j,int row_dim,int *p){

p=p+(j*row_dim);for(t=0;t<row_dim;++t){

if(t>j)cout<<*(p+t)<<" ";

elsecout<<"\n";

}}

};void main(){

clrscr();int n,num[10][10];cout<<"\nEnter the size of the matrix";cin>>n;cout<<"\nEnter the values";for(int i=0;i<n;i++)

for(int j=0;j<n;j++)cin>>num[i][j];

cout<<"\nMatrix is:\n";for(i=0;i<n;i++){

for(j=0;j<n;j++)cout<<num[i][j]<<" ";

cout<<"\n";}ut_matrix ob;cout<<"\nUpper triangular matrix implemented using 1-d is\n";for(i=0;i<n;i++)

ob.pr_row(i,n,(int *)num);getch();

}

Page 144: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT:

Enter the size of the matrix: 3

Enter the values: 9 8 7 6 5 4 3 2 1

Matrix is:9 8 76 5 43 2 1

Upper triangular matrix implemented using 1-d is

8 7 4

----------------------------------------------------------------------------------------------------------*/

Page 145: C++ Programs

/*Q no.34

WAP to implement Symmetric Matrix using one-dimensional array.

----------------------------------------------------------------------------------------------------------*/

#include<iostream.h>#include<conio.h>#define n 3class s_matrix{

int t;public:

void pr_row(int j, int row_dimension,int *p){

p=p+(j*row_dimension);for(t=0; t<row_dimension; t++)

cout<<*(p+t)<<" ";cout<<endl;

}};void main(){

clrscr();int num[3][3];cout<<"\n Enter the values of symmetric matrix:";for(int i=0; i<n; i++)

for(int j=0; j<n; j++)cin>>num[i][j];

cout<<"\nSymmetric matrix is:\n";for(i=0; i<n; i++){

for(int j=0; j<n; j++)cout<<num[i][j]<<" ";

cout<<endl;}s_matrix m;cout<<"\n Symmetric matrix implemented using 1-D:\n";for(i=0; i<n; i++)

m.pr_row(i,n,(int*)num);getch();

}

Page 146: C++ Programs

/*----------------------------------------------------------------------------------------------------------

OUTPUT

Enter the values of symmetric matrix:1 0 0 0 1 0 0 0 1Symmetric matrix is:1 0 0 0 1 00 0 1Symmetric matrix implemented using 1-D:1 0 0 0 1 00 0 1

----------------------------------------------------------------------------------------------------------*/