栈简介:

栈(stack)是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。对于栈结构存储的对象,都遵循“先进后出,后进先出”的原则。
原型类似于像一个圆桶里放与桶半径稍小一点的圆饼一样,先放入(入栈)的圆饼必然里桶底(栈底)较近,取出时必须将从桶面(栈顶)的圆饼先取出(出栈)才能拿到其他圆饼

栈示意图

栈.jpg

基于JAVA的String数组模拟栈结构的操作

  1. package stack;
  2. /**
  3. * @ClassName StackForArray
  4. * @Description String数组模拟栈,伪删除效果
  5. * @Author Vision
  6. * @Date 2019/7/1821:19
  7. * @Version 1.0.0
  8. */
  9. public class StackForArray {
  10. public static void main(String[] args) {
  11. Stack stack = new Stack(10);
  12. int length = stack.getMaxLength();
  13. System.out.printf("stack's maxLength:{%d} \n", stack.getMaxLength());
  14. System.out.printf("stack's top:{%d}\n", stack.getTop());
  15. System.out.println("入栈顺序");
  16. for (int i = 0; i < length; i++) {
  17. String str = "栈元素a" + i;
  18. stack.push(str);
  19. System.out.println(str);
  20. }
  21. System.out.printf("stack's new top:{%d}\n", stack.getTop());
  22. System.out.println("出栈顺序");
  23. for (int i = 0; i < length; i++) {
  24. System.out.println("pop = [" + stack.pop() + "]");
  25. }
  26. System.out.println("stack's maxLength after pop() = [" + stack.getMaxLength() + "]");
  27. System.out.println("stack's top after pop() = [" + stack.getTop() + "]");
  28. }
  29. }
  30. /**
  31. * 栈对象
  32. */
  33. class Stack {
  34. /**
  35. * 数组
  36. */
  37. private String[] str;
  38. /**
  39. * 栈顶指针
  40. */
  41. private int top;
  42. /**
  43. * 栈空间
  44. */
  45. private int maxLength;
  46. public Stack() {
  47. this.top = -1;
  48. }
  49. public Stack(int num) {
  50. if (num > 0) {
  51. this.str = new String[num];
  52. this.top = -1;
  53. this.maxLength = num;
  54. } else {
  55. throw new RuntimeException("栈大小不能为0");
  56. }
  57. }
  58. /**
  59. * 入栈
  60. *
  61. * @param str
  62. */
  63. public void push(String str) {
  64. if (isEmpty()) {
  65. throw new RuntimeException("栈为空");
  66. } else if (isFull()) {
  67. throw new RuntimeException("满栈");
  68. } else {
  69. this.str[++top] = str;
  70. }
  71. }
  72. /**
  73. * 出栈
  74. *
  75. * @return
  76. */
  77. public String pop() {
  78. if (isEmpty()) {
  79. throw new RuntimeException("栈为空");
  80. } else {
  81. String str = this.str[top--];
  82. /* 伪删除 */
  83. this.str[top + 1] = null;
  84. this.maxLength--;
  85. return str;
  86. }
  87. }
  88. /**
  89. * 判断是否为空栈
  90. *
  91. * @return
  92. */
  93. public boolean isEmpty() {
  94. return this.maxLength == 0;
  95. }
  96. /**
  97. * 判断是否满栈
  98. *
  99. * @return
  100. */
  101. public boolean isFull() {
  102. return this.top == this.maxLength - 1;
  103. }
  104. public String[] getStr() {
  105. return str;
  106. }
  107. public int getTop() {
  108. return top;
  109. }
  110. public void setStr(String[] str) {
  111. this.str = str;
  112. this.maxLength = str.length;
  113. }
  114. public void setTop(int top) {
  115. this.top = top;
  116. }
  117. public int getMaxLength() {
  118. return maxLength;
  119. }
  120. public void setMaxLength(int maxLength) {
  121. this.maxLength = maxLength;
  122. }
  123. }

以上代码有缺陷,出栈时原数组的真实大小依旧没有改变,只是为NULL值,仅作为对栈类型理解的一种方法 且上述代码仅为基于数组模型的顺序栈 链式栈需使用到链表结构

运行结果

image.png

线性表和非线性表

线性表,就是数据排成像一条直线一样的结构,数组,链表,队列,栈都是线性结构,而非线性表就是二叉树,堆,图,哈希表等,数据之间不是简单的先后关系。