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

"stack.h" file

 /* Stack Operation with push and pop */
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

struct stack
{
    int arr[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
    {
    s->top++;
    s->arr[s->top]=item;
    }
}


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

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