-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_with_array.c
73 lines (61 loc) · 884 Bytes
/
stack_with_array.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
73
#define MAX 256
#include<stdio.h>
#include<stdlib.h>
struct stack{
int item[MAX];
int top;
};
typedef struct stack STACK;
void init(STACK*);
int isEmpty(STACK*);
int isFull(STACK*);
void pop(STACK*,int*);
void push(STACK*,int);
int main() {
STACK* s = (STACK*)malloc(sizeof(STACK));
int x;
init(s);
push(s,50);
push(s,150);
pop(s,&x);
printf("popped element: %d\n",x);
return 0;
}
void init(STACK* s) {
s->top = 0;
}
int isEmpty(STACK* s) {
if(s->top == 0) {
return 1;
}
else{
return 0;
}
}
int isFull(STACK* s) {
if(s->top == MAX) {
return 1;
}
else{
return 0;
}
}
void pop(STACK* s,int* x) {
if(isEmpty(s)) {
printf("Stack is empty.\n");
}
else{
s->top--;
*x = s->item[s->top];
printf("POP done.\n");
}
}
void push(STACK* s, int x) {
if(isFull(s)) {
printf("Stack is full.\n");
}
else{
s->item[s->top] = x;
s->top++;
}
}