forked from Sjsingh101/Basic-C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_pointer.cpp
48 lines (45 loc) · 827 Bytes
/
stack_pointer.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
40
41
42
43
44
45
46
47
48
#include<cstdio>
struct stack{
int data[1024];
int top;
};
int push(struct stack *stk, int input){
if (stk->top <= 1024){
stk->top++;
stk->data[stk->top] = input;
return 0;
}
else{
printf("FULL\n");
return -1;
}
}
int pop(struct stack *stk){
if (stk->top != 0){
int temp = stk->top;
stk->top--;
return stk->data[temp];
}
else{
printf("No data\n");
return -1;
}
}
void init(struct stack *stk){
stk->top = 0;
for(int i=0;i<1024;i++){
stk->data[i] = 0;
}
}
int main(){
struct stack stk;
init(&stk);
for(int i=0;i<5;i++){
printf("push %d\n", i);
push(&stk, i);
}
for(int i=0;i<5;i++){
printf("pop ");
printf("%d\n", pop(&stk));
}
}