Você está na página 1de 145

PRACTICAL FILE

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.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) Sum
ii) Difference
iii) Product
iv) 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.
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 ‘+’ operator
ii) Subtraction, overload ‘-’ operator
iii) Multiply, overload ‘*’ operator
iv) Division, overload ‘/’ operator
v) 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 function
viii) Overload assignment operator
ix) Printing in the form a/b.
WAP to test the above class.

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 operator
vii) 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.

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 iteration
ii) using recursion

Ques.25: WAP to display fibonaccei series


i) using iteration
ii) using recursion

Ques.26: WAP to calculate GCD of 2 numbers


iii) using iteration
iv) using recursion

Ques.27: WAP to create a Binary Search Tree and include


following operations on tree:
i) Insertion
ii) Deletion by copying
iii) Deletion by Merging
iv) Serach a no. in BST
v) 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.

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.
/*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;
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

Enter string:god is great


String is not Palindrome
----------------------------------------------------------------------------------------------------------*/
//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";
}

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::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
{
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

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

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:2


Even elements are
2
4
6

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

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:3


Odd elements are
1
3
5

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

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:4


Sum of elements of array:21
Average of elements of array:3.5

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

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:5


Maximum element:6
Minimum element:1

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

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

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

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:7


Duplicate element removed
Element :1: 1
Element :2: 2
Element :3: 3
Element :4: 4
Element :5: 5
Element :6: 6

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

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:8


Enter starting and end point of compactoin:2
3
Element :1: 1
Element :2: 2
Element :3: 5
Element :4: 6
Element :5: 0
Element :6: 0
***----------** MENU **----------***

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:9


Reversed array
0
0
6
5
2
1

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

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;

//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++)
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";
}

//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();
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 matrix


Dimension 1:3
Dimension 2:2

Enter elements of Ist Matrix

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

Enter elements of IInd Matrix


Element 1:6
Element 2:5
Element 3:4
Element 4:3
Element 5:2
Element 6:1

Ist Matrix
1 2
3 4
5 6

IInd Matrix
6 5 4
3 2 1

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

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

Enter Your Choice:2


Matrix are not compatible for Addition

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

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

Enter Your Choice:3


Matrix are not compatible for Subtraction

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

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

Enter Your Choice:4


Product Matrix
4 2
12 4
20 6

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

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

Enter Your Choice:5


Transpose Of Ist Matrix
1 3 5
2 4 6
Transpose Of IInd Matrix
6 3
5 2
4 1

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

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

Enter Your Choice:6


Enter 0 to quit the program and any key to continue:0

You Have Quit the Program!!


----------------------------------------------------------------------------------------------------------*/
//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++)
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
**************************************************
**************************************************

Enter your choice: :2

Enter the item to be searched:-2


3 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
**************************************************
**************************************************

Enter your choice: :3


Enter the item to be searched:-6
3 2 1 5 4 6
Proceeding with Bsearch
Sorted array
1 2 3 4 5 6
The item 6 is at index: 5 & position: 6

----------------------------------------------------------------------------------------------------------*/
//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

Enter the size of array 6

Enter the array elements


1
3
2
5
6
4
1 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:


1
2
3
4
5
6

*********************************************
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 elements


23
21
25
65
31
11
23 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
21
23
25
31
65
*********************************************
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 elements


13
41
3
2
4
13 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
3
4
13
41

*********************************************
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)5


U Hav Quit the programme!!!
----------------------------------------------------------------------------------------------------------*/
//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);
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 AJMAK


Entered String:my name is XYANSJHS AJMAK

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

ENTER THE RADIUS OF THE CIRCLE:10


AREA OF CIRCLE IS:314

----------------------------------------------------------------------------------------------------------*/
//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:10


23

NUMBERS BEFORE SWAP IS:10 23


NUMBERS AFTER SWAP IS:23 10

----------------------------------------------------------------------------------------------------------*/
//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

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

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:2

Concatenated Strings without using Strcat :godGOD

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

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:3

Concatenated Strings using Strcat :godGOD

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

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:4

Length of Ist String :3


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

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:5


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

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:6

String 1 in Upper case:GOD


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

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:7

Strings are same


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

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

You Have Quit the Program!!

----------------------------------------------------------------------------------------------------------*/
//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;
}
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;
}

void rational::operator==(rational r5)


{
if((num==r5.num) && (den==r5.den))
cout<<"EQUAL";
else
cout<<"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();
}
/*----------------------------------------------------------------------------------------------------------

OUTPUT
Enter 2 Nos:10
33

CHECKING EQUALITY :EQUAL


ADDITION OF TWO RATIONAL NUMBERS:660/1089
SUBRACTION OF TWO RATIONAL NUMBERS:0/1089
MULTIPLICATION OF TWO RATIONAL NUMBERS:100/1089
DIVISION 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

----------------------------------------------------------------------------------------------------------*/
//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;
}
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.5


21.9
AREA OF TRIANGLE 3 IS:229.95
AREA OF TRIANGLE ONE IS:100
AFTER OVERLOADING OPERATOR -=::TRIANGLE ONE changed:
BASE IS:5
HEIGHT IS:14
AREA OF TRIANGLE ONE IS:35
AFTER OVERLOADING OPERATOR =::TRIANGLE ONE changed:
BASE IS:5
HEIGHT IS:6
AREA OF TRIANGLE ONE IS:15

----------------------------------------------------------------------------------------------------------*/
//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

----------------------------------------------------------------------------------------------------------*/
//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);
}

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

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

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

----------------------------------------------------------------------------------------------------------*/
//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:

TWO BOXES ARE NOT EQUAL


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

----------------------------------------------------------------------------------------------------------*/
//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

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 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

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:
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

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:
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 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

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

----------------------------------------------------------------------------------------------------------*/
//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

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:
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

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

----------------------------------------------------------------------------------------------------------*/
//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

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

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

ELEMENT DELETED IS:55


WANT TO CONTINUE(1 &0):1

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

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:5


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

----------------------------------------------------------------------------------------------------------*/
//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

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

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

Enter Your Choice:1


Enter Data:11

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

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

Want 2 continue(y/n):n

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

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

Enter Your Choice:2


Enter Data:10

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

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

Enter Your Choice:3


Enter Data:44

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

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

Enter Your Choice:6

1:Traverse from Left


2:Traverse from Right
Enter choice:1
10 11 22 33 44

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

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

Enter Your Choice:6

1:Traverse from Left


2:Traverse from Right
Enter choice:2
44 33 22 11 10
***----------** MENU **----------***
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

Enter Your Choice:4

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

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

Enter Your Choice:5

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

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

Enter Your Choice:6

1:Traverse from Left


2:Traverse from Right
Enter choice:1
11 22 33
***----------** MENU **----------***
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

Enter Your Choice:7


U Hav Quit The Program!!!

----------------------------------------------------------------------------------------------------------*/
//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

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

ENTER THE INFORMATION:3


ENTER THE INFORMATION:3

NEW POLYNOMIAL IS:


5X^2 + 5X + 5

----------------------------------------------------------------------------------------------------------*/
//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

ENTER THE NUMBER:15

FACTORIAL WITH RECURSION:


FACTORIAL IS:2004310016

FACTORIAL WITHOUT RECURSION:


FACTORIAL IS:2004310016

----------------------------------------------------------------------------------------------------------*/
//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

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

----------------------------------------------------------------------------------------------------------*/
//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

GCD of two numbers using recursion is: 5


GCD of two numbers without using recursion is: 5
----------------------------------------------------------------------------------------------------------*/
//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
{
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;
}
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;
}

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 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);
}
}
}

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


{
q=root;
int found=0;
while(q!=NULL)
{
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 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

Enter value & Enter 0 to quit:15

Enter next value:20

Enter next value:5

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?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

Total number of leaf nodes:3


Total number of non leaf nodes:5
Total number of nodes:8
Want to continue?y
Enter your choice:4

Mirror image using Breadth first


1520521102501
Want to continue?y
Enter your choice:5

Enter the node want to delete:5

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 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;
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;
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]);
}
}
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);
};

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;
}
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();
}

/*----------------------------------------------------------------------------------------------------------
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 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 array_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;
}

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";
else
stck[++top]=item;
}
int pop()
{
if(top<0)
cout<<"\nUnderflow";
else
return 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;
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 5
Reversed stack: 5 4 3 2 1

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

//Q31. WAP to implement Diagonal Matrix using one-dimensional array.


//----------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#define n 3
class 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];
else
num[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();
}

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

/OUTPUT:
Enter the values2 3 4

The matrix is:


200
030
004

Diagonal matrix implemented using 1-d is


234

----------------------------------------------------------------------------------------------------------*/
//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:

Enter the size of the matrix: 3

Enter the values: 9 8 7 6 5 4 3 2 1

Matrix is:
987
654
321

Lower triangular matrix implemented using 1-d is

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:

Enter the size of the matrix: 3

Enter the values: 9 8 7 6 5 4 3 2 1

Matrix is:
987
654
321

Upper triangular matrix implemented using 1-d is

87

----------------------------------------------------------------------------------------------------------*/
/*Q no.34

WAP to implement Symmetric Matrix using one-dimensional array.

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

#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

Enter the values of symmetric matrix:1 0 0 0 1 0 0 0 1


Symmetric matrix is:
100
010
001
Symmetric matrix implemented using 1-D:
100
010
001

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

Você também pode gostar