mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:16:28 +08:00
Update linkedlist_stack.cpp
仿写的链表栈
This commit is contained in:
parent
06d4162ddb
commit
06424ef023
1 changed files with 34 additions and 2 deletions
|
@ -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<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…
Reference in a new issue