-
Notifications
You must be signed in to change notification settings - Fork 4
/
stack.cpp
87 lines (75 loc) · 1.96 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <ostream> // noetig?
const int MAX = 3;
class Stack {
public:
Stack();
bool push(int top);
int pop();
bool empty() const;
int peek() const;
int const * begin() const;
int const * end() const;
private:
int data_[MAX];
int size_;
};
Stack::Stack(): size_(0) {}
bool Stack::push(int top) {
if (size_ >= MAX) return false;
data_[size_] = top;
size_++;
return true;
}
int Stack::pop() {
if(empty()) return -1;
return data_[--size_];
}
bool Stack::empty() const {
return size_ == 0;
}
int Stack::peek() const {
return data_[size_-1];
}
int const * Stack::begin() const {
return data_;
}
int const * Stack::end() const {
return data_+size_;
}
int main()
{
std::cout << "Der tolle Stack-Test" << std::endl;
Stack s;
std::cout << "Leer?" << std::endl;
std::cout << s.empty() << std::endl;
std::cout << "Pushen" << std::endl;
std::cout << s.push(4) << std::endl;
std::cout << "Leer?" << std::endl;
std::cout << s.empty() << std::endl;
std::cout << "3xPushen" << std::endl;
std::cout << s.push(3) << std::endl;
std::cout << s.push(5) << std::endl;
std::cout << s.push(6) << std::endl;
std::cout << "Peeken:" << std::endl;
std::cout << s.peek() << std::endl;
std::cout << "Leer?" << std::endl;
std::cout << s.empty() << std::endl;
std::cout << "Iterieren" << std::endl;
for (int const * i = s.begin(); i < s.end(); i++) {
std::cout << *i << std::endl;
}
std::cout << "3xPoppen" << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << "Leer?" << std::endl;
std::cout << s.empty() << std::endl;
std::cout << "Ueberpoppen" << std::endl;
std::cout << s.pop() << std::endl;
std::cout << "Push+Pop" << std::endl;
std::cout << s.push(42) << std::endl;
std::cout << s.pop() << std::endl;
std::cout << "Leer?" << std::endl;
std::cout << s.empty() << std::endl;
}