From 06424ef0235d6fa469c39bb2ddc0c435a0f0f057 Mon Sep 17 00:00:00 2001 From: qualifier1024 <102360259+qualifier1024@users.noreply.github.com> Date: Mon, 28 Nov 2022 10:15:18 +0800 Subject: [PATCH 1/3] Update linkedlist_stack.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 仿写的链表栈 --- .../linkedlist_stack.cpp | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp index b4fba44b0..489d2f3bb 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp @@ -1,8 +1,40 @@ /* * File: linkedlist_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" +#include +/* 基于链表实现的栈 */ +class LinkedListStack { + list lst; +public: + LinkedListStack() { + // 初始化空链表 + lst.clear(); + } + /* 获取栈的长度 */ + int size() { + return lst.size(); + } + /* 判断栈是否为空 */ + bool isEmpty() { + return lst.empty(); + } + /* 入栈 */ + void push(int num) { + lst.push_back(num); + } + /* 出栈 */ + int pop() { + int oldTop = lst.back(); + lst.pop_back(); + return oldTop; + } + /* 访问栈顶元素 */ + int top() { + return lst.back(); + } +}; From c18affcea39a282af835631eebaf6db95f5820ca Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 28 Nov 2022 21:08:10 +0800 Subject: [PATCH 2/3] Update linkedlist_stack.cpp Add test code. --- .../linkedlist_stack.cpp | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp index 489d2f3bb..3fbfd0bd4 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp @@ -5,36 +5,80 @@ */ #include "../include/include.hpp" -#include /* 基于链表实现的栈 */ class LinkedListStack { - list lst; +private: + list list; public: LinkedListStack() { // 初始化空链表 - lst.clear(); + list.clear(); } /* 获取栈的长度 */ int size() { - return lst.size(); + return list.size(); } /* 判断栈是否为空 */ - bool isEmpty() { - return lst.empty(); + bool empty() { + return list.empty(); } /* 入栈 */ void push(int num) { - lst.push_back(num); + list.push_back(num); } /* 出栈 */ int pop() { - int oldTop = lst.back(); - lst.pop_back(); + int oldTop = list.back(); + list.pop_back(); return oldTop; } /* 访问栈顶元素 */ int top() { - return lst.back(); + return list.back(); + } + + /* 将 List 转化为 Array 并返回 */ + vector toVector() { + vector vec; + for (int num : list) { + vec.push_back(num); + } + return vec; } }; + + +/* Driver Code */ +int main() { + /* 初始化栈 */ + LinkedListStack* stack = new LinkedListStack(); + + /* 元素入栈 */ + stack->push(1); + stack->push(3); + stack->push(2); + stack->push(5); + stack->push(4); + cout << "栈 stack = "; + vector vec = stack->toVector(); + PrintUtil::printVector(vec); + + /* 访问栈顶元素 */ + int top = stack->top(); + cout << "栈顶元素 top = " << top << endl; + + /* 元素出栈 */ + int pop = stack->pop(); + cout << "出栈元素 pop = " << pop << ",出栈后 stack = "; + vec = stack->toVector(); + PrintUtil::printVector(vec); + + /* 获取栈的长度 */ + int size = stack->size(); + cout << "栈的长度 size = " << size << endl; + + /* 判断是否为空 */ + bool empty = stack->empty(); + cout << "栈是否为空 = " << empty << endl; +} From 30a115082b134c5301f179fa053bc5602dfa9b53 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 28 Nov 2022 21:10:29 +0800 Subject: [PATCH 3/3] Update linkedlist_stack.cpp Remove constructor --- codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp index 3fbfd0bd4..15eec8071 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp @@ -11,10 +11,6 @@ class LinkedListStack { private: list list; public: - LinkedListStack() { - // 初始化空链表 - list.clear(); - } /* 获取栈的长度 */ int size() { return list.size();