-
Notifications
You must be signed in to change notification settings - Fork 0
/
myStack.cpp
45 lines (37 loc) · 846 Bytes
/
myStack.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
//Name: Caroline Su
//Student# : A01369603
#include <iostream>
#include <sstream>
#include "myStack.hpp"
MyStack::MyStack():index(-1){}
bool MyStack::push(int number) {
if (full()) return false;
intArray[++index] = number;
return true;
}
void MyStack::pop() {
if (index != -1) {
index--;
}
}
int MyStack::top() const {
if(empty()) return -1;
return intArray[index];
}
bool MyStack::empty() const{
return index == -1;
}
bool MyStack::full() const{
return index == (SIZE - 1);
}
std::string MyStack::print() const {
std::stringstream output;
output << "[";
for (int i = index; i >= 1; i--) {
output << intArray[i] << ", ";
// std::cout << intArray[i] << " ";
}
output << intArray[0] << "]" << std::endl;
return output.str();
// std::cout << std::endl;
}