-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMystack.cpp
94 lines (89 loc) · 1.52 KB
/
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
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
#include "Mystack.h"
template<class T>
TStack<T>::TStack(int n = 0) {
if (n < 0) {
throw "size is less then 0";
}
this->size = n;
this->mas = new T[size];
this->top = 0;
}
template<class T>
TStack<T>::TStack(TStack<T>& stack) {
delete[] this->mas;
this->size = stack.size;
this->top = stack.top;
this->mas = new T[this->size];
for (size_t i = 0; i < size; i++) {
mas[i] = stack.mas[i];
}
return *this;
}
template<class T>
void TStack<T>::Push(T a) {
if (IsFull()) {
throw "stack is full";
}
mas[top] = a;
top++;
}
template<class T>
T TStack<T>:: Get() {
if (IsEmpty()) {
throw "stack is empty";
}
this->top--;
return this->mas[top];
}
template<class T>
TStack<T>::~TStack() {
this->top = 0;
delete[] this->mas;
this->size = 0;
}
template<class T>
T TStack<T>::TopView() {
if (IsEmpty()) {
throw "stack is empty";
}
this->top--;
return this->mas[this->top];
}
template<class T>
int TStack<T>::GetSize() {
if (IsEmpty()) {
return 0;
}
return this->size;
}
template<class T>
T TStack<T>::GetTop() {
if (IsEmpty()) {
throw "stack is empty";
}
top--;
return this->mas[top];
}
template<class T>
bool TStack<T>::IsFull() {
if (this->top == this->size) {
return true;
}
return false;
}
template<class T>
bool TStack<T>::IsEmpty() {
if (this->top == 0) {
return true;
}
return false;
}
template<class T>
TStack<T>& TStack<T>:: operator=(const TStack<T>& stack) {
this->size = stack.size;
this->top = stack.top;
for (int i = 0; i < size; i++) {
this->mas[i] = stack.mas[i];
}
return *this;
}