-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack3.c
More file actions
62 lines (54 loc) · 1.21 KB
/
stack3.c
File metadata and controls
62 lines (54 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
int isEmpty(int top, int stack_arr[]);
void push(int x, int *top, int stack_arr[]);
int pop(int *top, int stack_arr[]);
void DecToBin(int num);
int main()
{
int num;
printf("Enter an integer : ");
scanf("%d",&num);
printf("Binary Equivalent is : ");
DecToBin(num);
return 0;
}
void DecToBin(int num)
{
int stack[MAX], top=-1, rem;
while(num!=0)
{
rem = num%8;
push(rem, &top, stack);
num/=8;
}
while(top!=-1)
printf("%d", pop(&top, stack));
printf("\n");
}
void push(int x, int *top, int stack_arr[])
{
if(*top == (MAX-1))
printf("Stack Overflow\n");
else
{
*top=*top+1;
stack_arr[*top] = x;
}
}
int pop(int *top, int stack_arr[])
{
int x;
if(*top == -1)
{
printf("Stack Underflow\n");
exit(1);
}
else
{
x = stack_arr[*top];
*top=*top-1;
}
return x;
}