Friday, February 27, 2015

Swap Kth node from beginning with Kth node from end in a Linked List

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};

void push(struct node ** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)  = new_node;
}



void printList(struct node *head)
{
    struct node *temp = head;
    while (temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}



void Fun(struct node **Node,int x,int y){
struct node *p=*Node;
struct node *q=*Node;
while(p->next->data!=x)
{
p=p->next;
}
while(q->next->data!=y)
{
q=q->next;
}
struct node *m=p->next;
struct node *r=p->next->next;
struct node *s=q->next->next;
p->next=q->next;
p->next->next=r;
q->next=m;
q->next->next=s;
 }



int main()
{
    int i;
     struct node *p = NULL;
     for (i = 8; i >= 1; i--){
       push(&p, i);
     }
     printf("Before Swapping 3 and 6   :");
     printList(p);
     Fun(&p,3,6);
     printf("\nAfter Swapping 3 and 6  :");
    printList(p);
     getchar();
     return 0;
}

Friday, February 20, 2015

Check if Two Nodes belongs to Same Parent or not

#include <stdio.h>
#include <stdlib.h>
 #include<stdbool.h>
struct Node
{
    int data;
    struct Node *left, *right;
};

// A utility function to create a new Binary Tree Node
struct Node *newNode(int item)
{
    struct Node *temp =  (struct Node *)malloc(sizeof(struct Node));
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;
}
bool isFUN(struct Node*root,struct Node *a,struct Node *b)
{
    if(root==NULL)
    {
        return 0;
    }
    if(((root->left==a)&&(root->right==b))||((root->left==b)&&(root->right==a)))
    {
        return true;
    }
    else
    {
        return isFUN(root->left,a,b)+isFUN(root->right,a,b);
    }
}

   

int main()
{
    struct Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->left->right->right = newNode(15);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->right->left->right = newNode(8);

    struct Node *Node1,*Node2;
    Node1 = root->left->left;
    Node2 = root->left->right;
    isFUN(root,Node1,Node2)? puts("YES"): puts("NO");
    return 0;
}

Tuesday, February 17, 2015

Print nodes at k distance from root

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node* left;
    struct node* right;
};

struct node* newNode(int data)
{
    struct node* node = (struct node*)
                             malloc(sizeof(struct node));
    node->data  = data;
    node->left  = NULL;
    node->right = NULL;

    return(node);
}
printDistance(struct node *root,int x,int k)
{
if(root==NULL)
{
return;
}
if(k==x)
{
printf("%10d  ",root->data);
}
else
{
printDistance(root->left,x+1,k);
printDistance(root->right,x+1,k);
}
}
int main(void)
{
     struct node *root = newNode(1);
  root->left        = newNode(2);
  root->right       = newNode(3);
  root->left->left  = newNode(4);
  root->left->right = newNode(5);
  root->right->left = newNode(8);
  printDistance(root,0,2);
return 0;
}

Monday, February 16, 2015

Check if a Tree is Full Binary Tree or Not

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct Node
{
    int key;
    struct Node *left, *right;
};
struct Node *newNode(char k)
{
    struct Node *node = (struct Node*)malloc(sizeof(struct Node));
    node->key = k;
    node->right = node->left = NULL;
    return node;
}

bool isFullTree(struct Node *root)
{
    if(root==NULL)
    {
        return true;
    }
         else if((root->left==NULL)&&(root->right!=NULL))
        {
            return false;
        }
        else if((root->left!=NULL)&&(root->right==NULL))
        {
            return false;
        }
        else if((root->left==NULL)&&(root->right==NULL))
        {
            return true;
        }
else
        {
            return (isFullTree(root->left)&&isFullTree(root->right));
        }
}

int main()
{
    struct Node* root = NULL;
    root = newNode(10);
    root->left = newNode(20);
    root->right = newNode(30);
 root->left->right = newNode(40);
    root->left->left = newNode(50);
    root->right->left = newNode(60);
    root->right->right = newNode(70);
    root->left->left->left = newNode(80);
    root->left->left->right = newNode(90);
    root->left->right->left = newNode(80);
    root->left->right->right = newNode(90);
    root->right->left->left = newNode(80);
    root->right->left->right = newNode(90);
    root->right->right->left = newNode(80);
    root->right->right->right = newNode(90);
    if (isFullTree(root))
        printf("The Binary Tree is full\n");
    else
        printf("The Binary Tree is not full\n");

    return(0);
}

Tuesday, December 23, 2014

Print all possible combinations of r elements in a given array of size n

#include<stdio.h>
 int data[30];
void fun(int arr[],int i,int N,int r,int index)
{
int j;
if(r==index)
{
for(i=0;i<r;i++)
{
printf("%d",data[i]);
}
printf("\n");
return;
}
for(j=i;j<N;j++)
{
data[index]=arr[j];
fun(arr,j+1,N,r,index+1);
}
}

int main()
{
 int arr[5]={1,2,3,4,5};
 int r=3;
 fun(arr,0,5,r,0);
 return 0;
}

Sunday, December 21, 2014

C++ program to print all distinct elements in a given array


#include <iostream>
#include <algorithm>
using namespace std;

void printDistinct(int arr[], int n)
{
    // First sort the array so that all occurrences become consecutive
    sort(arr, arr + n);
    int i=0;

    // Traverse the sorted array
    while(i<n)
    {
    if(arr[i]==arr[i+1])
    {
    i=i+1;
    }
    else
    {
    cout<<arr[i]<<endl;
    i++;
    }
    }
}

// Driver program to test above function
int main()
{
    int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    printDistinct(arr, n);
    return 0;
}

Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!


#include<stdio.h>
int main()
{
int fact[10];
int i,n;
float sum=0;
fact[1]=fact[0]=1;
printf("Enter value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact[i]=fact[i-1]*i;
sum+=(float)1/fact[i];
}
printf("%f",sum);
return 0;
}