-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStack_Static.cpp
More file actions
47 lines (35 loc) · 859 Bytes
/
Stack_Static.cpp
File metadata and controls
47 lines (35 loc) · 859 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
#include <bits/stdc++.h>
using namespace std;
class Stack {
int *stack, current, max;
public:
Stack(int max) {
this->max = max;
this->stack = new int[this->max];
this->current = -1;
}
void push(int element) {
if( this->current + 1 == this->max ) throw "Stack over flow";
this->stack[++current] = element;
}
int pop() {
if( this->current < 0 ) throw "Empty";
return this->stack[current--];
}
};
int main() {
Stack stack(5);
stack.push(5);
stack.push(4);
stack.push(3);
stack.push(2);
stack.push(1);
printf("Pop > %d\n", stack.pop());
printf("Pop > %d\n", stack.pop());
printf("Pop > %d\n", stack.pop());
printf("Pop > %d\n", stack.pop());
printf("Pop > %d\n", stack.pop());
/* Remover elementos da pilha vazia causa erro. */
//printf("Pop > %d\n", stack.pop());
return 0;
}