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] 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 b4fba44..489d2f3 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(); + } +};