Advanced programmers lessons - Architect of the road (4) - Stack

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37609579/article/details/99609574

First, the definition of the stack

[Baidu Encyclopedia] as a stack data structure , only a special insertions and deletions at one end of the linear form . It stores data in accordance with the principle of last-out, first enter the data is pushed onto the stack at the end, the final data in the top of the stack, when you need to read data starts pop-up data (the last data is first read out) from the top of the stack. Stack having a memory effect, insertion and deletion of stack operation, it is unnecessary to change the bottom of the stack pointer .

Stack allow specific insertion and deletion operations in the same end of the linear form . It allows for insertion and deletion of one end called stack (Top), and the other end of the stack bottom (bottom); fixed bottom of the stack, the stack while floating; called an empty stack if the number of elements in the stack is zero. Commonly referred to insert into the stack (PUSH), delete is called the stack back (POP). Stack also known as LIFO table.

Since the stack data structure may only be performed at one end, and thus in accordance with LIFO (LIFO, Last In First Out) principle of operation. Stack also known as LIFO table.

Second, a schematic view of the stack

Third, the implementation of the stack

Stack may be sequentially stored, stored in the chain can also be used, called sequential stack and a link stack.

Four, Java in the stack implementation example

1. Using stack implementations string in reverse

 
  1.  
    @Test
  2.  
    public void testStringReversal(){
  3.  
    Stack stack=new java.util.Stack();
  4.  
    String str="Hello World!";
  5.  
     
  6.  
    char[] chars = str.toCharArray();
  7.  
    for (char c : chars) {
  8.  
    stack.push(c);
  9.  
    }
  10.  
     
  11.  
    while (!stack.isEmpty()) {
  12.  
    System.out.print(stack.pop());
  13.  
    }
  14.  
     
  15.  
    }
 

2. Use the stack to achieve matching brackets

假设表达式中只允许两种括号:()、{};
正确表达顺序为:()或{}或({})或{({}{})}的形式;如{(}或(})或({)}的表达形式均不对。
算法的设计思想:

  1)出现左括弧则进栈;

  2)出现右括弧则首先检测栈是否为空;

  •     若栈空则表明此右括弧多余,表达式不匹配。
  •     否则和栈顶数据比较,若匹配则栈顶出栈。
  •     否则表明表达式不匹配;

  3)最后若栈空,则表明匹配成功;否则表明不匹配。

 
  1.  
    /**
  2.  
    * 此题还可以引申至配对字符符匹配问题,如单引号,双引号匹配问题。
  3.  
    */
  4.  
    public class ExpStackMatching {
  5.  
    public boolean matching(String expression) {
  6.  
    if (expression == null || expression == "") {
  7.  
    System.out.println("输入表达式为空或没有输入表达式");
  8.  
    }
  9.  
    Stack<Character> stack = new java.util.Stack<Character>();
  10.  
    for (int index = 0; index < expression.length(); index++) {
  11.  
    switch (expression.charAt(index)) {
  12.  
    case '(':
  13.  
    stack.push(expression.charAt(index));
  14.  
    break;
  15.  
    case '{':
  16.  
    stack.push(expression.charAt(index));
  17.  
    break;
  18.  
    case ')':
  19.  
    if (!stack.empty() && stack.peek() == '(') {
  20.  
    stack.pop();
  21.  
    }
  22.  
    break;
  23.  
     
  24.  
    case '}':
  25.  
    if (!stack.empty() && stack.peek() == '{') {
  26.  
    stack.pop();
  27.  
    }
  28.  
    }
  29.  
    }
  30.  
     
  31.  
    if (stack.empty())
  32.  
    return true;
  33.  
    return false;
  34.  
    }
  35.  
    public static void main(String[] args) {
  36.  
    String expression = "{((1+3)+2+4)+9*7}";
  37.  
    ExpStackMatching oj = new ExpStackMatching();
  38.  
    boolean flag = oj.matching(expression);
  39.  
    if (flag) {
  40.  
    System.out.println("匹配成功!");
  41.  
    } else {
  42.  
    System.out.println(" 匹配失败 ");
  43.  
    }
  44.  
    }
  45.  
    }
 

五、栈的字节码解析

六、我们使用过的栈

  1. Struts2的ValueStack(值栈)
  2. 使用栈实现浏览器的前进和后退

七、栈的总结

栈是一个概念上的工具,具体能实现什么功能可以由我们去想象。栈通过提供限制性的访问方法push()和pop(),使得程序不容易出错。

对于栈的实现,我们稍微分析就知道,数据入栈和出栈的时间复杂度都为O(1),也就是说栈操作所耗的时间不依赖栈中数据项的个数,因此操作时间很短。而且需要注意的是栈不需要比较和移动操作,我们不要画蛇添足。


我的微信公众号:架构真经(id:gentoo666),分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。每日更新哦!

参考文章

  1. https://blog.csdn.net/weixin_41362649/article/details/81660908
  2. https://www.cnblogs.com/ysocean/p/7911910.html
  3. https://www.cnblogs.com/rrttp/p/7913091.html

Guess you like

Origin www.cnblogs.com/anymk/p/11470487.html