Using the operations of stack using linked list convert a decimal number to binary.
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int Stack_Empty(int first, int stack_arr[]);
void push(int m, int *first, int arr[]);
int pop(int *first, int arr[]);
void Decimal_Binary(int n);
int main()
{
int n;
printf("Enter an integer : ");
scanf("%d",&n);
printf("It's binary equivalent is : ");
Decimal_Binary(n);
return 0;
}
void Decimal_Binary(int n)
{
int stack[MAX], first=-1, rem;
while(n!=0)
{
rem = n%2;
push(rem, &first, stack);
n/=2;
}
while(first!=-1)
printf("%d", pop(&first, stack));
printf("\n");
}
void push(int m, int *first, int arr[])
{
if(*first == (MAX-1))
printf("Stack Overflow\n");
else
{
*first=*first+1;
arr[*first] = m;
}
}
int pop(int *first, int arr[])
{
int m;
if(*first == -1)
{
printf("Stack Underflow\n");
exit(1);
}
else
{
m = arr[*first];
*first=*first-1;
}
return m;
}
Comments
Leave a comment