HOME

Monday, March 27, 2017

Binary Tree -Data Structure using C Language

#include<stdio.h>
#include<conio.h>
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
struct node *root=NULL;
struct node *insert(struct node*,int);
void displayPreorder(struct node*);
void displayInorder(struct node*);
void displayPostorder(struct node*);
int count=1;
void main()
{
    int value,choice;
    while(1)
    {
        printf("\n1.Insert");
        printf("\n2.Display in pre order");
        printf("\n3.Display in In-order");
        printf("\n4.Display in Post order");
        printf("\n5.Exit");
        printf("\nEnter your choice\n");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            printf("Enter Value\n");
            scanf("%d",&value);
            root=insert(root, value);
            break;
        case 2:
            displayPreorder(root);
            break;
        case 3:
            displayInorder(root);
            break;
        case 4:
            displayPostorder(root);
            break;
        case 5:
            exit(1);
            break;
        default:
            printf("\nInvalid Input");
            break;


        }
    }
}

struct node *insert(struct node *r, int v)
{
    if(r==NULL)
    {
        r=(struct node*)malloc(sizeof(struct node));
        r->left=NULL;
        r->right=NULL;
        r->data=v;
        count++;
    }
    else{
        if(count%2==0)
        {
            r->left=insert(r->left,v);
        }
        else{
            r->right=insert(r->right,v);
        }
    }
    return r;
}
void displayPreorder(struct node *r)
{
    if(r!=NULL)
    {
        printf("%d\n",r->data);
        displayPreorder(r->left);
        displayPreorder(r->right);
    }
}

void displayInorder(struct node *r)
{
    if(r!=NULL)
    {
        displayInorder(r->left);
        printf("%d\n",r->data);
        displayInorder(r->right);
    }
}
void displayPostorder(struct node *r)
{
    if(r!=NULL)
    {
        displayPostorder(r->left);
        displayPostorder(r->right);
        printf("%d\n",r->data);
    }
}

Friday, March 24, 2017

Shoes Store Sample program using C language !

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[100],i,n,value,loc1,loc2,counter;
    char ch;
    printf("Enter the Quantity of Shoes\n");
    scanf("%d",&n);
    printf("Enter the shoes to put in store\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("The Shoes Available in store are\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    do{
            counter=0;
    loc1=0;
    loc2=0;
    printf("Enter the shoes you want to buy\n");
    scanf("%d",&value);
    for(i=0;i<n;i++)
    {
        if(value==a[i])
        {
            counter++;
            if(counter==1)
            {
             loc1=i;
            }
            else if(counter==2)
            {
                loc2=i;
            }
        }

    }
    if(counter>=2)
    {
        n--;
        for(i=loc1;i<n;i++)
        {
            a[i]=a[i+1];
        }
        n--;
        loc2--;
        for(i=loc2;i<n;i++)
        {
            a[i]=a[i+1];
        }
        printf("\nYou bought Shoes Successfully\n");
    }
    else if(counter==1)
    {
        printf("\nShoes is not available in pairs");
    }
    else
    {
        printf("\nShoes is out of Stock\n");
    }
    printf("\nNow Available shoes are\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    printf("To Buy more shoes Press 'y' or Any key to Terminate\n");
    ch=getch();
    }while(ch=='y' || ch=='Y');
}

Monday, March 6, 2017

Various operation in Stack !

#include<stdio.h>
#include<conio.h>
#define max 5
void push(int);
int pop(void);
void traverse(void);
   int val,ch,value;
    int stack[max];
    int top=-1;
void main()
{


    while(1)
    {
    printf("\n1.Push data");
    printf("\n2. pop Data");
    printf("\n3. Traverse");
    printf("\n4. Exit");
    printf("\nEnter your choice\n");
    scanf("%d",&ch);
switch(ch)
{
case 1:
    if(top==max-1)
    {
        printf("\Stack overflow");
    }
    else{
    printf("\nEnter Data to Push in stack\n");
    scanf("%d",&value);
    top++;
    push(value);
    }
    break;
case 2:
    if(top==-1)
    {
        printf("\n stack underflow");
    }
    else
        {
    val=pop();
    printf("\n%d is Removed",val);
    top--;
    }
    break;
case 3:
    if(top==-1)
    {
        printf("\nNO Record Found");
    }
    else{
    traverse();}
    break;
case 4:
    exit(1);
    break;
default:
    printf("\nWrong Input\n");
}
    }
}
void push(int value)
{
    stack[top]=value;
}
int pop()
{
    return(stack[top]);
}
void traverse()
{
    int i;
    for(i=top;i>=0;i--)
    {
        printf("\n%d",stack[i]);
    }
}