• 链表完成栈模拟和栈模拟计数器原理


    1. package shuJuJieGouYuSuanFa.stack;
    2. /**
    3. * @ClassName ListStackDom
    4. * @Author 瞿肖
    5. * @Date 2022/7/30 19:02
    6. */
    7. public class ListStackDom {
    8. public static void main(String[] args) {
    9. ListStackUtil a = new ListStackUtil();
    10. ListStack l1 = new ListStack(1);
    11. ListStack l2 = new ListStack(2);
    12. ListStack l3 = new ListStack(3);
    13. a.push(l1);
    14. a.push(l2);
    15. a.push(l3);
    16. a.pop();
    17. a.pop();
    18. a.pop();
    19. a.pop();
    20. }
    21. }
    22. class ListStackUtil {
    23. public ListStack fist = new ListStack(-1);
    24. public boolean isEmpty() {
    25. return fist.next == null;
    26. }
    27. public void push(ListStack data) {
    28. ListStack t = fist;
    29. while (true) {
    30. if (t.next == null) {
    31. break;
    32. }
    33. t = t.next;
    34. }
    35. t.next = data;
    36. data.per = t;
    37. }
    38. public void pop() {
    39. if (isEmpty()) {
    40. System.out.println("为空!");
    41. return;
    42. }
    43. ListStack t = fist.next;
    44. while (true) {
    45. if (t.next == null) {
    46. break;
    47. }
    48. t = t.next;
    49. }
    50. System.out.println(t);
    51. t.per.next = t.next;
    52. if (t.next != null) {
    53. t.next.per = t.per;
    54. }
    55. }
    56. }
    57. class ListStack {
    58. public ListStack(int value) {
    59. this.value = value;
    60. }
    61. public int value;
    62. @Override
    63. public String toString() {
    64. return ""+value;
    65. }
    66. public ListStack next;
    67. public ListStack per;
    68. }

    和双向链表相同,注意最后一个取出的时候,不用将后一个的前指针指向本元素的前方

    计数器模拟:

    1. package shuJuJieGouYuSuanFa.stack;
    2. /**
    3. * @ClassName jsq
    4. * @Author 瞿肖
    5. * @Date 2022/7/30 20:01
    6. */
    7. public class jsq {
    8. public static void main(String[] args) {
    9. jsqStack dataStack = new jsqStack(10);
    10. jsqStack UtilStack = new jsqStack(10);
    11. String s = "32+2*2-4";
    12. int indx = 0;
    13. String date = "";
    14. int fh = 0;
    15. int num1 = 0;
    16. int num2 = 0;
    17. int res = 0;
    18. char c = ' ';
    19. while (true) {
    20. c = s.charAt(indx);
    21. if (dataStack.isfh(c)) {//判断数值或运算符
    22. if (!UtilStack.isEmpty()) {//运算符栈不为空
    23. if (UtilStack.priority(c) <= UtilStack.priority(UtilStack.stak())) {//优先级
    24. num1 = dataStack.pop();
    25. num2 = dataStack.pop();
    26. fh = UtilStack.pop();
    27. res = dataStack.cal(num1, num2, fh);
    28. dataStack.push(res);
    29. UtilStack.push(c);
    30. } else {//优先级大于就入栈
    31. UtilStack.push(c);
    32. }
    33. } else {//运算符栈为空
    34. UtilStack.push(c);
    35. }
    36. } else {//数值
    37. date += c;
    38. if (indx <= s.length() - 2) {//是否到了最后一位,最后一位固定为数值
    39. if (dataStack.isfh(s.charAt(indx + 1))) {//往后查看一位,是否为数值
    40. dataStack.push(Integer.parseInt(date));
    41. date = "";
    42. }
    43. } else {
    44. dataStack.push(Integer.parseInt(date));
    45. }
    46. }
    47. indx++;
    48. if (indx >= s.length()) {
    49. break;
    50. }
    51. }
    52. while (true) {
    53. if (UtilStack.isEmpty()) {
    54. break;
    55. }
    56. num1 = dataStack.pop();
    57. num2 = dataStack.pop();
    58. fh = UtilStack.pop();
    59. res = dataStack.cal(num1, num2, fh);
    60. dataStack.push(res);
    61. }
    62. System.out.println(s + "=" + dataStack.pop());
    63. }
    64. }
    65. class jsqStack {
    66. public int maxSize;
    67. public int[] arr;
    68. public int top = -1;
    69. public jsqStack(int maxSize) {
    70. this.maxSize = maxSize;
    71. arr = new int[maxSize];
    72. }
    73. public boolean isEmpty() {
    74. return top == -1;
    75. }
    76. public boolean isFull() {
    77. return top == maxSize - 1;
    78. }
    79. public void push(int value) {
    80. if (isFull()) {
    81. System.out.println("已满");
    82. return;
    83. }
    84. top++;
    85. arr[top] = value;
    86. }
    87. public int pop() {
    88. if (isEmpty()) {
    89. System.out.println("为空");
    90. return -1;
    91. }
    92. int t = arr[top];
    93. arr[top] = 0;
    94. top--;
    95. return t;
    96. }
    97. public boolean isfh(int fh) {
    98. return fh == '+' || fh == '-' || fh == '*' || fh == '/';
    99. }
    100. public int priority(int fh) {
    101. if (fh == '*' || fh == '/') {
    102. return 1;
    103. } else {
    104. return 0;
    105. }
    106. }
    107. public int stak() {
    108. return arr[top];
    109. }
    110. public int cal(int a, int b, int fh) {
    111. int res = 0;
    112. switch (fh) {
    113. case '+':
    114. res = a + b;
    115. break;
    116. case '-':
    117. res = b - a;
    118. break;
    119. case '*':
    120. res = a * b;
    121. break;
    122. case '/':
    123. res = b / a;
    124. break;
    125. }
    126. return res;
    127. }
    128. }

    把每个数据取出,

    判断运算符或数字

    为运算符时判断运算符栈是否为空,是就直接入栈

    否则判断运算符栈头优先级是否大于等于本次运算符,大于就将数字栈出栈两个,运算符栈出栈一个,进行运算,结果入数字栈,

    遍历到最后一位后,退出循环,进入计算循环:

    进行数字栈出栈两个,运算符栈出栈一个进行运算,结果入数字栈,直到数字栈剩一个时,就是答案吧

  • 相关阅读:
    如何快速掌握DDT数据驱动测试?
    mysql获取重复数据最新一条,并显示重复数量
    Qt 事件处理机制简介
    Linux权限
    Maven环境搭建
    关于Modal的封装的记录【Vue3、computed、Naive UI】
    Wt库的C++下载器程序
    记住这份软件测试八股文还怕不能拿offer?你值得拥有
    数据库之MySQL查询去重数据
    【节能学院】智能操控装置在高压开关柜的应用
  • 原文地址:https://blog.csdn.net/qx020814/article/details/126077633