Header Ads Widget

program to reverse a number by using stack..

program:-

#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top;
int capacity;
int *array;
};

// function for create stack

struct stack* creatstack(int cap)
{
struct stack *stack;
stack=(struct stack*)malloc(sizeof(struct stack));
stack->capacity=cap;
stack->top=-1;
stack->array=(int *)malloc(sizeof(int)*stack->capacity);
return(stack);
}

// function for check stack is empty or not

int isempty(struct stack *stack)
{
if(stack->top==-1)
return(0);
else
return(1);
}

// function for check stack is full or not

int isfull(struct stack *stack)
{
if(stack->top==stack->capacity-1)
return(0);
else
return(1);
}

// function for push element into stack

void push(struct stack *stack,int val)
{
if(isfull(stack))
{
stack->top++;
stack->array[stack->top]=val;
}
else
printf("Stack is full");
}

// function for pop element from stack

int pop(struct stack* stack)
{
int item;
if(isempty(stack))
{
item=stack->array[stack->top];
stack->top--;
return(item);
}
else
return(0);
}
void main()
{
int n,r,rev=0,f=1,n1;
struct stack* stack;
scanf("%d",&n);
stack=creatstack(100);
n1=n;
while(n!=0)
{
r=n%10;
n=n/10;
push(stack,r);
}
while(isempty(stack))

r=pop(stack);
rev=r*f+rev;
f=f*10;
}
printf("%d",rev);
getch();
}



Post a Comment

0 Comments