-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.cpp
More file actions
51 lines (44 loc) · 860 Bytes
/
MyStack.cpp
File metadata and controls
51 lines (44 loc) · 860 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
48
49
50
51
#include "MyStack.h"
MyStack::MyStack()
{
this->top = new StackElem();
this->top->str = "";
this->top->next = nullptr;
}
MyStack::~MyStack()
{
StackElem* tmp = this->top;
while (this->top != nullptr) {
this->top = this->top->next;
delete tmp;
tmp = this->top;
}
}
void MyStack::setTop(string str)
{
this->top = new StackElem();
this->top->str = str;
this->top->next = nullptr;
}
void MyStack::addToStack(string str)
{
StackElem* add = new StackElem();
add->str = str;
if (this->top == nullptr) add->next = nullptr;
else add->next = this->top;
this->top = add;
}
void MyStack::removeTop()
{
if (this->top == nullptr) return;
else {
StackElem* tmp = this->top;
this->top = this->top->next;
delete tmp;
}
return;
}
string MyStack::getLastState()
{
return this->top->str;
}