Tuesday, 20 October 2015

Implement Binary Search Tree in C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node{
    int data;
    struct node *left;
    struct node *right;
};

void minValue(struct node *root){
    if(root==NULL){
        printf("\nCan't find Min. Value ,  Empty Tree\n");
    }
    while(root->left!=NULL){
        root=root->left;
    }
    printf("Min Value = %d",root->data);
}


void maxValue(struct node *root){
    if(root == NULL){
        printf("\nCan't find Max. Value , Empty Tree\n");
    }
    while(root->right!=NULL){
        root=root->right;
    }
    printf("Max value = %d",root->data);
}


struct node *rightinsert(int data, struct node *root)
{
    struct node *temp=NULL;
    printf("Entered the right insert loop\n");
    if(root==NULL){
        temp = (struct node *)malloc(sizeof(struct node));
        if(temp==NULL){
            printf("Out of Space !! \n");
        }
        else{
            temp->data=data;
            temp->right=temp->left =NULL;
        }
    return temp;
    }
    else if(root->data>data){
        printf("entering to the left\n");
        root->left = rightinsert(data,root->left);
    }
    else if(root->data<data){
        printf("entering to the right\n");
        root->right = rightinsert(data,root->right);
    }
    else{
        printf("Element exist in the tree\n");
    }
    return root;
}

struct node *insert(struct node *root){
    int data;
    printf("Enter the data to be inserted\n");
    scanf("%d",&data);
    root=rightinsert(data,root);
    //free(temp);
    printf("printing root after insert operation\n");
    printf("%d\t",root->data);
    return root;
}

struct node *inorder(struct node *root)
{
    if(root!=NULL){
        inorder(root->left);
        printf("%d\t",root->data);
        inorder(root->right);
    }
    return root;   
}

struct node *preorder(struct node *root){
    if(root != NULL){
        printf("%d\t",root->data);
        preorder(root->left);
        preorder(root->right);
    }
    return root;
}

struct node *postorder(struct node *root){
    if(root!=NULL){
        postorder(root->left);
        postorder(root->right);
        printf("%d\t",root->data);
    }
return root;
}

int main()
{
    struct node *root =NULL;
    int i=0;
    for(i=0;i<10;i++){
    root=insert(root);
    }
    printf("\nInorder Representation \n");
    root=inorder(root);   
    printf("\nPreorder Representation \n");
    root=preorder(root);
    printf("\nPostOrder Representation \n");
    root=postorder(root);
    printf("\n\n");

    printf("\nFind Min Value in tree\n");
    minValue(root);
    printf("\nFInd Max Value in tree\n");
    maxValue(root);
   
    printf("\n\n");
   
return 0;
}

Monday, 5 October 2015

Simplest Program to implement Stack using Array

#include<stdio.h>
#define MAX 10
int top=-1;
void push(int arr[],int element);
int pop(int arr[]);
int peek(int arr[]);
int isFull();
int isEmpty();
int main()
{
//implement main function in your way. main function is written only for testing purpose
//let me know about any discrepency

    int arr[MAX];
    int data=0;
    int i=0;
    for(i=0;i<10;i++){
        scanf("%d",&data);
        push(arr,data);
    }
    push(arr,data);
    data=pop(arr);
    printf("%d",data);
    return 0;
}

void push(int arr[],int element)
{
    if(isFull()){
        printf("Array Full\n");
        return;
    }   
    arr[++top]=element;
}

int pop(int arr[]){
    if(!isEmpty){
        printf("Array Empty\n");
        return;
    }
    return arr[top--];
}

int peek(int arr[]){
    if(isEmpty){
        printf("Array Empty\n");
        return;
    }
    return arr[top];
}

int isFull()
{
    if(top==MAX-1)
        return 1;
    return 0;
}

int isEmpty()
{
    if(top==-1)
        return 1;
    return 0;
}

Sunday, 4 October 2015

Write A Program to Implement functionalities of a linklist in detail?


#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node {
    int data;
    struct node *link;
};
int search(struct node *head , int data);
struct node *insertion(struct node *head,int data);
struct node *insertatbegin(struct node *head,int data);
struct node* deletenode(struct node *head,int data);
struct node *insertatend(struct node *head,int data);
struct node *insertinmiddle(struct node *head , int data1,int data2);
void Print(struct node *head);

int length(struct node *head);
int getlengthR(struct node *head);
struct node *swapnodes(struct node *head,int x,int y);
int nthnode(struct node *head,int n);
int middle(struct node *head);
int nthnodefromlast(struct node * head,int n);

int main(){

//implement main function in your way. main function is written only for testing purpose
//let me know about any discrepency
    struct node *head=NULL;
    struct node *curr=NULL;
    int i=0;
    int data=0,data2=0;
    for(i=0;i<5;i++){
        scanf("%d",&data);
        head=insertion(head,data);
    }
    for(i=0;i<2;i++){
        printf("Enter a number to search\n");
        scanf("%d",&data);
        if(search(head,data)==1)
            printf("Element Found\n");
        else
            printf("Element Not Found\n");   
    }
   
    Print(head);
    scanf("%d",&data);
    head=deletenode(head,data);
    scanf("%d",&data);
    head=insertatbegin(head,data);
    Print(head);
    scanf("%d",&data);
    scanf("%d",&data2);
    head=insertinmiddle(head,data,data2);
    Print(head);
    scanf("%d",&data);
    head=insertatend(head,data);
    Print(head);

    printf("\n\n");
    int len = getlengthR(head);
    printf("Linklist length = %d",len);
    printf("\n\n");
    printf("Enter two numbers in the linklist to swap the nodesn\n");
    scanf("%d %d",&data,&data2);
    swapnodes(head,data,data2);
    Print(head);
    printf("\n\n");
    printf("\nEnter n to get the required number less than %d\n",length(head));
    scanf("%d",&data);
    data=nthnode(head,data);
    printf("Nth node is %d",data);
    printf("\n\n");
    int mid=middle(head);
    printf("middle element in the linklist = %d\n",mid);
    printf("\n\n");
    printf("Enter number to get nth node from last\n");
    scanf("%d",&data);
    int nthnodelast=nthnodefromlast(head,data);
    printf("nth node from last in the linklist = %d\n\n",nthnodelast);

   
    return 0;
}

void Print(struct node *head){
    struct node *curr=head;
    while(curr!=NULL){
        printf("%d\t",curr->data);
        curr=curr->link;
    }
}

struct node *insertion(struct node *head,int data){
    struct node *curr=head;
    struct node *temp=NULL;
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data = data;
    temp->link =NULL;
    if(head==NULL){
        head=temp;
    }
    else{
        while(curr->link!=NULL){
            curr=curr->link;
        }
        curr->link=temp;
    }
    //free(temp);
    return head;
}

int search(struct node *head , int data){
    struct node *curr=head;
    while(curr!=NULL){
        if(curr->data==data){
            return 1;
        }
        curr=curr->link;
    }
    return 0;
}

struct node *insertatbegin(struct node *head,int data){
    struct node *curr=NULL;
    curr=(struct node *)malloc(sizeof(struct node));
    curr->data=data;
    curr->link=head;
    head=curr;
    return head;
}

struct node *insertinmiddle(struct node *head , int data1,int data2){
    struct node *temp=NULL;
    struct node *curr=head;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data=data2;
    temp->link=NULL;
    while(curr->data!=data1){
        curr=curr->link;
    }
    temp->link=curr->link;
    curr->link=temp;
    return head;
}

struct node *insertatend(struct node *head,int data){
    struct node *temp=NULL;
    struct node *curr=head;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data=data;
    temp->link=NULL;
    while(curr->link!=NULL){
        curr=curr->link;
    }
    temp->link=curr->link;
    curr->link=temp;
    return head;
}

struct node* deletenode(struct node *head,int data){
    struct node *curr=head;
    struct node *temp=NULL;
    if(head->data=data){
        temp=head;
        head=head->link;
        free(temp);
    }
    else{
        while(curr->data!=data){
            curr=curr->link;
        }
    }
    return head;
}



int length(struct node *head){
        int len=0;
        struct node *curr=head;
        while(curr!=NULL){
                curr=curr->link;
                ++len;
        }
        return len;
}

int getlengthR(struct node *head){
        int len=0;
        if(head==NULL){
                return len;
        }
        else{
                return 1+getlengthR(head->link);
        }
}

struct node *swapnodes(struct node *head,int x ,int y){
        struct node *prevX = NULL ,*currX=NULL, *prevY=NULL, *currY=NULL;
        if(x==y)
                return head;
        while(currX && currX->data!=x){
                prevX=currX;
                currX=currX->link;
        }

        while(currY && currY->data != y){
                prevY=currY;
                currY=currY->link;
        }

        if(currX==NULL || currY==NULL){
                return head;
        }
        if(prevX!=NULL)
                prevX->link=currY;
        else
                head = currY;

        if(prevY!=NULL)
                prevY->link=currX;
        else
                head = currX;
        return head;
}

int nthnode(struct node *head ,int n){
        struct node *curr=head;
        if(n>length(head)){
                printf("Enter a valid number less than %d\n",length(head));
                scanf("%d",&n);
        }
        int count=0;
        while(count!=n-1){
                count++;
                curr = curr->link;
        }
        return curr->data;
}



int middle(struct node *head){
        struct node *curr=head;
        struct node *next=head;

        while(!(next==NULL || next->link==NULL)){
                curr=curr->link;
                next = next->link->link;
        }
        return curr->data;
}

int nthnodefromlast(struct node * head,int n){
        int nfromstart = length(head)-n+1;
        int data = nthnode(head,nfromstart);
        return data;
}