From f772e28acb68e717613c403d886e2f940099dc10 Mon Sep 17 00:00:00 2001 From: qualifier1024 <102360259+qualifier1024@users.noreply.github.com> Date: Mon, 28 Nov 2022 22:23:27 +0800 Subject: [PATCH 1/2] Update array_stack.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 仿照linkedlist_stack实现的array_stack,补上了测试代码 --- .../chapter_stack_and_queue/array_stack.cpp | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/codes/cpp/chapter_stack_and_queue/array_stack.cpp b/codes/cpp/chapter_stack_and_queue/array_stack.cpp index e0112ed..444376c 100644 --- a/codes/cpp/chapter_stack_and_queue/array_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_stack.cpp @@ -1,8 +1,74 @@ /* * File: array_stack.cpp - * Created Time: 2022-11-25 - * Author: Krahets (krahets@163.com) + * Created Time: 2022-11-28 + * Author: qualifier1024 (2539244001@qq.com) */ #include "../include/include.hpp" +/* 基于数组实现的栈 */ +class ArrayStack { +public: + vector vec; + + /* 获取栈的长度 */ + int size() { + return vec.size(); + } + + /* 判断栈是否为空 */ + bool empty() { + return vec.empty(); + } + + /* 入栈 */ + void push(int num) { + vec.push_back(num); + } + + /* 出栈 */ + int pop() { + int oldTop = vec.back(); + vec.pop_back(); + return oldTop; + } + + /* 访问栈顶元素 */ + int top() { + return vec.back(); + } +}; + +/* Driver Code */ +int main() { + /* 初始化栈 */ + ArrayStack* stack = new ArrayStack(); + + /* 元素入栈 */ + stack->push(1); + stack->push(3); + stack->push(2); + stack->push(5); + stack->push(4); + cout << "栈 stack = "; + PrintUtil::printVector(stack->vec); + + /* 访问栈顶元素 */ + int top = stack->top(); + cout << "栈顶元素 top = " << top << endl; + + /* 元素出栈 */ + int pop = stack->pop(); + cout << "出栈元素 pop = " << pop << ",出栈后 stack = "; + PrintUtil::printVector(stack->vec); + + /* 获取栈的长度 */ + int size = stack->size(); + cout << "栈的长度 size = " << size << endl; + + /* 判断是否为空 */ + bool empty = stack->empty(); + cout << "栈是否为空 = " << empty << endl; + + return 0; +} From 56ef5a71cdaf65eb50dded32992a2248ed803038 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 29 Nov 2022 00:26:18 +0800 Subject: [PATCH 2/2] Update array_stack.cpp Make `vec` a private member. --- .../chapter_stack_and_queue/array_stack.cpp | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/codes/cpp/chapter_stack_and_queue/array_stack.cpp b/codes/cpp/chapter_stack_and_queue/array_stack.cpp index 444376c..8b08e17 100644 --- a/codes/cpp/chapter_stack_and_queue/array_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_stack.cpp @@ -8,37 +8,43 @@ /* 基于数组实现的栈 */ class ArrayStack { -public: +private: vector vec; - +public: /* 获取栈的长度 */ int size() { return vec.size(); } - + /* 判断栈是否为空 */ bool empty() { return vec.empty(); } - + /* 入栈 */ void push(int num) { vec.push_back(num); } - + /* 出栈 */ int pop() { int oldTop = vec.back(); vec.pop_back(); return oldTop; } - + /* 访问栈顶元素 */ int top() { return vec.back(); } + + /* 返回 Vector */ + vector 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 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(); @@ -69,6 +77,6 @@ int main() { /* 判断是否为空 */ bool empty = stack->empty(); cout << "栈是否为空 = " << empty << endl; - + return 0; }