-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp stack
More file actions
61 lines (50 loc) · 987 Bytes
/
Copy pathapp stack
File metadata and controls
61 lines (50 loc) · 987 Bytes
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
#include <stdio.h>
#define MAX 100
int stack[MAX];
int top = -1;
// Check if stack is empty
int isEmpty() {
return top == -1;
}
// Check if stack is full
int isFull() {
return top == MAX - 1;
}
// Push element onto stack
void push(int x) {
if (isFull()) {
printf("Stack Overflow\n");
return;
}
stack[++top] = x;
printf("Pushed %d\n", x);
}
// Pop element from stack
int pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
// Peek top element
int peek() {
if (isEmpty()) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}
int main() {
push(10);
push(20);
push(30);
printf("Top element is %d\n", peek());
printf("Popped element is %d\n", pop());
printf("Popped element is %d\n", pop());
if (isEmpty())
printf("Stack is empty\n");
else
printf("Stack is not empty\n");
return 0;
}