forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
61 lines (51 loc) · 972 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#define MAX_STACK 5
using namespace std;
struct Stack{
int elemen[MAX_STACK];
int top;
};
void createStack(Stack &stack){
stack.top = -1;
}
bool isEmpty(Stack stack){
return stack.top == -1;
}
bool isFull(Stack stack){
return stack.top == MAX_STACK-1;
}
void push(Stack &stack, int i){
if (isFull(stack)) return;
stack.top++;
stack.elemen[stack.top] = i;
}
void pop(Stack &stack, int &i){
if (isEmpty(stack)) return;
i = stack.elemen[stack.top];
stack.top--;
}
int main(){
Stack s;
int x;
createStack(s);
push(s, 1);
push(s, 2);
push(s, 3);
push(s, 4);
pop(s,x);
push(s, 5);
push(s, 6);
pop(s,x);
pop(s,x);
pop(s,x);
pop(s,x);
push(s, 7);
push(s, 8);
push(s, 9);
pop(s,x);
cout << "Isi stack : ";
for(int i=s.top; i>=0; i--) cout << s.elemen[i] << " ";
cout << "\nNilai top : " << s.top << endl;
cout << "Nilai x : " << x << endl;
return 0;
}