-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.cpp
132 lines (104 loc) · 2.98 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//*****************************************************************************************************
//
// This program demonstrates the use of the Stack class by instantiating an array-based list
// stack of short integers and performing its methods.
//
// Other files required:
// 1. stack.h - header file for the Stack class
//
//*****************************************************************************************************
#include "stack.h"
#include <iostream>
using namespace std;
//*****************************************************************************************************
void displayStack(bool success, Stack<short> &stack);
void checkState(Stack<short> &stack);
//*****************************************************************************************************
int main() {
Stack<short> shortStack(10);
short num;
bool success;
checkState(shortStack);
for (int i = 0; i < 10; ++i) {
num = 10 * (i + 1);
success = shortStack.push(num);
if (success)
cout << "pushed: " << num << endl;
displayStack(success, shortStack);
}
for (int i = 0; i < 10; ++i) {
success = shortStack.pop(num);
if (success)
cout << "popped: " << num << endl;
displayStack(success, shortStack);
}
return 0;
}
//*****************************************************************************************************
void displayStack(bool success, Stack<short> &stack) {
short num;
int numVal;
if (success) {
numVal = stack.getNumValues();
stack.peek(num);
if (numVal > 0)
cout << "numValues: " << numVal
<< "\tpeeked: " << num << endl;
else
cout << "numValues: " << numVal << endl;
}
checkState(stack);
}
//*****************************************************************************************************
void checkState(Stack<short> &stack) {
if (stack.isFull())
cerr << "\nstack is full\n\n";
else if (stack.isEmpty())
cerr << "\nstack is empty\n\n";
}
//*****************************************************************************************************
/*
stack is empty
pushed: 10
numValues: 1 peeked: 10
pushed: 20
numValues: 2 peeked: 20
pushed: 30
numValues: 3 peeked: 30
pushed: 40
numValues: 4 peeked: 40
pushed: 50
numValues: 5 peeked: 50
pushed: 60
numValues: 6 peeked: 60
pushed: 70
numValues: 7 peeked: 70
pushed: 80
numValues: 8 peeked: 80
pushed: 90
numValues: 9 peeked: 90
pushed: 100
numValues: 10 peeked: 100
stack is full
popped: 100
numValues: 9 peeked: 90
popped: 90
numValues: 8 peeked: 80
popped: 80
numValues: 7 peeked: 70
popped: 70
numValues: 6 peeked: 60
popped: 60
numValues: 5 peeked: 50
popped: 50
numValues: 4 peeked: 40
popped: 40
numValues: 3 peeked: 30
popped: 30
numValues: 2 peeked: 20
popped: 20
numValues: 1 peeked: 10
popped: 10
numValues: 0
stack is empty
*/