-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.c
More file actions
66 lines (56 loc) · 1.06 KB
/
StackArray.c
File metadata and controls
66 lines (56 loc) · 1.06 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
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
void push(int value);
void pop();
int Top();
int IsEmpty();
void MakeEmpty();
int stack[MAX_SIZE];
int top = -1;
int main() {
push(5);
push(10);
push(15);
printf("Top value: %d\n", Top());
pop();
printf("Top value: %d\n", Top());
pop();
printf("Top value: %d\n", Top());
pop();
pop();
printf("Is stack empty? %s\n", IsEmpty() ? "Yes" : "No");
MakeEmpty();
printf("Is stack empty? %s\n", IsEmpty() ? "Yes" : "No");
return 0;
}
void push(int value) {
if (top == MAX_SIZE - 1) {
printf("Stack overflow\n");
return;
}
top++;
stack[top] = value;
}
void pop() {
if (top == -1) {
printf("Stack underflow\n");
}
top--;
}
int Top() {
if (top == -1) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}
int IsEmpty() {
if (top == -1) {
return 1;
}
return 0;
}
void MakeEmpty() {
top = -1;
}