#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int num,temp;
int factor=1;
cout<<"Enter a number: ";
cin>>num;
temp=num;
while(temp)
{
temp=temp/10;
factor = factor*10;
}
cout<<"Relevant digits of "<<num<<" are.\n";
while(factor>1)
{
factor=factor/10;
cout<<num/factor<<"\t";
num=num%factor;
}
cout<<"\n";
return 0;
}
Sunday, 31 August 2014
Factorial of an Integer
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
long int num;
int i=1,fact=1;
cout<<"Enter an integer:";
cin>>num;
while(i<=num)
{
fact=fact*i;
i++;
}
cout<<"Factorial of number is : "<<fact<<"\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
long int num;
int i=1,fact=1;
cout<<"Enter an integer:";
cin>>num;
while(i<=num)
{
fact=fact*i;
i++;
}
cout<<"Factorial of number is : "<<fact<<"\n";
return 0;
}
Find minimum number without using Comparison Operator
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3;
cout<<"Please enter three numbers :\n";
cin>>num1>>num2>>num3;
int min=0;
while(num1 && num2 && num3)
{
num1--;num2--;num3--;min++;
}
cout<<"Minimum is "<<min<<"\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3;
cout<<"Please enter three numbers :\n";
cin>>num1>>num2>>num3;
int min=0;
while(num1 && num2 && num3)
{
num1--;num2--;num3--;min++;
}
cout<<"Minimum is "<<min<<"\n";
return 0;
}
Friday, 29 August 2014
Amazing Series of Numbers
//Output : 99 98 97 1 96 95 94 2 93 92 91 3 up to 1
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int b=1;
for(int a=99;a>=1;)
{
//cout<<a--<<","<<a--<<","<<a--<<","<<b++<<","<<"\n";
cout<<a--<<",";
cout<<a--<<",";
cout<<a--<<",";
cout<<b++<<","<<"\n";
}
return 0;
}
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int b=1;
for(int a=99;a>=1;)
{
//cout<<a--<<","<<a--<<","<<a--<<","<<b++<<","<<"\n";
cout<<a--<<",";
cout<<a--<<",";
cout<<a--<<",";
cout<<b++<<","<<"\n";
}
return 0;
}
Maximum value in Array
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int ary[10],max;
for(int i=1;i<10;i++)
{
cout<<"ENTER MARKS OF STUDENT NO. "<<i<<endl;
cin>>ary[i];
}
max=ary[0];
for(int i=0;i<10;i++)
if(max<ary[i])
max=ary[i];
cout<<"HIGHEST MARKS ARE "<<max<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int ary[10],max;
for(int i=1;i<10;i++)
{
cout<<"ENTER MARKS OF STUDENT NO. "<<i<<endl;
cin>>ary[i];
}
max=ary[0];
for(int i=0;i<10;i++)
if(max<ary[i])
max=ary[i];
cout<<"HIGHEST MARKS ARE "<<max<<endl;
return 0;
}
Binary Search Tree with all Operations
#include "stdafx.h"
#include<iostream>
#include<conio.h>>
using namespace std;
int flag=0;
struct Node
{
int data;
Node *left;
Node *right;
};
class BST
{
private:
int check1;
int check2;
public:
Node *root;
BST()
{
root=NULL;
check1=0;
check2=1;
}
void find_parent(int data,int &found,Node* &parent)
{
Node *ptr;
found=check1;
parent=NULL;
ptr=root;
while(ptr!=NULL)
{
if(ptr->data==data)
{
found=check2;
}
if(ptr->data>data)
{
parent=ptr;
ptr=ptr->left;
}
else
{
parent=ptr;
ptr=ptr->right;
}
}
}
void insert_values(int data)
{
int found;
Node *temp,*parent;
find_parent(data,found,parent);
if(found==check2)
{
cout<<"\nData with such value already exists\n";
}
else
{
temp=new Node;
temp->data=data;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
{
root=temp;
}
else if(parent->data>data)
{
parent->left=temp;
}
else
{
parent->right=temp;
}
}
}
void in_fix(Node *ptr)
{
if(ptr!=NULL)
{
in_fix(ptr->left);
cout<<ptr->data<<"\t";
in_fix(ptr->right);
}
}
void post_fix(Node *ptr,int flag)
{
flag++;
if(ptr!=NULL)
{
post_fix(ptr->left,flag);
post_fix(ptr->right,flag);
cout<<"\n\n\t\tData : Level\n";
cout<<"\n\t\t"<<ptr->data<<" : "<<flag<<endl;
}
}
void pre_fix(Node *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<"\t";
pre_fix(ptr->left);
pre_fix(ptr->right);
}
}
void traverse_BST()
{
int choice;
cout<<"\n1)In-fix\n2)Pre-fix\n3)Post_fix\n\n\t\tWhat is your chice???\n";
cin>>choice;
if(choice==1)
{
in_fix(root);
}
else if(choice==2)
{
pre_fix(root);
}
else
{
post_fix(root,flag);
}
}
bool search_node(int search,Node *ptr )
{
if (root==NULL)
{
return false;
}
else if (search<ptr->data)
{
return search_node(search,ptr->left);
}
else if (search>ptr->data)
{
return search_node(search,root->right );
}
else
{
return true;
}
}
int get_smallest(Node *ptr)
{
if (ptr->left==NULL)
{
return ptr->data;
}
else
{
return get_smallest(ptr->left);
}
}
int get_highest(Node *ptr)
{
if (ptr->right==NULL)
{
return ptr->data;
}
else
{
return get_highest(ptr->right);
}
}
void get_degree(Node *temp,int data)
{
int counter=0;
if(temp->data==data)
{
if(temp->left!=NULL)
{
counter++;
}
if(temp->right!=NULL)
{
counter++;
}
cout<<counter<<endl;
}
if(temp->left!=NULL)
{
get_degree(temp->left,data);
}
if(temp->right!=NULL)
{
get_degree(temp->right,data);
}
}
void recur_delete(int d,int &found,Node *&parent,Node *&x)
{
Node *ptr;
found=0;
if(root==NULL)
return;
ptr=root;
while(ptr!=NULL)
{
if(ptr->data==d)
{
found=1;
x=ptr;
return;
}
if(ptr->data>d)
{
parent=ptr;
ptr=ptr->left;
}
else
{
parent=ptr;
ptr=ptr->right;
}
}
}
void delete_node(int value)
{
Node *parent,*curr,*next;
int found;
if(root==NULL)
{
cout<<"\nBST is Empty!!!\n";
return;
}
parent=curr=NULL;
recur_delete(value,found,parent,curr);
if(found==0)
{
cout<<"\nNode with such value is not present!!!\n";
return;
}
if(curr->left!=NULL && curr->right!=NULL)
{
parent=curr;
next=curr->right;
while(next!=NULL)
{
parent=curr;
next=next->left;
}
curr->data=next->data;
curr=next;
}
if(curr->left==NULL && curr->right==NULL)
{
if(parent->right==curr)
{
parent->right=NULL;
}
else
{
parent->left=NULL;
}
delete curr;
return;
}
if(curr->left==NULL && curr->right!=NULL)
{
if(parent->left==curr)
{
parent->left=curr->right;
}
else
{
parent->right=curr->right;
}
delete curr;
return;
}
if(curr->left!=NULL && curr->right==NULL)
{
if(parent->left==curr)
{
parent->left=curr->left;
}
else
{
parent->right=curr->left;
}
delete curr;
return;
}
}
void show_min()
{
cout<<"\n\nMinum Value : "<<get_smallest(root);
}
void show_max()
{
cout<<"\n\nMaximum Value : "<<get_highest(root);
}
void search_result()
{
int search;
cout<<"\nEnter an Integer to Search : ";
cin>>search;
if(search_node(search,root)==true)
{
cout<<"\n"<<search<<" is present in BST!!!\n";
}
else
{
cout<<"\n"<<search<<" is not present in BST!!!\n";
}
}
void show_degree()
{
int data;
cout<<"Enter a value to find its degree :\n";
cin>>data;
cout<<"\nDegree of "<<data<<" is ";
get_degree(root,data);
}
BST(BST &s1)
{
root=new Node;
root->left=NULL;
root->right=NULL;
root->data=s1.root->data;
copy(root,s1.root);
}
void copy(Node *temp,Node *ptr)
{
if(ptr->left!=NULL)
{
temp->left=new Node;
temp->left->data=ptr->left->data;
temp->left->left=temp->left->right=NULL;
}
if(ptr->right!=NULL)
{
temp->right=new Node;
temp->right->data=ptr->right->data;
temp->right->left=temp->right->right=NULL;
}
if(ptr->left!=NULL)
{
copy(temp->left,ptr->left);
}
if(ptr->right!=NULL)
{
copy(temp->right,ptr->right);
}
}
};
int main()
{
BST s1;
int choice;
do
{
cout<<"\n\n\t\t\t-----Main Menu-----\n";
cout<<"\n1)Add New\n2)Traverse\n3)Get Degree\n4)Get Level \n5)Search a Node\n6)Get Minimum \n7)Get Maximum\n8)Delete\n9)Copy Constructor\n10)Quit\n";
cout<<"\n\t\t\tPlease select your choice : (1-10)\n";
cin>>choice;
if(choice==1)
{
int value;
char choice;
cout<<"\nEnter data :";
cin>>value;
s1.insert_values(value);
do
{
cout<<"\nDo you want more ??? (y/n)\n";
cin>>choice;
if(choice=='y')
{
cout<<"\nEnter data :";
cin>>value;
s1.insert_values(value);
}
}
while(choice!='n');
}
else if(choice==2)
{
s1.traverse_BST();
}
else if(choice==3)
{
s1.show_degree();
}
else if(choice==5)
{
s1.search_result();
}
else if(choice==6)
{
s1.show_min();
}
else if(choice==7)
{
s1.show_max();
}
else if(choice==8)
{
int value;
cout<<"\nEnter a value to delete : ";
cin>>value;
s1.delete_node(value);
s1.traverse_BST();
}
else if(choice==9)
{
BST s2=s1;
cout<<"Entered data is \n";
s2.pre_fix(s2.root);
}
else if(choice==10)
{
//exit(0);
}
else
{
cout<<"\n\nInvalid Choice!!!Try again...\n";
}
}
while(choice!=10);
return 0;
}
#include<iostream>
#include<conio.h>>
using namespace std;
int flag=0;
struct Node
{
int data;
Node *left;
Node *right;
};
class BST
{
private:
int check1;
int check2;
public:
Node *root;
BST()
{
root=NULL;
check1=0;
check2=1;
}
void find_parent(int data,int &found,Node* &parent)
{
Node *ptr;
found=check1;
parent=NULL;
ptr=root;
while(ptr!=NULL)
{
if(ptr->data==data)
{
found=check2;
}
if(ptr->data>data)
{
parent=ptr;
ptr=ptr->left;
}
else
{
parent=ptr;
ptr=ptr->right;
}
}
}
void insert_values(int data)
{
int found;
Node *temp,*parent;
find_parent(data,found,parent);
if(found==check2)
{
cout<<"\nData with such value already exists\n";
}
else
{
temp=new Node;
temp->data=data;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
{
root=temp;
}
else if(parent->data>data)
{
parent->left=temp;
}
else
{
parent->right=temp;
}
}
}
void in_fix(Node *ptr)
{
if(ptr!=NULL)
{
in_fix(ptr->left);
cout<<ptr->data<<"\t";
in_fix(ptr->right);
}
}
void post_fix(Node *ptr,int flag)
{
flag++;
if(ptr!=NULL)
{
post_fix(ptr->left,flag);
post_fix(ptr->right,flag);
cout<<"\n\n\t\tData : Level\n";
cout<<"\n\t\t"<<ptr->data<<" : "<<flag<<endl;
}
}
void pre_fix(Node *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<"\t";
pre_fix(ptr->left);
pre_fix(ptr->right);
}
}
void traverse_BST()
{
int choice;
cout<<"\n1)In-fix\n2)Pre-fix\n3)Post_fix\n\n\t\tWhat is your chice???\n";
cin>>choice;
if(choice==1)
{
in_fix(root);
}
else if(choice==2)
{
pre_fix(root);
}
else
{
post_fix(root,flag);
}
}
bool search_node(int search,Node *ptr )
{
if (root==NULL)
{
return false;
}
else if (search<ptr->data)
{
return search_node(search,ptr->left);
}
else if (search>ptr->data)
{
return search_node(search,root->right );
}
else
{
return true;
}
}
int get_smallest(Node *ptr)
{
if (ptr->left==NULL)
{
return ptr->data;
}
else
{
return get_smallest(ptr->left);
}
}
int get_highest(Node *ptr)
{
if (ptr->right==NULL)
{
return ptr->data;
}
else
{
return get_highest(ptr->right);
}
}
void get_degree(Node *temp,int data)
{
int counter=0;
if(temp->data==data)
{
if(temp->left!=NULL)
{
counter++;
}
if(temp->right!=NULL)
{
counter++;
}
cout<<counter<<endl;
}
if(temp->left!=NULL)
{
get_degree(temp->left,data);
}
if(temp->right!=NULL)
{
get_degree(temp->right,data);
}
}
void recur_delete(int d,int &found,Node *&parent,Node *&x)
{
Node *ptr;
found=0;
if(root==NULL)
return;
ptr=root;
while(ptr!=NULL)
{
if(ptr->data==d)
{
found=1;
x=ptr;
return;
}
if(ptr->data>d)
{
parent=ptr;
ptr=ptr->left;
}
else
{
parent=ptr;
ptr=ptr->right;
}
}
}
void delete_node(int value)
{
Node *parent,*curr,*next;
int found;
if(root==NULL)
{
cout<<"\nBST is Empty!!!\n";
return;
}
parent=curr=NULL;
recur_delete(value,found,parent,curr);
if(found==0)
{
cout<<"\nNode with such value is not present!!!\n";
return;
}
if(curr->left!=NULL && curr->right!=NULL)
{
parent=curr;
next=curr->right;
while(next!=NULL)
{
parent=curr;
next=next->left;
}
curr->data=next->data;
curr=next;
}
if(curr->left==NULL && curr->right==NULL)
{
if(parent->right==curr)
{
parent->right=NULL;
}
else
{
parent->left=NULL;
}
delete curr;
return;
}
if(curr->left==NULL && curr->right!=NULL)
{
if(parent->left==curr)
{
parent->left=curr->right;
}
else
{
parent->right=curr->right;
}
delete curr;
return;
}
if(curr->left!=NULL && curr->right==NULL)
{
if(parent->left==curr)
{
parent->left=curr->left;
}
else
{
parent->right=curr->left;
}
delete curr;
return;
}
}
void show_min()
{
cout<<"\n\nMinum Value : "<<get_smallest(root);
}
void show_max()
{
cout<<"\n\nMaximum Value : "<<get_highest(root);
}
void search_result()
{
int search;
cout<<"\nEnter an Integer to Search : ";
cin>>search;
if(search_node(search,root)==true)
{
cout<<"\n"<<search<<" is present in BST!!!\n";
}
else
{
cout<<"\n"<<search<<" is not present in BST!!!\n";
}
}
void show_degree()
{
int data;
cout<<"Enter a value to find its degree :\n";
cin>>data;
cout<<"\nDegree of "<<data<<" is ";
get_degree(root,data);
}
BST(BST &s1)
{
root=new Node;
root->left=NULL;
root->right=NULL;
root->data=s1.root->data;
copy(root,s1.root);
}
void copy(Node *temp,Node *ptr)
{
if(ptr->left!=NULL)
{
temp->left=new Node;
temp->left->data=ptr->left->data;
temp->left->left=temp->left->right=NULL;
}
if(ptr->right!=NULL)
{
temp->right=new Node;
temp->right->data=ptr->right->data;
temp->right->left=temp->right->right=NULL;
}
if(ptr->left!=NULL)
{
copy(temp->left,ptr->left);
}
if(ptr->right!=NULL)
{
copy(temp->right,ptr->right);
}
}
};
int main()
{
BST s1;
int choice;
do
{
cout<<"\n\n\t\t\t-----Main Menu-----\n";
cout<<"\n1)Add New\n2)Traverse\n3)Get Degree\n4)Get Level \n5)Search a Node\n6)Get Minimum \n7)Get Maximum\n8)Delete\n9)Copy Constructor\n10)Quit\n";
cout<<"\n\t\t\tPlease select your choice : (1-10)\n";
cin>>choice;
if(choice==1)
{
int value;
char choice;
cout<<"\nEnter data :";
cin>>value;
s1.insert_values(value);
do
{
cout<<"\nDo you want more ??? (y/n)\n";
cin>>choice;
if(choice=='y')
{
cout<<"\nEnter data :";
cin>>value;
s1.insert_values(value);
}
}
while(choice!='n');
}
else if(choice==2)
{
s1.traverse_BST();
}
else if(choice==3)
{
s1.show_degree();
}
else if(choice==5)
{
s1.search_result();
}
else if(choice==6)
{
s1.show_min();
}
else if(choice==7)
{
s1.show_max();
}
else if(choice==8)
{
int value;
cout<<"\nEnter a value to delete : ";
cin>>value;
s1.delete_node(value);
s1.traverse_BST();
}
else if(choice==9)
{
BST s2=s1;
cout<<"Entered data is \n";
s2.pre_fix(s2.root);
}
else if(choice==10)
{
//exit(0);
}
else
{
cout<<"\n\nInvalid Choice!!!Try again...\n";
}
}
while(choice!=10);
return 0;
}
Linear Search
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int size;
int search_rollnumber;
int *arr;
bool flag=false;
cout<<"\nPlease enter total size of array : ";
cin>>size;
arr=new int [size];
for(int i=0;i<size;i++)
{
cout<<"\nPlease enter a roll number of student no. "<<i+1<<" : ";
cin>>arr[i];
}
cout<<"\nPlease enter a roll number to search : ";
cin>>search_rollnumber;
for(int i=0;i<size;i++)
{
if(arr[i]==search_rollnumber)
{
flag=true;
break;
}
else
{
flag=false;
}
}
if(flag==true)
{
cout<<"\n"<<search_rollnumber<<" is present in array...\n\n";
}
else
{
cout<<"\n"<<search_rollnumber<<" is not present in array...\n\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int size;
int search_rollnumber;
int *arr;
bool flag=false;
cout<<"\nPlease enter total size of array : ";
cin>>size;
arr=new int [size];
for(int i=0;i<size;i++)
{
cout<<"\nPlease enter a roll number of student no. "<<i+1<<" : ";
cin>>arr[i];
}
cout<<"\nPlease enter a roll number to search : ";
cin>>search_rollnumber;
for(int i=0;i<size;i++)
{
if(arr[i]==search_rollnumber)
{
flag=true;
break;
}
else
{
flag=false;
}
}
if(flag==true)
{
cout<<"\n"<<search_rollnumber<<" is present in array...\n\n";
}
else
{
cout<<"\n"<<search_rollnumber<<" is not present in array...\n\n";
}
return 0;
}
Tuesday, 26 August 2014
Polynomial Operations
#include "stdafx.h"
#include<iostream>
using namespace std;
struct poly
{
int ex,co;
poly *next;
};
void sum(poly *a,poly *b)
{
bool t=0,s=0,c=0;
cout<<" S U M :\n";
while(true)
{
if(a->ex==b->ex)
{
cout<<a->co+b->co<<" X^"<<a->ex;
a=a->next;
b=b->next;
}
else
{
if(a->ex>b->ex)
{
cout<<a->co<<" X^"<<a->ex;
a=a->next;
}
else
{
cout<<b->co<<" X"<<b->ex;
b=b->next;
}
}
if(c==1)
{
cout<<endl;
break;
}
if(a->next->next==NULL && b->next==NULL)
c=1;
cout<<" + ";
}
}
int main()
{
poly *first[2],*temp,*prev;
first[0]=new poly;
first[1]=new poly;
first[0]->next=first[1]->next=NULL;
for(int i=0;i<2;i++)
{
if(i==0)
cout<<"ENTER FIRST POLYNOMIAL OR PRESS 0 TO END\n";
if(i==1)
cout<<"ENTER SECOND POLYNOMIAL OR PRESS 0 TO END\n";
temp=first[i];
prev=temp;
do
{
cin>>temp->co;
system("cls");
if(temp->co==0)
{
break;
}
temp=first[i];
while(true)
{
// if(i==0)
// {
if(temp->co>0)
cout<<temp->co<<" X^";
else
cout<<"("<<temp->co<<") X^";
// }
/* if(i==1)
{
if(temp->co>0)
cout<<temp->co<<" Y^";
else
cout<<"("<<temp->co<<") Y^";
}*/
if(temp->next!=NULL)
cout<<temp->ex<<" + ";
else
break;
temp=temp->next;
}
cin>>temp->ex;
prev=temp;
temp->next=new poly;
temp=temp->next;
temp->next=NULL;
system("cls");
}
while(prev->co!=0);
}
for(int i=0;i<2;i++)
{
int x;
prev=temp=first[i];
while(temp->next->next!=NULL)
{
prev=temp;
while(true)//prev->next!=NULL)
{
if(temp->ex<prev->ex)
{
x=temp->ex;
temp->ex=prev->ex;
prev->ex=x;
x=temp->co;
temp->co=prev->co;
prev->co=x;
}
if(prev->next==NULL)
break;
prev=prev->next;
}
temp=temp->next;
}
}
cout<<"POLYNOMIALS :"<<endl<<endl;
for(int i=0;i<2;i++)
{
temp=first[i];
while(true)
{
//if(i==0)
// {
if(temp->co>0)
cout<<temp->co<<" X^ "<<temp->ex;
else
cout<<"("<<temp->co<<") X^ "<<temp->ex;
//}
/*else
{
if(temp->co>0)
cout<<temp->co<<" Y^ "<<temp->ex;
else
cout<<"("<<temp->co<<") Y^ "<<temp->ex;
}*/
if(temp->next->next!=NULL)
cout<<" + ";
else
break;
temp=temp->next;
}
cout<<endl<<endl;
}
sum(first[0],first[1]);
return 0;
}
#include<iostream>
using namespace std;
struct poly
{
int ex,co;
poly *next;
};
void sum(poly *a,poly *b)
{
bool t=0,s=0,c=0;
cout<<" S U M :\n";
while(true)
{
if(a->ex==b->ex)
{
cout<<a->co+b->co<<" X^"<<a->ex;
a=a->next;
b=b->next;
}
else
{
if(a->ex>b->ex)
{
cout<<a->co<<" X^"<<a->ex;
a=a->next;
}
else
{
cout<<b->co<<" X"<<b->ex;
b=b->next;
}
}
if(c==1)
{
cout<<endl;
break;
}
if(a->next->next==NULL && b->next==NULL)
c=1;
cout<<" + ";
}
}
int main()
{
poly *first[2],*temp,*prev;
first[0]=new poly;
first[1]=new poly;
first[0]->next=first[1]->next=NULL;
for(int i=0;i<2;i++)
{
if(i==0)
cout<<"ENTER FIRST POLYNOMIAL OR PRESS 0 TO END\n";
if(i==1)
cout<<"ENTER SECOND POLYNOMIAL OR PRESS 0 TO END\n";
temp=first[i];
prev=temp;
do
{
cin>>temp->co;
system("cls");
if(temp->co==0)
{
break;
}
temp=first[i];
while(true)
{
// if(i==0)
// {
if(temp->co>0)
cout<<temp->co<<" X^";
else
cout<<"("<<temp->co<<") X^";
// }
/* if(i==1)
{
if(temp->co>0)
cout<<temp->co<<" Y^";
else
cout<<"("<<temp->co<<") Y^";
}*/
if(temp->next!=NULL)
cout<<temp->ex<<" + ";
else
break;
temp=temp->next;
}
cin>>temp->ex;
prev=temp;
temp->next=new poly;
temp=temp->next;
temp->next=NULL;
system("cls");
}
while(prev->co!=0);
}
for(int i=0;i<2;i++)
{
int x;
prev=temp=first[i];
while(temp->next->next!=NULL)
{
prev=temp;
while(true)//prev->next!=NULL)
{
if(temp->ex<prev->ex)
{
x=temp->ex;
temp->ex=prev->ex;
prev->ex=x;
x=temp->co;
temp->co=prev->co;
prev->co=x;
}
if(prev->next==NULL)
break;
prev=prev->next;
}
temp=temp->next;
}
}
cout<<"POLYNOMIALS :"<<endl<<endl;
for(int i=0;i<2;i++)
{
temp=first[i];
while(true)
{
//if(i==0)
// {
if(temp->co>0)
cout<<temp->co<<" X^ "<<temp->ex;
else
cout<<"("<<temp->co<<") X^ "<<temp->ex;
//}
/*else
{
if(temp->co>0)
cout<<temp->co<<" Y^ "<<temp->ex;
else
cout<<"("<<temp->co<<") Y^ "<<temp->ex;
}*/
if(temp->next->next!=NULL)
cout<<" + ";
else
break;
temp=temp->next;
}
cout<<endl<<endl;
}
sum(first[0],first[1]);
return 0;
}
Adjacency Matrix
#include "stdafx.h"
#include<iostream>
using namespace std;
class HurryUp
{
private:
int array[5][5];
char choice;
public:
HurryUp()
{
array[0][0]=0;
}
void set()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<"Edge exist from "<<i<<" to "<<j<<"???('y'/'n')\n";
cin>>choice;
if(choice=='y')
{
array[i][j]=1;
}
else
{
array[i][j]=0;
}
}
}
}
void display()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<array[i][j]<<"\t";
}
}
}
void remove()
{
cout<<"\nDo you want to remove link???\n";
cin>>choice;
if(choice=='y')
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
array[i][j]=0;
}
}
}
}
};
int main()
{
HurryUp s1;
s1.set();
s1.display();
s1.remove();
s1.display();
return 0;
}
#include<iostream>
using namespace std;
class HurryUp
{
private:
int array[5][5];
char choice;
public:
HurryUp()
{
array[0][0]=0;
}
void set()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<"Edge exist from "<<i<<" to "<<j<<"???('y'/'n')\n";
cin>>choice;
if(choice=='y')
{
array[i][j]=1;
}
else
{
array[i][j]=0;
}
}
}
}
void display()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<array[i][j]<<"\t";
}
}
}
void remove()
{
cout<<"\nDo you want to remove link???\n";
cin>>choice;
if(choice=='y')
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
array[i][j]=0;
}
}
}
}
};
int main()
{
HurryUp s1;
s1.set();
s1.display();
s1.remove();
s1.display();
return 0;
}
Adjacency List
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class Graph
{
private:
char name;
Graph *next,*down;
Graph *root,*ptr;
public:
Graph()
{
root=NULL;
ptr=NULL;
}
void set()
{
Graph *ptr1,*ptr2,*ptr3;
int total_Graph;
char vertex;
cout<<"\nEnter No. of vertices in Graph : ";
cin>>total_Graph;
for(int i=0;i<total_Graph;i++)
{
cout<<"\nEnter Vertex Symbol: ";
cin>>vertex;
ptr1=new Graph;
ptr1->name=vertex;
ptr1->next=NULL;
ptr1->down=NULL;
if(root==NULL)
{
root = ptr1;
}
else
{
ptr2=root;
while(ptr2->next)
ptr2=ptr2->next;
ptr2->next=ptr1;
}
int edge;
cout<<"\nEnter edges connected with vertex "<<vertex<<" : ";
cin>>edge;
for(int j=0;j<edge; j++)
{
char n_edge;
cout<<"\nEnter symbol of edge "<<j+1<<" : ";
cin >>n_edge;
Graph *temp;
ptr=ptr1->down;
temp=new Graph;
temp->name=n_edge;
temp->down=NULL;
if(ptr==NULL)
{
ptr1->down=temp;
ptr=temp;
}
else
{
ptr3=ptr;
while(ptr3->down)
ptr3=ptr3->down;
ptr3->down=temp;
}
}
}
}
void Display()
{
Graph *ptr1,*ptr2;
ptr1=root;
ptr2=root;
cout<<"\n\n\t\tHere comes the output for you guys.......\n\n\n";
while(ptr1!=NULL)
{
cout<<ptr1->name<<"->";
ptr2=ptr1->down;
while(ptr2!=NULL)
{
cout<<ptr2->name<<"->";
ptr2=ptr2->down;
}
cout<<"NULL"<<endl;
ptr1=ptr1->next;
}
}
};
int main()
{
Graph s1;
s1.set();
s1.Display();
_getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
class Graph
{
private:
char name;
Graph *next,*down;
Graph *root,*ptr;
public:
Graph()
{
root=NULL;
ptr=NULL;
}
void set()
{
Graph *ptr1,*ptr2,*ptr3;
int total_Graph;
char vertex;
cout<<"\nEnter No. of vertices in Graph : ";
cin>>total_Graph;
for(int i=0;i<total_Graph;i++)
{
cout<<"\nEnter Vertex Symbol: ";
cin>>vertex;
ptr1=new Graph;
ptr1->name=vertex;
ptr1->next=NULL;
ptr1->down=NULL;
if(root==NULL)
{
root = ptr1;
}
else
{
ptr2=root;
while(ptr2->next)
ptr2=ptr2->next;
ptr2->next=ptr1;
}
int edge;
cout<<"\nEnter edges connected with vertex "<<vertex<<" : ";
cin>>edge;
for(int j=0;j<edge; j++)
{
char n_edge;
cout<<"\nEnter symbol of edge "<<j+1<<" : ";
cin >>n_edge;
Graph *temp;
ptr=ptr1->down;
temp=new Graph;
temp->name=n_edge;
temp->down=NULL;
if(ptr==NULL)
{
ptr1->down=temp;
ptr=temp;
}
else
{
ptr3=ptr;
while(ptr3->down)
ptr3=ptr3->down;
ptr3->down=temp;
}
}
}
}
void Display()
{
Graph *ptr1,*ptr2;
ptr1=root;
ptr2=root;
cout<<"\n\n\t\tHere comes the output for you guys.......\n\n\n";
while(ptr1!=NULL)
{
cout<<ptr1->name<<"->";
ptr2=ptr1->down;
while(ptr2!=NULL)
{
cout<<ptr2->name<<"->";
ptr2=ptr2->down;
}
cout<<"NULL"<<endl;
ptr1=ptr1->next;
}
}
};
int main()
{
Graph s1;
s1.set();
s1.Display();
_getch();
return 0;
}
Bubble sort using Linked-List
#include "stdafx.h"
#include<iostream>
using namespace std;
struct Link
{
int data;
Link *next;
};
class List
{
private:
Link *first;
public:
List()
{
first=NULL;
}
void set_values(int v)
{
Link *ptr,*temp;
if(first==NULL)
{
first=new Link;
first->data=v;
first->next=NULL;
}
else
{
ptr=first;
while(ptr->next!=NULL)
ptr=ptr->next;
temp=new Link;
temp->data=v;
temp->next=NULL;
ptr->next=temp;
}
}
void get_values()
{
Link *temp;
temp=first;
cout<<"Inserted Integers are..."<<endl;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<"\n\n";
}
void sort_values()
{
Link *temp1,*temp2;
temp1=first;
int a;
while(temp1->next!=NULL)
{
temp2=first;
while(temp2->next!=NULL)
{
if(temp2->data>temp2->next->data)
{
a=temp2->data;
temp2->data=temp2->next->data;
temp2->next->data=a;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
cout<<"\n\n";
temp1=first;
while(temp1!=NULL)
{
cout<<temp1->data<<"\t";
temp1=temp1->next;
}
}
};
int main()
{
List s1;
Link *temp,*first;
first=NULL;
temp=first;
int value;
char choice;
int count=0;
do
{
cout<<"Do you want to add data in the List,press 'y' for yes & press 'n' for no."<<endl;
cin>>choice;
if(choice=='y')
{
cout<<"Enter an Integer:"<<endl;
cin>>value;
s1.set_values(value);
}
else if(choice=='n')
{
cout<<"\nCongratulations!!!Data has been entered successfully!!!"<<endl;
cout<<"Thank you v.much!!!"<<endl;
}
else
{
cout<<"Invalid choice,,,Please try again!!!"<<endl;
}
count++;
}while(choice!='n');
cout<<"\n\nDo you wish to see the stored data?press 'w' for this query:"<<endl;
cin>>choice;
if(choice=='w')
{
count=count-1;
s1.get_values();
cout<<"Array has "<<count<<" elements in it"<<endl;
s1.sort_values();
}
else
{
cout<<"\n\nThank you very much once again!!!"<<endl;
exit(0);
}
return 0;
}
#include<iostream>
using namespace std;
struct Link
{
int data;
Link *next;
};
class List
{
private:
Link *first;
public:
List()
{
first=NULL;
}
void set_values(int v)
{
Link *ptr,*temp;
if(first==NULL)
{
first=new Link;
first->data=v;
first->next=NULL;
}
else
{
ptr=first;
while(ptr->next!=NULL)
ptr=ptr->next;
temp=new Link;
temp->data=v;
temp->next=NULL;
ptr->next=temp;
}
}
void get_values()
{
Link *temp;
temp=first;
cout<<"Inserted Integers are..."<<endl;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<"\n\n";
}
void sort_values()
{
Link *temp1,*temp2;
temp1=first;
int a;
while(temp1->next!=NULL)
{
temp2=first;
while(temp2->next!=NULL)
{
if(temp2->data>temp2->next->data)
{
a=temp2->data;
temp2->data=temp2->next->data;
temp2->next->data=a;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
cout<<"\n\n";
temp1=first;
while(temp1!=NULL)
{
cout<<temp1->data<<"\t";
temp1=temp1->next;
}
}
};
int main()
{
List s1;
Link *temp,*first;
first=NULL;
temp=first;
int value;
char choice;
int count=0;
do
{
cout<<"Do you want to add data in the List,press 'y' for yes & press 'n' for no."<<endl;
cin>>choice;
if(choice=='y')
{
cout<<"Enter an Integer:"<<endl;
cin>>value;
s1.set_values(value);
}
else if(choice=='n')
{
cout<<"\nCongratulations!!!Data has been entered successfully!!!"<<endl;
cout<<"Thank you v.much!!!"<<endl;
}
else
{
cout<<"Invalid choice,,,Please try again!!!"<<endl;
}
count++;
}while(choice!='n');
cout<<"\n\nDo you wish to see the stored data?press 'w' for this query:"<<endl;
cin>>choice;
if(choice=='w')
{
count=count-1;
s1.get_values();
cout<<"Array has "<<count<<" elements in it"<<endl;
s1.sort_values();
}
else
{
cout<<"\n\nThank you very much once again!!!"<<endl;
exit(0);
}
return 0;
}
Merge Sort
#include <iostream>
#include <string>
void DoMerge(int numbers[], int left, int mid, int right)
{
int temp[25];
int i, left_end, num_elements, tmp_pos;
left_end = (mid - 1);
tmp_pos = left;
num_elements = (right - left + 1);
while ((left <= left_end) && (mid <= right))
{
if (numbers[left] <= numbers[mid])
temp[tmp_pos++] = numbers[left++];
else
temp[tmp_pos++] = numbers[mid++];
}
while (left <= left_end)
temp[tmp_pos++] = numbers[left++];
while (mid <= right)
temp[tmp_pos++] = numbers[mid++];
for (i=0; i < num_elements; i++)
numbers[right--] = temp[right];
}
void MergeSort(int numbers[], int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
MergeSort(numbers, left, mid);
MergeSort(numbers, (mid+1), right);
DoMerge(numbers, left, (mid+1), right);
}
}
void MergeSortHelper(int numbers[], int array_size)
{
MergeSort(numbers, 0, array_size - 1);
}
void max()
{
int max;
std::cout << "\nProgram for Ascending order of Numeric Values using MERGE SORT";
std::cout << "\n\nEnter the total number of elements: ";
std::cin >> max;
int *numarray = new int[max];
for(int i = 0; i < max; i++)
{
std::cout << "\nEnter [" << i + 1 << "] element: ";
std::cin >> numarray[i];
}
std::cout << "Before Sorting : ";
for(int k = 0; k < max; k++)
std::cout << numarray[k] << " ";
std::cout << "\n";
MergeSortHelper(numarray,max);
std::cout << "\n\nThe numbers in ascending orders are given below:\n\n";
for(int i = 0; i < max; i++)
{
std::cout << "Sorted [" << i + 1 << "] element: ";
std::cout << numarray[i];
std::cout << "\n";
}
delete [] numarray;
}
Quick Sort
#include <iostream>
#include <string>
int Partition(int a[], int left, int right)
{
int pivot = a[left];
while (true)
{
while (a[left] < pivot)
left++;
while (a[right] > pivot)
right--;
if (left < right)
{
int temp = a[right];
a[right] = a[left];
a[left] = temp;
}
else
{
return right;
}
}
}
void QuickSort(int *arr, int left, int right)
{
if(left < right)
{
int pivot = Partition(arr, left, right);
//for(int i = 0; i < 9; i++)
// std::cout << arr[i] << " ";
// std::cout << "\n";
if(pivot > 1)
QuickSort(arr, left, pivot - 1);
if(pivot + 1 < right)
QuickSort(arr, pivot + 1, right);
}
}
void QuickSort(int *arr, int len)
{
QuickSort(arr, 0, len - 1);
}
void main()
{
int max;
std::cout << "\nProgram for Ascending order of Numeric Values using QUICK SORT";
std::cout << "\n\nEnter the total number of elements: ";
std::cin >> max;
int *numarray = new int[max];
for(int i = 0; i < max; i++)
{
std::cout << "\nEnter [" << i + 1 << "] element: ";
std::cin >> numarray[i];
}
std::cout << "Before Sorting : ";
for(int k = 0; k < max; k++)
std::cout << numarray[k] << " ";
std::cout << "\n";
QuickSort(numarray,max);
std::cout << "\n\nThe numbers in ascending orders are given below:\n\n";
for(int i = 0; i < max; i++)
{
std::cout << "Sorted [" << i + 1 << "] element: ";
std::cout << numarray[i];
std::cout << "\n";
}
delete [] numarray;
}
Monday, 25 August 2014
Circular Linked List
#include "stdafx.h"
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *link;
Node()
{
data=0;
link=NULL;
}
};
class Link_List
{
private:
Node *first;
Node *last;
public:
Link_List()
{
first=NULL;
last=NULL;
}
void insert(int value)
{
if(first==NULL)
{
first=new Node;
first->data=value;
first->link=NULL;
last=first;
//return;
}
Node *ptr=new Node;
ptr->data=value;
ptr->link=first;
first=ptr;
last->link=first;
}
void insert_last(int value)
{
if(first==NULL)
{
first=new Node;
first->data=value;
first->link=first;
last=first;
// return;
}
Node *ptr=new Node;
last->link=ptr;
last=ptr;
last->data=value;
}
void remove()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
if(first->link==first)
{
delete first;
last=NULL;
//return;
}
Node *ptr=first;
first=first->link;
last->link=first;
delete ptr;
}
void remove_last()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
if(first->link==first)
{
delete first;
last=NULL;
//return;
}
Node *ptr=first;
while(ptr->link!=last)
ptr=ptr->link;
ptr->link=first;
delete last;
last=ptr;
}
void remove_at(int index)
{
Node *temp1=first;
int i=1;
while(i<index-1)
{
i++;
if(temp1==NULL)
{
cout<<"\nLink not found!!!"<<endl;
}
else
{
temp1=temp1->link;
}
}
Node *temp2=temp1->link;
temp1->link=temp1->link->link;
delete temp2;
}
void display()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
Node *ptr=first;
while(ptr->link!=first)
{
cout<<ptr->data<<"\t";
ptr=ptr->link;
}
cout<<ptr->data<<"\t";
}
~Link_List()
{
Node *ptr=first;
while(first->link!=last)
{
first=first->link;
delete ptr;
ptr=first;
}
delete ptr;
ptr=first->link;
}
};
int main()
{
Link_List s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s1.insert(5);
s1.display();
s1.remove_at(2);
s1.display();
s1.remove();
cout<<"\n";
s1.display();
s1.remove();
cout<<"\n";
s1.display();
s1.remove_last();
cout<<"\n";
s1.display();
s1.remove_last();
cout<<"\n";
s1.display();
//s1.remove_last();
// s1.remove_last();
return 0;
}
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *link;
Node()
{
data=0;
link=NULL;
}
};
class Link_List
{
private:
Node *first;
Node *last;
public:
Link_List()
{
first=NULL;
last=NULL;
}
void insert(int value)
{
if(first==NULL)
{
first=new Node;
first->data=value;
first->link=NULL;
last=first;
//return;
}
Node *ptr=new Node;
ptr->data=value;
ptr->link=first;
first=ptr;
last->link=first;
}
void insert_last(int value)
{
if(first==NULL)
{
first=new Node;
first->data=value;
first->link=first;
last=first;
// return;
}
Node *ptr=new Node;
last->link=ptr;
last=ptr;
last->data=value;
}
void remove()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
if(first->link==first)
{
delete first;
last=NULL;
//return;
}
Node *ptr=first;
first=first->link;
last->link=first;
delete ptr;
}
void remove_last()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
if(first->link==first)
{
delete first;
last=NULL;
//return;
}
Node *ptr=first;
while(ptr->link!=last)
ptr=ptr->link;
ptr->link=first;
delete last;
last=ptr;
}
void remove_at(int index)
{
Node *temp1=first;
int i=1;
while(i<index-1)
{
i++;
if(temp1==NULL)
{
cout<<"\nLink not found!!!"<<endl;
}
else
{
temp1=temp1->link;
}
}
Node *temp2=temp1->link;
temp1->link=temp1->link->link;
delete temp2;
}
void display()
{
if(first==NULL)
{
cout<<"\nNo value to Display!!!\n";
//return;
}
Node *ptr=first;
while(ptr->link!=first)
{
cout<<ptr->data<<"\t";
ptr=ptr->link;
}
cout<<ptr->data<<"\t";
}
~Link_List()
{
Node *ptr=first;
while(first->link!=last)
{
first=first->link;
delete ptr;
ptr=first;
}
delete ptr;
ptr=first->link;
}
};
int main()
{
Link_List s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s1.insert(5);
s1.display();
s1.remove_at(2);
s1.display();
s1.remove();
cout<<"\n";
s1.display();
s1.remove();
cout<<"\n";
s1.display();
s1.remove_last();
cout<<"\n";
s1.display();
s1.remove_last();
cout<<"\n";
s1.display();
//s1.remove_last();
// s1.remove_last();
return 0;
}
Subscribe to:
Posts (Atom)