Skip to content

Commit aa390eb

Browse files
committed
update
1 parent 29d04fd commit aa390eb

File tree

27 files changed

+369
-1035
lines changed

27 files changed

+369
-1035
lines changed
File renamed without changes.

ArrayStack.hpp

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Copyright (c) 2017 waynezxcv <liuweiself@126.com>
3+
4+
https://github.com/waynezxcv/Playground
5+
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
26+
27+
28+
#ifndef ArrayStack_hpp
29+
#define ArrayStack_hpp
30+
31+
#include "Stack.hpp"
32+
#include "IllegalParameterValue.hpp"
33+
34+
/******************** 用数组描述栈 *******************/
35+
36+
37+
namespace LWTL {
38+
39+
40+
template <typename T>
41+
42+
class ArrayStack : public Stack<T> {
43+
44+
public:
45+
46+
//构造函数
47+
ArrayStack(int capacity = 10) : stackTop(-1),arrayLength(capacity) {
48+
if (capacity < 1) {
49+
throw IllegalParameterValue("capacity must > 0 ");
50+
}
51+
elements = new T [arrayLength];
52+
}
53+
54+
//拷贝构造函数
55+
ArrayStack(const ArrayStack& rhs) {
56+
printf("test2\n");
57+
arrayLength = rhs.arrayLength;
58+
stackTop = rhs.stackTop;
59+
elements = new T [arrayLength];
60+
memcpy(elements, rhs.elements, sizeof(T) * arrayLength);
61+
}
62+
63+
64+
//拷贝赋值运算符
65+
ArrayStack& operator = (const ArrayStack& rhs) {
66+
printf("test1\n");
67+
if (this == &rhs) {
68+
return *this;
69+
}
70+
arrayLength = rhs.arrayLength;
71+
stackTop = rhs.stackTop;
72+
elements = new T [arrayLength];
73+
memcpy(elements, rhs.elements, sizeof(T) * arrayLength);
74+
return *this;
75+
}
76+
77+
//析构函数
78+
~ArrayStack() {
79+
delete [] elements;
80+
}
81+
82+
//判断是否为空
83+
bool empty () const override {
84+
return stackTop == -1;
85+
}
86+
87+
//获取栈长度
88+
int size() const override {
89+
return stackTop + 1;
90+
}
91+
92+
//获取栈顶元素
93+
T& top() const override {
94+
if (stackTop == -1) {
95+
throw IllegalParameterValue("the stack is empty");
96+
}
97+
return elements[stackTop];
98+
}
99+
100+
//入栈
101+
void push (const T& theElement) override {
102+
103+
if (stackTop + 1 == arrayLength) {
104+
T* tmp = new T [arrayLength * 2];
105+
memcpy(tmp, elements, sizeof(T) * arrayLength);
106+
delete [] elements;
107+
elements = tmp;
108+
arrayLength *= 2;
109+
}
110+
111+
elements[stackTop + 1] = theElement;
112+
stackTop ++;
113+
}
114+
115+
116+
//出栈
117+
void pop () override {
118+
119+
if (stackTop == -1) {
120+
throw IllegalParameterValue("the stack is empty");
121+
}
122+
123+
//如果栈长度为stackTop为数组长度的四分之一时,将数组缩短到原来的二分之一
124+
if (stackTop < arrayLength/4) {
125+
T* tmp = new T [arrayLength/2];
126+
memcpy(tmp, elements,arrayLength/2 * sizeof(T));
127+
delete [] elements;
128+
elements = tmp;
129+
arrayLength = arrayLength/2;
130+
}
131+
132+
elements[stackTop].~T();//调用栈顶元素的析构函数
133+
stackTop --;
134+
}
135+
136+
private:
137+
138+
int stackTop;//当前栈顶
139+
T* elements;//保存元素的素组
140+
int arrayLength;//数组的长度
141+
142+
};
143+
}
144+
145+
146+
147+
#endif /* ArrayStack_hpp */

C/C_Review/a.out

-8.89 KB
Binary file not shown.

0 commit comments

Comments
 (0)