Monday, October 12, 2015

write a prgram for to find x^n(x to power of n) using bitwise operators only

int rev(unsigned int byte)
{
unsigned int temp1=0;
unsigned int temp2=0;
unsigned int count=0;
int i;
for(i=0;i<=7;i++)
{
temp1=byte&127;
if(temp1)
{
temp2=temp1|cnt;
}
count=count<<1;
byte=byte<<1;
}
void main()
{
printf("reversse its=%d\n",rev(130));
}

Find Kth smallest element in Binary search tree

/*
TXT file
20 8 22 4 12 10 14 -1
input.txt
*/


#include <iostream>
#include <fstream>
using namespace std;
struct node
{
int data;//,height;
node * left;
node *right;
};
void print(node *t)
{
if(t!=NULL)
{
print(t->left);
print(t->right);
cout<<t->data<<" ";
}

}

int height(node *t)
{
if(t==NULL)
return -1;
else{
if(t->left==NULL&&t->right==NULL)
return 0;
else
{
int a=height(t->left);
int b=height(t->right);
if(a>b)
return a+1;
else
return b+1;
}
}
}
int max(int a ,int b)
{
if(a>b)
return a;
else return b;
}

void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->left=NULL;
t->right=NULL;
}
else if(a<t->data)
{
create(t->left,a);
}
else {
create(t->right,a);
}
}
void K_small(node *t,int a,int &c)
{
if(t==NULL||c>=a)
return;
K_small(t->left,a,c);
c++;
if(a==c){

cout<<t->data<<" is kth K_small element\n";
return ;
}
K_small(t->right,a,c);
}

int main()
{
ifstream fin;
int k;
fin.open("/home/pawan/Desktop/ds/input.txt");
if(!fin.is_open())

cout<<"file not opened\n";
node *t;
t=NULL;
int a;
fin>>a;
while(a!=-1)
{
create(t,a);
fin>>a;
}

fin.close();
print(t);
cout<<endl;
int c=0;
K_small(t,3,c);
return 0;
}

Sunday, October 11, 2015

Find Minimum Height in binary tree

#include <iostream>
#include <fstream>
using namespace std;
struct node
{
int data;//,height;
node * left;
node *right;
};
void print(node *t)
{
if(t!=NULL)
{
print(t->left);
print(t->right);
cout<<t->data<<" ";
}

}

int height(node *t)
{
if(t==NULL)
return -1;
else{
if(t->left==NULL&&t->right==NULL)
return 0;
else
{
int a=height(t->left);
int b=height(t->right);
if(a>b)
return a+1;
else
return b+1;
}
}
}
int max(int a ,int b)
{
if(a>b)
return a;
else return b;
}

void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->left=NULL;
t->right=NULL;
}
else if(a<t->data)
{
create(t->left,a);
}
else {
create(t->right,a);
}
}
int min_h(node *t)
{
static int h=height(t);
if(t==NULL)
return 0;
if(t->left==NULL&&t->right==NULL)
return 0;
if(t->left==NULL)
return 1+min_h(t->right);
else if(t->right==NULL)
return 1+min_h(t->left);
int a=min_h(t->left);
int b=min_h(t->right);
if(a>b)
return b+1;
else
return a+1;

}
int main()
{
ifstream fin;
int k;
fin.open("/home/pawan/Desktop/ds/input.txt");
if(!fin.is_open())

cout<<"file not opened\n";
node *t;
t=NULL;
int a;
fin>>a;
while(a!=-1)
{
create(t,a);
fin>>a;
}

fin.close();
print(t);
int min=min_h(t);
cout<<"minimun height :";
cout<<min;
return 0;
}

Find longest prefix among strings

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string longestCommonPrefix(vector<string> &A) {
    int in=A[0].length()-1;
   // cout<<in;
    for(int i=0;i<A.size()-1;i++)
    {
        cout<<in<<endl;
        while(A[i][in]!=A[i+1][in]) {
            cout<<"equal";
          in--;
          }
    }
    string str;
    str=A[0].substr(0,in+1);
    return str;
    }
int main()
{
    vector <string> v;
    v.push_back("hello");
    v.push_back("helpp");
    v.push_back("heloo");
    string str=longestCommonPrefix(v);
    cout<<str;
}

Find no. of Trailing zeroes in factorial of given number

trailing zeroes depends on no. of 5's and 2's .

total=no. of 5's.
#include <iostream>
using namespace std;
int main()
{
int a=trailingZeroes(12);
cout<<a;
}
int trailingZeroes(int A) {
    int count=0;
    while(A>=5)
    {
        count=count+A/5;
        A=A/5;
    }
    return count;
}

Thursday, October 8, 2015

Merge a linked list into another linked list at alternate positions

#include <iostream>
using namespace std;
struct node
{
int  data;
node *next;
};

void print(node *t)
{
if(t!=NULL)
{

cout<<t->data<<" ";
print(t->next);

}

}

node * create(int  a)
{
node *t=new node;
t->data=a;
t->next=NULL;
return t;
}
node * altswap(node *t,node *l)
{
if(t==NULL )return l;
if(l==NULL) return t;
node *x,*y;
x=t;
while(t&&l){
y=t->next;
t->next=l;
l=y;
t=t->next;
}

return x;
}
int main()
{
node *t=NULL;
t=create(4);
t->next=create(2);
t->next->next=create(11);
t->next->next->next=create(43);
t->next->next->next->next=create(13);
t->next->next->next->next->next=create(45);
node *l=NULL;
l=create(1);
l->next=create(3);
l->next->next=create(12);
l->next->next->next=create(63);
l->next->next->next->next=create(153);
l->next->next->next->next->next=create(145);

print(t);
cout<<endl;
print(l);
cout<<endl;
t=altswap(t,l);
print(t);
return 0;
}

Swap nodes in a linked list without swapping data

#include <iostream>
using namespace std;
struct node
{
int  data;
node *next;
};

void print(node *t)
{
if(t!=NULL)
{

cout<<t->data<<" ";
print(t->next);

}

}

node * create(int  a)
{
node *t=new node;
t->data=a;
t->next=NULL;
return t;
}
node * swap(node *t,int a,int b)
{
if(t==NULL ||t->next==NULL)
return t;
node *x,*y;
x=t;
while(x->data!=a&&x)
x=x->next;
y=t;
while(y->data!=b)
y=y->next;

node *m=y->next;
node *t1=t;
while(t1->next!=x&&t1->next)
t1=t1->next;
t1->next=y;
y->next=x->next;
node *y1=y;
while(y->next!=y1)
y=y->next;
y->next=x;
x->next=m;
return t;
}
int main()
{
node *t=NULL;
t=create(4);
t->next=create(2);
t->next->next=create(11);
t->next->next->next=create(43);
t->next->next->next->next=create(13);
t->next->next->next->next->next=create(45);
print(t);
cout<<endl;
t=swap(t,2,13);
print(t);
return 0;
}

Contributors

Translate