-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.hpp
More file actions
157 lines (114 loc) · 4.56 KB
/
Copy pathArrayStack.hpp
File metadata and controls
157 lines (114 loc) · 4.56 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
150
151
152
153
154
155
156
157
/*
Copyright (c) 2017 waynezxcv <liuweiself@126.com>
https://github.com/waynezxcv/Playground
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef ArrayStack_hpp
#define ArrayStack_hpp
#include "Stack.hpp"
#include "IllegalParameterValue.hpp"
/******************** 用数组描述栈 *******************/
namespace LWTL {
template <typename T>
class ArrayStack : public Stack<T> {
public:
//构造函数
ArrayStack(int capacity = 10) : stackTop(-1),arrayLength(capacity) {
if (capacity < 1) {
throw IllegalParameterValue("capacity must > 0 ");
}
elements = new T [arrayLength];
}
//拷贝构造函数
ArrayStack(const ArrayStack<T>& rhs) {
arrayLength = rhs.arrayLength;
stackTop = rhs.stackTop;
elements = new T [arrayLength];
memcpy(elements, rhs.elements, sizeof(T) * arrayLength);
}
//拷贝赋值运算符
ArrayStack& operator = (const ArrayStack<T>& rhs) {
if (this == &rhs) {
return *this;
}
arrayLength = rhs.arrayLength;
stackTop = rhs.stackTop;
delete [] elements;
elements = new T [arrayLength];
memcpy(elements, rhs.elements, sizeof(T) * arrayLength);
return *this;
}
//析构函数
~ArrayStack() {
delete [] elements;
}
//判断是否为空
bool empty () const override {
return stackTop == -1;
}
//获取栈长度
int size() const override {
return stackTop + 1;
}
//获取栈顶元素
T& top() const override {
if (stackTop == -1) {
throw IllegalParameterValue("the stack is empty");
}
return elements[stackTop];
}
//入栈
void push (const T& theElement) override {
if (stackTop + 1 == arrayLength) {
T* tmp = new T [arrayLength * 2];
memcpy(tmp, elements, sizeof(T) * arrayLength);
delete [] elements;
elements = tmp;
arrayLength *= 2;
}
elements[stackTop + 1] = theElement;
stackTop ++;
}
//出栈
void pop () override {
if (stackTop == -1) {
throw IllegalParameterValue("the stack is empty");
}
//如果栈长度为stackTop为数组长度的四分之一时,将数组缩短到原来的二分之一
if (stackTop < arrayLength/4) {
T* tmp = new T [arrayLength/2];
memcpy(tmp, elements,arrayLength/2 * sizeof(T));
delete [] elements;
elements = tmp;
arrayLength = arrayLength/2;
}
elements[stackTop].~T();//调用栈顶元素的析构函数
stackTop --;
}
void show(){
for (int i = stackTop; i >= 0; i --) {
T element = elements[i];
std::cout<<"index"<<i<<":"<<element<<std::endl;
}
}
private:
int stackTop;//当前栈顶
T* elements;//保存元素的素组
int arrayLength;//数组的长度
};
}
#endif /* ArrayStack_hpp */