Wednesday 26 October 2011

implementation of a program to count the no. of leaves in a tree

//program is implemented by shadab khan
#include <stdio.h>
#include <stdlib.h>

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};

/* Function to get the count of leaf nodes in a binary tree*/
unsigned int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right==NULL)
return 1;
else
return getLeafCount(node->left)+
getLeafCount(node->right);
}

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
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);
}

/*Driver program to test above functions*/
int main()
{
/*create a tree*/
struct node *root = newNode(1);
root->left        = newNode(2);
root->right       = newNode(3);
root->left->left  = newNode(4);
root->left->right = newNode(5);

/*get leaf count of the above created tree*/
printf("Leaf count of the tree is %d", getLeafCount(root));

getchar();
return 0;
}

implementation of stack

//program is implemented by shadab khan
#include<conio.h>
#include<iostream.h>
#include<process.h>

void main()
{
            int stack[150],top,n,i,max_stack,choice,element,n_pop;
            max_stack=150;
            cout<<"How many elements are in the stack: ";
            cin>>n;
            cout<<"Enter "<<" elements.\n";
            for(top=0;top>stack[top];top++)
            {
                        cin>>stack[top];
            }
            cout<<"\nStack is implemented.\nThe stack is\n";
            for(i=top-1;i>=0;i--)
                        cout<<"   ";
            getch();
            while(1)
            {
                        clrscr();
                        cout<<"1: PUSH\n2: POP\n3: Display Stack\n4: Exit\nEnter your choice: ";
                        cin>>choice;
                        switch(choice)
                        {
                                    case 1:     //  PUSH
                                                cout<<"\nPUSH OPERATION\n";
                                                if(top>=max_stack)
                {
                                                            cout<<"Stack is full!";
                                                            getch();
                                                            break;
                                                }
                                                cout<<"Enter an element: ";
                                                cin>>element;
                                                stack[top]=element;
                top++;
                                                cout<<"Item Inserted!";
                                                getch();
                                                break;
                                    case 2:             // POP
                                                cout<<"POP OPERATION\n";
                                                if(top<0)
                                                {
                                                            cout<<"Stack is empty";
                                                            getch();
                                                            break;
                                                }
                                                cout<<"How many elements you want to pop: ";
                cin>>n_pop;
                                                if(n_pop>top)
                                                {
                                                            cout<<"\nError!\nStack is small.";
                                                            getch();
                   break;
                                                }
                                                top=top-n_pop;
                                                cout<<"Items POPed";
                                                getch();
                                                break;
                                    case 3:       //  Display
                                                cout<<"\The stack is\n";
                                                for(i=top-1;i>=0;i--)
                                                            cout<< stack[i] <<"   ";
                                                getch();
                                                break;
                                    case 4:
                                                exit(0);
                                                getch();
                                    default:
                                                cout<<"RE-enter your choice!";
                        }          // End of switch
            }                      // End of while
            getch();
}

implementation of merging of two array or list

/*
ALGORITHM:

1. Initialized low1=0 of A array and low2=0 of B array.
2. Repeat step 2 until low1<n && low2<n.
            a. copy element of A array into C array if element of A array is smaller than B.
            b. Other wise copy element of B array into C array.
3. Repeat if any element of A array is still left to be insert into C array i.e. low1<n
4. Repeat if any element of B array is still left to be insert into C array i.e. low2<n
*/
//program is implemented by shadab khan
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int a[5],b[5],c[10],i,j,k;
            cout<<"enter the 5 element of Ist array : \n\n";
            for(i=0;i<=4;i++)
            cin>>a[i];
            cout<<"enter the 5 element of IInd array : \n\n";
            for(i=0;i<=4;i++)
            cin>>b[i];
            i=0;j=0;k=0;
            while(i<5 && j<5)
            {
                        if(a[i]<b[j])
                        {
                                    c[k]=a[i];
                                    i++;
                                    k++;
                        }
                        else
                        {
                                    c[k]=b[j];
                                    j++;
                                    k++;
                        }
            }
            while(i<5)
            {
                        c[k]=a[i];
                        i++;
                        k++;
            }
            while(j<5)
            {
                        c[k]=b[j];
                        j++;
                        k++;
            }
            cout<<"Merged array : \n\n";
            for(i=0;i<=9;i++)
            cout<<c[i]<<endl;
            getch();
            }

implementation of insertion sort

//program is implemented by shadab khan
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int a[5], i,t,p;
            cout<<”enter the 4 element of array”;
            for(i=1;i<=4;i++)
            cin>>a[0];
            a[0]=NULL;
            for(i=2;i<=4;i++)
            {
                        t=a[i];
                        p=i-1;
                        while(t<a[p])
                        {
                        a[p+1]=a[p];
                        P=p-1;
                        }
            a[p+1]=t;
            }
            cout<<”Elements after insertion sort : \n\n”;
            for(i=0;i<=4;i++)
            {
                        cout<<a[i]<<endl;
            }
            getch();
}

implementation of selection sort algorithm

//program is implemented by shadab khan
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int a[5], i,j,t,p,s;
            cout<<”enter the 5 element of array”;
            for(i=0;i<=4;i++)
            cin>>a[0];
            for(i=0;i<=4;i++)
            {
                        s=a[i];
                        p=I;
                        for(j=i=1;j<=4;j++)
                        {
                                    if(s>a[j])
                                    {
                                                s=a[j];
                                                p=j;
                                    }
                        }
                        t=a[i];
                        a[i]=a[p];
                        a[p]=t;
            }
            cout<<”5 sorted elements of array”;
            for(i=0;i<=4;i++)
            cout<<a[i];
            getch();
}

implementation of bubble sort

//program is implemented by shadab khan
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int a[5], i,j,t;
            cout<<”enter the 5 element of array”;
            for(i=0;i<=4;i++)
            cin>>a[0];
            for(i=0;i<=4;i++)
            {
                        for(j=0;j<=4-I;j++)
            {
                        if(a[j]>a[j+1])
                        {
                                    t=a[j];
                                    a[j]=a[j+1];
                                    a[j+1]=t;
                        }
            }
            }
            cout<<”5 sorted elements of array”;
            for(i=0;i<=4;i++)
            cout<<a[i];
            getch();
}

implementation of quick sort algorithm

/*
ALGORITHM:

1.  low =l, high = h, key a[(l+h)/2]
2.  Repeat through step 7 while (low <= high)
3.  Repeat step 4 while (a[low] < key)
4.  low = low +1
5.  Repeat step 6 while (a[high] > key)
6.  high = high – 1
7.  If (low <= high)
a)  temp = a[low]
b)  a[low] = a[high]
c)  a[high] = temp
d)  low = low + 1
e)  high = high + 1
8.  If (l < high) quicksort (a,l,high)
9.  If (h>low) quicksort (a,low,h)
10.  Exit
*/
//program is implemented by shadab khan
#include <stdio.h>
#include <conio.h>
#define max 100
Int a[max],n,i,l,h;

void main()
{
void input(void);
input();
getch();
}

void input(void);
{
void output( int a[], int n);
void quick_sort(int a[], int l, int h);
printf("How many elements in the array : ");
scanf("%d",&n);
printf("\n");
printf("Enter the elemennts : \n");
for(i=0;i<=n-1;i++)
{
            scanf("%d",&a[i]);
}
l=0;
h=n-1;
quick_sort(a,l,h);
printf("Sorted Array :\n ");
output(a,n);
}
void quick_sort(int a[],int l, int h)
{
            Int temp,key,low,high;
low=l;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
while(low<=high)
{
if (l<high)
quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
}
void output(int a[], int n)
{
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}

implementation of transpose of matrix

/*
ALGORITHM:

Transpose(a,m,n)
1 for(i= 1 to m)
            for(j= 1 to n)
            b[i][j]= a[j][i]
2 for (i=1to m)
            for (j= 1to n)
            a[i][j]= b[i][j]
3 exit.
*/
//program implemented by shadab khan
#include <stdio.h>
#include <conio.h>
Void main()
{
int a[10][10],b[10][10],i,j,m,n;
clrscr();
printf("Enter the order of the matrix\n");
printf("No. of rows : \n");
scanf("%d",&n);
printf("No. of columns :\n ");
scanf("%d",&m);
printf("Enter the matrix elements\n");
for(i=0;i<=n-1;i++)
{
            for(j=0;j<=m-1;j++)
            {
                        scanf("%d\n",&a[i][j]);
                        b[j][i] = a[i][j];
            }
}
printf("Matrix A was\n ");
for(i=0;i<=n-1;i++)
{
            for(j=0;j<=m-1;j++)
            {
                        printf("%d\n",a[i][j]);
            }
}
printf("Transpose of matrix A is \n");
for(i=0;i<=m-1;i++)
{
            for(j=0;j<=n-1;j++)
            {
            printf("%d\n",b[i][j]);
            }
}
getch( );
}

implementation of addition of two matrix

/*
ALGORITHM:

Matadd(a,b,m,n)
1 for (i=1 to m
2 for(j= 1 to n)
3c[i][j] = a[i][j]+b[i][j]
4 exit
*/
//program implemented by shadab khan
#include <stdio.h>
#include <conio.h>
Void main()
{
Int a[2][2],b[2][2],s[2][2],i,j;
clrscr ();
printf("enter first matrix: \n");
for( i=1; i<=2; i++)
{
            for( j=1; j<=2; j++)
            {
            printf("Enter %d%d", i,j, "element:");
            scanf("%d",&a[i][j]);
            }
}
printf("enter second matrix: \n");
for(i=1;i<=2;i++)
{
            for(j=1; j<=2;j++)
            {
            printf( "enter  %d%d",i + 1 ,j + 1 , "element:");
            scanf("%d",&b[i][j])  ;
            }
}
for(i=1;i<=2;i++)
{
            for(j=1;j<=2;j++)
            {
            s[i][j]= a[i][j]+b[i][j];
            }
}
printf("The addition matrix is:\n");
for(i=1;i<=2;i++)
{
            for(j=1;j<=2;j++)
            {
            printf("%d\n",s[i][j] );
            }
}
getch ();
}