-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpop operation.c
46 lines (35 loc) · 900 Bytes
/
pop operation.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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
struct Stack {
int arr[MAX_SIZE];
int top;
};
// we initialize a stack here
void initialize(struct Stack *stack) {
stack->top = -1;
}
// checking if the stack is empty
int isEmpty(struct Stack *stack) {
return stack->top == -1;
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
exit(EXIT_FAILURE);
}
return stack->arr[stack->top--];
}
int main() {
struct Stack stack;
initialize(&stack);
// Push some elements onto the stack
stack.arr[++stack.top] = 10;
stack.arr[++stack.top] = 20;
stack.arr[++stack.top] = 30;
// Pop elements from the stack
printf("%d popped from the stack\n", pop(&stack));
printf("%d popped from the stack\n", pop(&stack));
printf("%d popped from the stack\n", pop(&stack));
return 0;
}