forked from shiningflash/Advance-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
39 lines (35 loc) · 764 Bytes
/
Stack.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10
typedef struct {
int top;
int arr[SIZE];
} Stack;
void push(Stack *st, int item) {
if (st->top == SIZE) {
printf("Error: Stack is full\n");
return;
}
st->arr[st->top] = item;
st->top = st->top + 1;
}
int pop(Stack *st) {
if (st->top == 0) {
printf("Error: Stack is empty\n");
exit(EXIT_FAILURE);
}
st->top = st->top - 1;
return st->arr[st->top];
}
int main() {
Stack mystack;
mystack.top = 0;
push(&mystack, 1);
push(&mystack, 2);
push(&mystack, 3);
printf("%d\n", pop(&mystack));
printf("%d\n", pop(&mystack));
printf("%d\n", pop(&mystack));
printf("%d\n", pop(&mystack));
return 0;
}