-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
72 lines (72 loc) · 1.17 KB
/
stack.c
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
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
int MAX = 12;
typedef struct Stack
{
int arr[10];
int top;
} stack;
void push(stack *st, int data)
{
if (st->top == MAX - 1)
{
printf("stack overflow\n");
return;
}
st->top++;
st->arr[st->top] = data;
return;
}
void peek(stack *st)
{
if (st->top == -1)
{
printf("stack is empty");
return;
}
else
{
printf("%d\n", st->arr[st->top]);
return;
}
}
int pop(stack *st)
{
if (st->top == -1)
{
printf("stack underrflow\n");
return -1;
}
return st->arr[st->top--];
}
int main()
{
stack *st = (stack *)malloc(sizeof(stack));
st->top = -1;
int input;
int size = MAX;
int arr[size], i = 0;
while (size--)
{
scanf("%d", &input);
if (input == 0)
{
arr[i] = st->arr[st->top];
i++;
}
else if (input == -1)
{
int save = pop(st);
arr[i++] = save;
}
else
{
push(st, input);
}
}
for (int j = 0; j < 4; j++)
{
printf("%d ", arr[j]);
}
return 0;
}