-
Notifications
You must be signed in to change notification settings - Fork 23
/
stack.c
43 lines (41 loc) · 924 Bytes
/
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
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
typedef struct{
int top;
int items[MAX];
}stack;
void push(stack *, int);
int pop(stack *);
int main()
{
stack st;
st.top = -1;
push(&st,67);
push(&st,-2);
push(&st,72);
printf("%d\n",pop(&st));
printf("%d\n",pop(&st));
printf("%d\n",pop(&st));
return 0;
}
void push(stack *st, int x)
{
if(st->top == 8)
{
printf("The stack is overflow\n");
exit(1);
}
else
++(st->top);
st->items[st->top]= x;
}
int pop (stack *st)
{
if (st->top == -1)
{
printf("stack is underflow underflow");
exit(0);
}
return st->items[st->top--];
}