Update linkedlist_stack.cpp

仿写的链表栈
This commit is contained in:
qualifier1024 2022-11-28 10:15:18 +08:00 committed by GitHub
parent 06d4162ddb
commit 06424ef023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,40 @@
/* /*
* File: linkedlist_stack.cpp * File: linkedlist_stack.cpp
* Created Time: 2022-11-25 * Created Time: 2022-11-28
* Author: Krahets (krahets@163.com) * Author: qualifier1024 (2539244001@qq.com)
*/ */
#include "../include/include.hpp" #include "../include/include.hpp"
#include<list>
/* 基于链表实现的栈 */
class LinkedListStack {
list<int> 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();
}
};