mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
Update array_stack.cpp
Make `vec` a private member.
This commit is contained in:
parent
f772e28acb
commit
56ef5a71cd
1 changed files with 17 additions and 9 deletions
|
@ -8,9 +8,9 @@
|
|||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
public:
|
||||
private:
|
||||
vector<int> vec;
|
||||
|
||||
public:
|
||||
/* 获取栈的长度 */
|
||||
int size() {
|
||||
return vec.size();
|
||||
|
@ -37,8 +37,14 @@ public:
|
|||
int top() {
|
||||
return vec.back();
|
||||
}
|
||||
|
||||
/* 返回 Vector */
|
||||
vector<int> toVector() {
|
||||
return vec;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化栈 */
|
||||
|
@ -51,7 +57,8 @@ int main() {
|
|||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "栈 stack = ";
|
||||
PrintUtil::printVector(stack->vec);
|
||||
vector<int> vec = stack->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top = stack->top();
|
||||
|
@ -60,7 +67,8 @@ int main() {
|
|||
/* 元素出栈 */
|
||||
int pop = stack->pop();
|
||||
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
|
||||
PrintUtil::printVector(stack->vec);
|
||||
vec = stack->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack->size();
|
||||
|
|
Loading…
Reference in a new issue