Update linkedlist_stack.cpp

仿写的链表栈
pull/41/head
qualifier1024 2 years ago committed by GitHub
parent 06d4162ddb
commit 06424ef023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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();
}
};

Loading…
Cancel
Save