Print Friendly and PDF
#include<iostream> 
#include<conio.h>
#include<cstdlib>
#include<vector> 
#include <stdio.h>
#include<algorithm>
using namespace std;
class combine 
{
public:
void factorial() 

int num ;
char c;
cout<<"Enter Number To Find Its Factorial: ";
cin>>num;int factorial = 1;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of given number is "<< factorial <<endl; 
}
void sum() 
{
int sum, num1,num2;
cout<<"Enter two digits for their sum: \n";
cin>>num1>>num2;sum=num1+num2;
cout<<"sum is : "<<sum;
}
void bubble_sort(vector<int>& a) 
{
for (int i = a.size(); i > 0;i--)
{
for (int j = 0, k = 1; k < i;j++, k++)
{
if (a[j] > a[k])
{
int swap = a[j];
a[j] = a[k];
a[k] = swap;
}
}
}
}
void mergeArrays(int a[], int asize, int b[], int bsize ,int tmp[])
{
int l=0,j=0;
for(int k=0; k<asize+bsize; k++)
{
if(l==asize)
{
tmp[k]=b[j];
j++;
}
else if(j==bsize)
{
tmp[k]=a[l];
l++;
}
else if(a[l]<=b[j])
{
tmp[k]=a[l];
l++;
}
else if(b[j]<a[l])
{
tmp[k]=b[j];
j++;
}
}
}
void mergeSortRec(int array[],int size,int tmp[])
{
int i;
int mid = size/2;
if (size > 1)
{
mergeSortRec(array, mid, tmp);
mergeSortRec(array+mid, size-mid, tmp);
mergeArrays(array,mid, array+mid, size-mid, tmp);
for (i = 0; i < size; i++)
array[i] = tmp[i];
}
}
int part(int low,int high,int *a) 
{
int i,h=high,l=low,p,t;
p=a[low];
while(low<high)
{
while(a[l]<p)
{
l++;
}
while(a[h]>p)
{
h--;
}
if(l<h)
{
t=a[l];
a[l]=a[h];
a[h]=t;
}
else
{
t=p;
p=a[l];
a[l]=t;
break;
}
}
return h;
}
void quick(int l,int h,int *a)
{
int index,i;
if(l<h)
{
index=part(l,h,a);
quick(l,index-1,a);
quick(index+1,h,a);
}
}
int insertion()
{
int data[100],n,i,j,hold,k;
printf("Enter number of terms(should be less than 100): ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<=n-1;++i)
{
scanf("%d",&data[i]);
}
for(i=1;i<=n-1;++i)
{
for(j=0;j<i;++j)

if(data[j]>data[i])
{
hold=data[i];
k=i;
while(k!=j)
{
data[k]=data[k-1];
--k;
}
data[j]=hold;
}
}
printf("In sacending Order: ");
for(i=0;i<=n-1;++i)
{
printf("%d ",data[i]);
}
return 0;
}
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] )
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
}
}
}
void stackimp()
{
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};

/*===================================================================
STACK IMPLEMENTON
==================================================================*/
int ch;
stack st;
while(1)
{
cout<<"________________"<<endl;
cout<<"Enter the Option"<<endl;
cout<<"________________"<<endl;
cout<<"1.push" <<endl;
cout<<"2.pop " <<endl;
cout<<"3.display" <<endl;
cout<<"4.exit:" <<endl;
cout<<"Enter ur choice" <<endl;
cout<<"________________"<<endl;
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
}
void queueimp()
{
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
int ch;
/*=========================================================================
QUEUE IMPLEMENTATION
========================================================================*/
queue qu;
while(1)
{
cout<<"________________" <<endl;
cout<<"Enter the Option:"<<endl;
cout<<"________________" <<endl;
cout<<"1.insert" <<endl;
cout<<"2.delet " <<endl;
cout<<"3.display"<<endl;
cout<<"4.exit" <<endl;
cout<<"Enter ur choice";
cout<<"________________"<<endl;
cin >> ch;
switch(ch)
{
case 1: 
cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: 
qu.delet(); break;
case 3: 
qu.display();break;
case 4:
exit(0);
}
}
}
void treeimp()
{
struct tree_node
{
 int data;tree_node *left;
tree_node *right;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data) 
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void inordertrav()
{
inorder(root);
}
void inorder(tree_node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<" "<<ptr->data<<" ";
inorder(ptr->right);
}
}
void postordertrav()
{
postorder(root);
}
void postorder(tree_node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<" "<<ptr->data<<" ";
}
}
void preordertrav()
{
preorder(root);
}
void preorder(tree_node *ptr)
{
if(ptr!=NULL)
{
cout<<" "<<ptr->data<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
};
bst b;
int c;
cout<<"How many nodes in the tree?";
int r;
cin>>r
cout<<"Enter nodes:\n";
for(int i=0; i<r; i++)
{
cin>>c;
b.insert(c);
}
cout<<"inorder"<<endl;
b.inordertrav();
cout<<endl<<"postorder"<<endl;
b.postordertrav();
cout<<endl<<"preorder"<<endl;
b.preordertrav();
}
void listimp()
{
class Node
{
private :
int data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};
class List
{
Node *head;
public:
List() { head = NULL; };
void Print()
{
Node *tmp = head;
while ( tmp != NULL )
{
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
cout << "NULL" << endl;
}
void Add(int data)
{
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
Node *tmp = head;
if ( tmp != NULL )
{
while ( tmp->Next() != NULL )
{
tmp = tmp->Next();
}
tmp->SetNext(newNode);
}
else
{
head = newNode;
}
}
void Append()
{Node* newNode = new Node();
Node *tmp = head;
Node *temp = head;
int n,data;
cout<<"Enter the node number : ";
cin>>n;
cout<<"Enter the data : ";
cin>>data;
newNode->SetData(data);

for( int i=1; i<n; i++)
{
 tmp = tmp->Next();
 }temp = tmp->Next();
tmp->SetNext(newNode);
newNode->SetNext(temp);
}
void find()
{
int n, count = 0;
cout<<"Enter data to find in the list : ";
cin>>n;
Node *tmp = head;
while ( tmp != NULL )

count = count + 1;
if( n == tmp->Data())
{
cout<<"The data found on node number "<< count <<endl ;
break;
}
tmp = tmp->Next();
}
}
void Delete()
{
int n, count = 0;
cout<<"Enter data to delete : ";
cin>>n;
Node *tmp = head;
Node *temp = head;
while ( tmp != NULL )
{
count = count + 1;
if( n == tmp->Data())
{
break;
}
tmp = tmp->Next();
}
for( int j = 1; j < count-1; j++ )
{
temp = temp->Next();
}
temp->SetNext(tmp->Next());
delete tmp;
}
} ;
int n;
char c;
List mylist;
mylist.Add(5);
mylist.Add(10);
mylist.Add(15);
mylist.Add(20);
mylist.Add(25);
mylist.Add(30);
mylist.Add(35);
mylist.Add(40);
mylist.Add(45);
Choice:
cout<<"_______________________________________________" <<endl;
cout<< "Plz Enter the choice :"<<endl;
cout<<"_______________________________________________" <<endl;
cout<<" A) To add node at the end of list : " <<endl;
cout<<" B) To add node anywhere in the list : " <<endl;
cout<<" C) To find data in the list : " <<endl;
cout<<" D) To delete data from the list :" <<endl;
cout<<" E) To display the list " <<endl;
cout<<" F) To exit " <<endl;
cout<<"_______________________________________________" <<endl;
cin>>c;
switch(c)
{
case 'A':
cout<<"Enter data you want to enter in the last node : ";
cin>>n;
mylist.Add(n);
goto Choice;
break;
case 'B':
mylist.Append();goto Choice;
break;
case 'C':
mylist.find();
goto Choice;
break;
case 'D':
mylist.Delete();
goto Choice;
break;
case 'E':
mylist.Print();
goto Choice;
break;
case 'F':
break;
default:
cout<<"\n\n Wrong Choice .";
goto Choice;
}
}
void heapimp()
{
class heap
{
int a[30],n;
int x;
public:
void read()
{
cout<<"Enter the size\n";
cin>>n;
x=n;
cout<<"Enter the elements\n";
for(int i=1;i<=n;i++)cin>>a[i];
}

void maxd()
{
swap(a[1],a[n]);
n--;
heapsort();
}
void disp()
{
cout<<"Sorted Array is\n";
for(int i=1;i<=x;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void heapsort()
{
if(n==0)return;
int i,k,v,h,j;
for(i=n/2;i>0;i--)
{
k=i;
v=a[k];
h=0;
while(!h&&2*k<=n)
{
j=2*k;
if(j<n)
{
if(a[j]<a[j+1])j++;
}
if (v>=a[j])
h=1;
else
{
a[k]=a[j];
k=j;
}
}
a[k]=v;
}
maxd();
}
};
heap H;
H.read();
H.heapsort();
H.disp();
}
};
int main()
{
system("color 3f");
int ch;
string user;
string pass;
cout<<"\t\t\tEnter Username: ";
cin>>user;
cout<<"\t\t\tEnter Password: ";
cin>>pass;
if(user=="zubair" && pass=="12345")
{
/*========================================
MUNE OF PROGRAMM
==========================================
combine cm;
while(1)
{
cout<<"________________"<<endl;
cout<<"Enter The option"<<endl;
cout<<"________________"<<endl;
cout<<"1. Factorial" <<endl;
cout<<"2. Sum" <<endl;
cout<<"3. Bubble " <<endl;
cout<<"4. Mergesort" <<endl;
cout<<"5. Quicksort "<<endl;
cout<<"6. Insertion "<<endl;
cout<<"7. selection "<<endl;
cout<<"8. Stack" <<endl;
cout<<"9. Queue " <<endl;
cout<<"10. Tree " <<endl;
cout<<"11.List " <<endl;
cout<<"12.Heap" <<endl;
cout<<"13.Exit" <<endl;
cout<<"________________"<<endl;
cout<<"Enter ur choice: ";
cin >> ch;
switch(ch)
{
case 1:
cm.factorial();
break;
case 2:
cm.sum();
break;
case 3:
{
int alen, val;
vector<int> a;
cout << "Enter the number of elements : ";
cin >> alen;
for(int i = 0; i < alen; i++)
{
cin >> val;
a.push_back(val);
}
cm.bubble_sort(a);
cout << "List of sorted elements: " << endl;
for(int i = 0; i < alen; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
break;
case 4:
{
int N=10;
int array[10];
int tmp[10];
cout<<"enter 10 different numbers :"<<endl;
for(int i=0; i<N;i++)
{
cin>>array[i];
}
cout<<endl;
cm.mergeSortRec(array, N, tmp);
for(int j=0; j<N; j++)
{
cout<<array[j];
cout<<endl;
}
}
break;
case 5:
{
int a[100],n,l,h,i;
cout<<"Enter number of elements:";
cin>>n;
cout<<"Enter the elements [Use Space As A Separator]:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nInitial Array:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
h=n-1;
l=0;
cm.quick(l,h,a);
cout<<"\nAfter Sorting:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
break;
getch();
}
case 6:
cm.insertion();
case 7:
int arr[10],size;
cout<<" Enter the no. of elements that you want to enter [max 10 ] : ";
cin>>size;
cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
cout<<" \n Element "<<i<<" : ";
cin>>arr[i];
}
cm.selectionSort(arr,size);
cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
cout<<" \n Element "<<i<<" : "<<arr[i];
}
case 8:
{
cm.stackimp();
}
case 9:
{
cm.queueimp();
}
case 10;
{
cm.treeimp();
}
case 11:
{
cm.listimp();
}
case 12:
{
cm.heapimp();
}
case 13: exit(0);
}
}
}
else
{cout<<"\n\n\t\tWrong Username or Password!!!\n\n";
return main();
}
return (0);
}





zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: