Stack

---

What it is?

A stack is a one-ended linear data structures which has two primary operations,namely push and pop ( Last in First out )

8C6EB8B71BEA69F6DDB90491F35ED9DE

Complexity55950B3218B7C018ED0FA433619F275D

Example_01_ Brackets

按顺序push

if 当前进栈符号为左括号,then 直接进栈

if 当前入栈符号为右括号,then 检查栈顶是否为对应的reverse bracket

若是,则括号匹配,pop栈顶符号,否则匹配失败,结束程序

重复上述操作直至栈为空,匹配成功,结束程序

源码实现

使用java.util.LinkedList构造链栈

链尾表示栈顶,用addLast实现push,removeLast实现pop

测试结果:

image-20210214170633034

源码:https://github.com/GigoGigo-CH/Learn_data_structures/tree/master/src/main/java/com/andy/algorithms/datastructures/stack

posted @ 2021-02-14 17:10  盐盐盐の锅  阅读(69)  评论(0编辑  收藏  举报