-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTStack.cpp
More file actions
150 lines (133 loc) · 2.91 KB
/
TStack.cpp
File metadata and controls
150 lines (133 loc) · 2.91 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
template <class T>
struct TNode {
T val;
TNode* next = nullptr;
public:
TNode() : val(T()) {}
TNode(T value) : val(value) {}
};
template <class T>
class TStack {
TNode<T>* pFirst;
public:
TStack();
TStack(const TStack& oth);
~TStack();
bool operator==(const TStack& oth) const;
bool operator!=(const TStack& oth) const;
TStack<T>& operator=(const TStack& oth);
bool isEmpty() const { return pFirst == nullptr; }
bool isFull() const;
void Clear();
T Top() const;
T Pop();
void Push(T value);
};
template <class T>
TStack<T>::TStack() : pFirst(nullptr) {}
template <class T>
TStack<T>::TStack(const TStack& oth) : pFirst(nullptr) {
if (oth.pFirst == nullptr) {
return;
}
pFirst = new TNode<T>(oth.pFirst->val);
TNode<T>* tmp = oth.pFirst->next;
TNode<T>* prev = pFirst;
while (tmp != nullptr) {
TNode<T>* node = new TNode<T>(tmp->val);
prev->next = node;
tmp = tmp->next;
prev = prev->next;
}
}
template <class T>
TStack<T>::~TStack() {
Clear();
}
template <class T>
bool TStack<T>::operator==(const TStack& oth) const {
if (this == &oth) {
return true;
}
TNode<T>* tmp1 = pFirst;
TNode<T>* tmp2 = oth.pFirst;
while (tmp1 != nullptr && tmp2 != nullptr) {
if (tmp1->val != tmp2->val) {
return false;
}
tmp1 = tmp1->next;
tmp2 = tmp2->next;
}
return tmp1 == tmp2;
}
template <class T>
bool TStack<T>::operator!=(const TStack& oth) const {
return !(*this == oth);
}
template <class T>
TStack<T>& TStack<T>::operator=(const TStack& oth) {
if (this == &oth) {
return *this;
}
Clear();
if (oth.pFirst == nullptr) {
pFirst = nullptr;
return *this;
}
pFirst = new TNode<T>(oth.pFirst->val);
TNode<T>* tmp = oth.pFirst->next;
TNode<T>* prev = pFirst;
while (tmp != nullptr) {
TNode<T>* node = new TNode<T>(tmp->val);
prev->next = node;
tmp = tmp->next;
prev = prev->next;
}
return *this;
}
template <class T>
bool TStack<T>::isFull() const {
try {
TNode<T>* tmp = new TNode<T>();
delete tmp;
return false;
}
catch (...) {
return true;
}
}
template <class T>
void TStack<T>::Clear() {
while (pFirst != nullptr) {
TNode<T>* p = pFirst;
pFirst = pFirst->next;
delete p;
}
}
template <class T>
T TStack<T>::Top() const {
if (pFirst == nullptr) {
throw -1;
}
return pFirst->val;
}
template <class T>
T TStack<T>::Pop() {
if (isEmpty()) {
throw -1;
}
T res = pFirst->val;
TNode<T>* p = pFirst;
pFirst = pFirst->next;
delete p;
return res;
}
template <class T>
void TStack<T>::Push(T value) {
if (isFull()) {
throw -1;
}
TNode<T>* tmp = new TNode<T>(value);
tmp->next = pFirst;
pFirst = tmp;
}