C++ Programs
C++ Programs
PRESENT
ED BY:-
RINKY SACHDEVA
BSc. Comp. Sc.(H)
Ist year(IInd
sem)
Roll No. :-
206-Software Lab based on 201 (C++ and Data Structures)
Questions
Ques.9: Write a macro that swaps two numbers. WAP to use it.
Ques.23: WAP to scan a polynomial using linked list and add two
polynomials.
----------------------------------------------------------------------------------------------------------*/
#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;
else
r=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
{
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:28
Product:5040
Reversed Number:7654321
----------------------------------------------------------------------------------------------------------*/
/*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";
else
cout<<"String is Palindrome";
}
int main()
{
ob.read();
ob.check();
getch();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
Enter string:malayalam
String is Palindrome
//----------------------------------------------------------------------------------------------------------
#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";
}
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];
}
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::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
{
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;
}
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;
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
Enter size of array:6
Enter elements of array
Element 1:1
Element 2:3
Element 3:2
Element 4:5
Element 5:4
Element 6:6
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
Enter Your Choice:6
Sorted array
1
2
3
4
5
6
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
1 :Re-Enter array
2 :Print Even elements of array
3 :Print Odd elements of array
4 :Print Sum and Average of array elements
5 :Print Maximum and Minimum element of array
6 :Sort Array
7 :Remove Duplicate elements from array
8 :Compact array
9 :Print Reverse of Array
10 :To quit the program
Enter Your Choice:10
Final array
Element :1: 1
Element :2: 2
Element :3: 5
Element :4: 6
Element :5: 0
Element :6: 0
To Quit the program press 'q' and 'c' to continue
q
You Have Quit the Programme!!!!!!!!!!!!
--------------------------------------------------------------------------------------------------------- */
//Q4 Perform Matrix Operations like ADD, SUBTRACT, MULTIPLY And
TRANSPOSE
//----------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
//class definition
class 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;
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++)
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";
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)
{
for(int j=0;j<q;++j)
cout<<b[j][i]<<" ";
cout<<"\n";
}
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();
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
Element 1:1
Element 2:2
Element 3:3
Element 4:4
Element 5:5
Element 6:6
Enter Dimension of IInd Matrix
Dimension 1:2
Dimension 2:3
Ist Matrix
1 2
3 4
5 6
IInd Matrix
6 5 4
3 2 1
1 :Re-Enter Matrix
2 :Add the Matrix
3 :Subtract the Matrix
4 :Multiply the Matrix
5 :Transpose the Matrix
6 :To quit the program
1 :Re-Enter Matrix
2 :Add the Matrix
3 :Subtract the Matrix
4 :Multiply the Matrix
5 :Transpose the Matrix
6 :To quit the program
1 :Re-Enter Matrix
2 :Add the Matrix
3 :Subtract the Matrix
4 :Multiply the Matrix
5 :Transpose the Matrix
6 :To quit the program
1 :Re-Enter Matrix
2 :Add the Matrix
3 :Subtract the Matrix
4 :Multiply the Matrix
5 :Transpose the Matrix
6 :To quit the program
1 :Re-Enter Matrix
2 :Add the Matrix
3 :Subtract the Matrix
4 :Multiply the Matrix
5 :Transpose the Matrix
6 :To quit the program
//----------------------------------------------------------------------------------------------------------
#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++)
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;
else
last=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];
}
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.";
}
getch();
clrscr();
}
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
Size of array:6
Enter Elements of array
3
2
1
5
4
6
**************************************************
Select the technique by which you want to proceed:
**************************************************
1.Read Array
2.Linear Search
3.Binary Search
4.Exit
**************************************************
**************************************************
**************************************************
Select the technique by which you want to proceed:
**************************************************
1.Read Array
2.Linear Search
3.Binary Search
4.Exit
**************************************************
**************************************************
----------------------------------------------------------------------------------------------------------*/
//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;
}
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 ";
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);
break;
}
default:
cout<<"\n\t Wrong Selection .Try again(1-4)";
}
}
while(ch!=5);
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
*********************************************
Select the technique You want to proceed with
*********************************************
1.Read Array
2.Bubble sort
3.Selection sort
4.Insertion sort
5.Exit
*********************************************
*********************************************
*********************************************
Select the technique You want to proceed with
*********************************************
1.Read Array
2.Bubble sort
3.Selection sort
4.Insertion sort
5.Exit
*********************************************
*********************************************
*********************************************
Select the technique You want to proceed with
*********************************************
1.Read Array
2.Bubble sort
3.Selection sort
4.Insertion sort
5.Exit
*********************************************
*********************************************
*********************************************
Select the technique You want to proceed with
*********************************************
1.Read Array
2.Bubble sort
3.Selection sort
4.Insertion sort
5.Exit
*********************************************
*********************************************
*********************************************
Select the technique You want to proceed with
*********************************************
1.Read Array
2.Bubble sort
3.Selection sort
4.Insertion sort
5.Exit
*********************************************
*********************************************
//----------------------------------------------------------------------------------------------------------
#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);
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
a :1
b :0
c :0
d :0
e :1
f :0
g :0
h :0
i :1
j :0
k :0
l :0
m :2
n :1
o :0
p :0
q :0
r :0
s :1
t :0
u :0
v :0
w :0
x :0
y :1
z :0
A :3
B :0
C :0
D :0
E :0
F :0
G :0
H :1
I :0
J :2
K :1
L :0
M :1
N :1
O :0
P :0
Q :0
R :0
S :2
T :0
U :0
V :0
W :0
X :1
Y :1
Z :0
----------------------------------------------------------------------------------------------------------*/
//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
----------------------------------------------------------------------------------------------------------*/
//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
----------------------------------------------------------------------------------------------------------*/
//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);
}
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;
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];
}
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";
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:
{
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
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
1 :Read Strings
2 :Concatnate without STRCAT
3 :Concatenate with STRACAT
4 :Length of Strings
5 :Convert 2 Lower
6 :Convert 2 Upper
7 :Compare The Strings
8 :Count The Vowels
9 :Reverse
10 :To quit the program
Enter Your Choice:10
----------------------------------------------------------------------------------------------------------*/
//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;
}
rational rational::operator++()
{
num++;
den++;
return *this;
}
rational rational::operator--()
{
num--;
den--;
return *this;
}
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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
Enter 2 Nos:10
33
----------------------------------------------------------------------------------------------------------*/
//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;
}
----------------------------------------------------------------------------------------------------------*/
//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
----------------------------------------------------------------------------------------------------------*/
//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;
};
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);
}
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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
NAME OF PERSON:xyz
NAME OF PERSON:aaa
AGE OF THE PERSON:21
----------------------------------------------------------------------------------------------------------*/
//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--;
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";
else
cout<<"\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";
else
cout<<"\nIT IS A CUBOID";
}
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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
Enter Dimension of Box1:1
2
3
Enter Dimension of Box2:2
3
4
CHECKING EQUALITY:
----------------------------------------------------------------------------------------------------------*/
//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;
else
x=x->next;
}
if(found==1)
cout<<"\nNUMBER FOUND";
else
cout<<"\nNOT FOUND";
}
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;
}
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;
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;
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;
}
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:
exit(0);
}
cout<<"\nWANT TO CONTINUE(1&0):";
cin>>ch;
}
while(ch!=0);
getch();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
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 1
ROLL NO. :10
INFORMATION OF STUDENT 2
ROLL NO. :22
INFORMATION OF STUDENT 3
ROLL NO. :9
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:2
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
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:8
INFORMATION OF STUDENT 1
ROLL NO. :11
INFORMATION OF STUDENT 2
ROLL NO. :10
INFORMATION OF STUDENT 3
ROLL NO. :22
INFORMATION OF STUDENT 4
ROLL NO. :9
INFORMATION OF STUDENT 5
ROLL NO. :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:11
----------------------------------------------------------------------------------------------------------*/
//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;
else
x=x->next;
}
if(found==1)
cout<<"\nNUMBER FOUND";
else
cout<<"\nNOT FOUND";
}
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;
}
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;
}
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);
}
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:
{
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):";
cin>>ch;
}
while(ch!=0);
getch();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
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
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:3
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 1
ROLL NO. :10
INFORMATION OF STUDENT 2
ROLL NO. :11
INFORMATION OF STUDENT 3
ROLL NO. :22
INFORMATION OF STUDENT 4
ROLL NO. :33
INFORMATION OF STUDENT 5
ROLL NO. :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:
10.SEARCHING:
11.EXIT:
ENTER YOUR OPTION:5
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
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
----------------------------------------------------------------------------------------------------------*/
//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;
else
x=x->next;
}
if(found==1)
cout<<"\nNUMBER FOUND";
else
cout<<"\nNOT FOUND";
}
template<class T>
void stud<T>::show()
{
count=1;
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;
}
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:";
}
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:";
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();
break;
}
case 6:
exit(0);
}
cout<<"\nWANT TO CONTINUE(1&0):";
cin>>ch;
}
while(ch!=0);
getch();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
MENU:
1.CREATING A LINKED LIST:
2.INSERTING:
3.DELETING :
4.SHOW LIST:
5.SEARCHING:
6.EXIT:
ENTER YOUR OPTION:2
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:4
INFORMATION 1
ROLL NO. :33
INFORMATION 2
ROLL NO. :11
INFORMATION 3
ROLL NO. :15
INFORMATION 4
ROLL NO. :22
WANT 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
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
----------------------------------------------------------------------------------------------------------*/
//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;
}
}
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;
}
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
1.INSERT:
2.DELETION:
3.DISPLAY:
4.EXIT:
ENTER YOUR CHOICE:2
1.INSERT:
2.DELETION:
3.DISPLAY:
4.EXIT:
ENTER YOUR CHOICE:3
INFORMATION 1: 44
INFORMATION 2: 22
INFORMATION 3: 33
INFORMATION 4: 11
WANT TO CONTINUE(1 &0):1
1.INSERT:
2.DELETION:
3.DISPLAY:
4.EXIT:
ENTER YOUR CHOICE:4.
----------------------------------------------------------------------------------------------------------/*
//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:";
else
stck[++top]=item;
}
T pop()
{
if(top<0)
cout<<"\nUNDERFLOW:";
else
return 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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
----------------------------------------------------------------------------------------------------------*/
//Q21.CIRCULAR QUEUE ARRAY IMPLEMENTION .........
//----------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#define MAX 100
template<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];
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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
HOW MANY ELEMENT YOU WANT TO ADD:5
ENTER THE ELEMENT:1 2 3 4 5
ELEMENT DELETED IS:1
QUEUE IS:2 3 4 5
----------------------------------------------------------------------------------------------------------*/
//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;
}
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;
}
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";
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!!!!!";
}
}
while(ch!=0);
getch();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
Want 2 continue(y/n):y
Enter Data:22
Want 2 continue(y/n):y
Enter Data:33
Want 2 continue(y/n):n
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
1 :Create Queue
2 :Insert At Left
3 :Insert At Right
4 :Delete At Left
5 :Delete At Right
6 :Display Queue
7 :Quit the Programme
----------------------------------------------------------------------------------------------------------*/
//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();
}
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
----------------------------------------------------------------------------------------------------------*/
//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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
----------------------------------------------------------------------------------------------------------*/
//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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
----------------------------------------------------------------------------------------------------------*/
//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);
}
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
Enter two numbers: 20 45
//----------------------------------------------------------------------------------------------------------
#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 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;
}
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);
}
void binary::inorder(binary *p)
{
if(p!=NULL)
{
inorder(p->left);
cout<<p->data<<" ";
inorder(p->right);
}
else
return;
}
}
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 number
p=search(i,&p1);
if(p->left!=NULL)
Queue.add(p->left->data);
if(p->right!=NULL)
Queue.add(p->right->data);
}
}
}
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 numbers
p=search(i,&p1);
if(p->right!=NULL)
Queue.add(p->right->data);
if(p->left!=NULL)
Queue.add(p->left->data);
}
}
}
}
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 number
q=search(i,&p1);
if(q==NULL)
{
cout<<"\nElement not found";
return;
}
getch();
clrscr();
//node to be deleted has no child
if((q->right==NULL)&&(q->left==NULL))
{
if(p1->right==q)
p1->right=NULL;
else
p1->left=NULL;
delete q;
}
//node to be deleted has no left child
if((q->right!=NULL)&&(q->left==NULL))
{
if(p1->right==q)
p1->right=q->right;
else
p1->left=q->right;
delete q;
}
//node to be deleted has no right child
if((q->right==NULL)&&(q->left!=NULL))
{
if(p1->right==q)
p1->right=q->left;
else
p1->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;
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();
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.Insert
2.Traverse
3.Calculate leaf
4.Mirror Image
5.Delete
Enter your choice:1
Want to continue?y
Enter your choice:2
Inorder Traversal:1 2 5 10 15 20 21 50
Preorder Traversal:15 5 2 1 10 20 21 50
Postorder Traversal:1 2 10 5 50 21 20 15
Breadth Traversal:15 5 20 2 10 21 1 50
Want to continue?y
Enter your choice:3
1.Copying
2.Merging1
Deletion by copying
Inorder Traversal:1 2 10 15 20 21 50
Preorder Traversal:15 2 1 10 20 21 50
Postorder Traversal:1 10 2 50 21 20 15
Breadth Traversal:15 2 20 1 10 21 50
Want to continue?n
----------------------------------------------------------------------------------------------------------*/
//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];
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 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]);
}
}
cout<<"\n CONVERTED INTO NON ZERO FORM:";
s.show_list();
}
getch();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
ENTER THE ELEMENTS:1 2 3 4 5 6 7 8 9
MATRIX IS:
123
456
789
----------------------------------------------------------------------------------------------------------*/
//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);
};
/*----------------------------------------------------------------------------------------------------------
OUTPUT:
Original stack is
Information1: 10
Information2: 8
Information3: 6
Information4: 4
Information5: 2
Reversed stack is
Information1: 2
Information2: 4
Information3: 6
Information4: 8
Information5: 10
----------------------------------------------------------------------------------------------------------*/
//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 main()
{
clrscr();
int n;
int item;
stack s,s1;
array_queue a;
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:
Original stack: 1 2 3 4 5
Reversed stack: 5 4 3 2 1
----------------------------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------------------------
/OUTPUT:
Enter the values2 3 4
----------------------------------------------------------------------------------------------------------*/
//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)<<" ";
else
cout<<"\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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT:
Matrix is:
987
654
321
32
----------------------------------------------------------------------------------------------------------*/
//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)<<" ";
else
cout<<"\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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT:
Matrix is:
987
654
321
87
----------------------------------------------------------------------------------------------------------*/
/*Q no.34
----------------------------------------------------------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#define n 3
class 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();
}
/*----------------------------------------------------------------------------------------------------------
OUTPUT
----------------------------------------------------------------------------------------------------------*/