Showing posts with label Data structure. Show all posts
Showing posts with label Data structure. Show all posts

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;
}

Sunday, December 21, 2014

Replace all ‘0’ with ‘5’ in an input Integer

 /* Use of array to store all digits is not allowed.*/
#include<stdio.h>
static int x=0;
int fun(int num)
{
    if(num==0)
    {
    return;
    }
    else
    {
    int rem=num%10;
    if(rem==0)
    {
     rem=5;
    }
    num=num/10;
    fun(num);
    x=x*10+rem;
    }
return x;
}
int main()
{
printf("num is %d ",fun(105));
return 0;
}

Saturday, December 20, 2014

"QQ.h" file for Queue Operation

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int info;
    struct node *link;
} *front=NULL,*rear=NULL;


void insert(int item)
{

    struct node *tmp;
    tmp=(struct node*)malloc(sizeof(struct node));
    if(tmp==NULL)
        {
        printf("Memory  not avaliable\n");
        return ;
        }
    tmp->info=item;
    tmp->link=NULL;
    if(front==NULL)
    {
        front=tmp;
    }
    else
        rear->link=tmp;
        rear=tmp;
}


int del()
{
    struct node *tmp;
    int item;
    if(isEmpty())
        {
            printf("Queue underflow\n");
            exit(1);
        }
    tmp=front;
    item=tmp->info;
    front=front->link;
    free(tmp);
    return item;
}

Reverse of Queue using Stack

 /*  1.First insert all  elements in rear side of Queue.
     2.Secondly,delete elements one by one from   front    of Queue and puts those in a stack .
  3.Then pop and print the elements until empty */

#include<stdio.h>
#include "stack.h"  //given below
#include"QQ.h"   //given below

int main()
{
    int n,arr[20],i,j=0;
    struct stack s;
    initstack(&s);
    printf("Enter no");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter values:");
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
        insert(arr[i]);
    }
    while(j!=n)
    {
        push(&s,del());
        j++;
    }
    printf("Reverse is ");
    while(s.top!=-1)
    {
        printf("%d",pop(&s));
    }
    printf("\n");
return 0;
}

get_Minimum.h file

/* save as "get_Minimum.h" file  */
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct stack
{
int arr[MAX];
int getMin[MAX];
int top;
};
void initstack(struct stack *s)
{
s->top=-1;
}
void push(struct stack *s,int item)
{
if(s->top==MAX-1)
{
printf("stack is full\n");
}
else
{
if(s->top==-1)
{
s->top++;
s->getMin[s->top]=item;
s->arr[s->top]=item;
}
else if(item<=(s->getMin[s->top]))
{
   printf("push in else if is %d \n",s->getMin[s->top]);
s->top++;
s->getMin[s->top]=item;
s->arr[s->top]=item;
}
else
{
int x=s->getMin[s->top];
printf("push in else is %d\n",s->getMin[s->top]);
s->top++;
s->arr[s->top]=item;
s->getMin[s->top]=x;
}
}


}
int pop(struct stack *s)
{
int data,min;
if(s->top==-1)
{
printf("Stack is empty.\n");
exit(1);
}
else
{
data=s->arr[s->top];
min=s->getMin[s->top];
s->top--;
printf("\n minimum is %d \n",min);
return data;
}
}

How to design a stack such that getMinimum() should be O(1) ??


#include<stdio.h>
#include<stdlib.h>
#include"get_Minimum.h"
#define MAX 10
int main()
{
struct stack s;
int i,item;
int  ch;
initstack(&s);
while(1)
{
printf("Enter 1. for Push\n");
printf("Enter 2. for pop  with minimum value\n");
printf("Enter 3. for exit.\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter item:");
scanf("%d",&item);
push(&s,item);
break;
case 2:
printf("poped element is %d \n",pop(&s));
break;
case 3:
exit(1);
break;
default:
printf("Wrong Input\n");
}
}
return 0;

}http://www.geeksforgeeks.org/design-and-implement-special-stack-data-structure/

Thursday, December 18, 2014

Implement Queue Using Link List in c

#include<stdio.h>
#include<stdlib.h>
#include "QUEUE.h"  //given before
int main()
{
struct node *s1=NULL,*s2=NULL;
int choice ,item;
while(1)

{
printf("\n1.Insert\n");
printf("2. Delete\n");
printf("3. Display the element at the front\n");
printf("4. Display all element of the queue\n");
printf("5. Quit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input the element for adding in queue:");
scanf("%d",&item);
insert(&s1,&s2,item);
break;
case 2:
printf("deleted item is %d",del(&s2));
break;
case 3:
printf("Element at the front of Queue is %d\n",peek(s2));
break;
case 4:
display(s2);
break;
case 5:
exit(1);
}
}
return 0;
}

Tuesday, December 16, 2014

Queue Implemention Using Two Stacks in c

/* Queue implemention using two stack  */


#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
#define MAX 20
int main()
{
    struct stack s1,s2;
    int i,item;
    int  ch;
    initstack(&s1);
    initstack(&s2);
    while(1)
    {
        printf("Enter 1. for Push\n");
        printf("Enter 2. for pop\n");
        printf("Enter 3. for exit.\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
        printf("Enter item:");
        scanf("%d",&item);
        push(&s1,item);
        break;
        case 2:
        if(isEmpty(&s2))
        {   
        while(!isEmpty(&s1))
        {
        push(&s2,pop(&s1));
        }
        }
        printf("poped element is %d",pop(&s2));
        break;
        case 3:
        exit(1);
        break;
        default:
        printf("Wrong Input\n");
        }
    }
return 0;
}

Monday, December 15, 2014

Sort a stack elements using recursion in c

#include<stdio.h>
#include"stack.h"    //given before
void insertAtBottom(struct stack *S,int data)
{
   
    if(isEmpty(S))
    {
    push(S,data);
    return;
    }
    if(topElement(S) >data)
    {
    int temp=pop(S);   
    insertAtBottom(S,data);
    push(S,temp);
    }
    else
    {
    push(S,data);
    }
}
void Sort_Stack(struct stack *S)
{
    int data;
    if(isEmpty(S))
    {
    return ;
    }   
    else
    {
    data=pop(S);
    Sort_Stack(S);
    insertAtBottom(S,data);
    }
}

int main()
{
    struct stack s;
    int i=0,x;
    initstack(&s);
    printf("Before Reverse:\n");
    while(i<6)
    {
    printf("Enter value:");
    scanf("%d",&x);
    push(&s,x);
    i++;
    }
    Sort_Stack(&s);
    printf("After Reverse:");
    display(&s);
return 0;
}
   

Reverse the elements of stack using only stack operation(push() and pop()) in c

 Algorithm:
* Fiorst pop all the elements of the stack till it becomes empty.
* For each call in recursion ,insert the element at the bottom of the stack.

#include<stdio.h>
#include"stack.h"
void insertAtBottom(struct stack *S,int data)
{
    if(isEmpty(S))
    {
    push(S,data);
    return ;
    }
    else
    {
    int temp=pop(S);
    insertAtBottom(S,data);
    push(S,temp);
    }
}
void Reverse_Stack(struct stack *S)
{
    int data;
    if(isEmpty(S))
    {
    return ;
    }   
    else
    {
    data=pop(S);
    Reverse_Stack(S);
    insertAtBottom(S,data);
    }
}

int main()
{
    struct stack s;
    int i;
    initstack(&s);
    printf("Before Reverse:");
    for(i=0;i<=5;i++)
    {
    printf("%d-->",i);
    push(&s,i);
    }
    Reverse_Stack(&s);
    printf("After Reverse:");
    display(&s);
return 0;
}