当前位置:   article > 正文

栈的实现(分别用数组和链表的java实现)_使用数组和链表实现堆栈 java

使用数组和链表实现堆栈 java


  1. /**
  2. * 文件名:StackText.java
  3. * 时间:2014年10月21日下午8:43:02
  4. * 作者:修维康
  5. */
  6. package chapter3;
  7. /**
  8. * 类名:StackText 说明:用链表和数组分别实现栈;
  9. */
  10. class ArrayStack<AnyType> {
  11. public static final int DEFAULT_CAPACITY = 10;
  12. private AnyType[] theArray;
  13. private int topOfStack;
  14. private int size;
  15. public ArrayStack() {
  16. clear();
  17. topOfStack = -1;
  18. }
  19. public void push(AnyType x) {
  20. if (size == theArray.length)
  21. ensureCapacity(2 * size + 1);
  22. theArray[++topOfStack] = x;
  23. size++;
  24. }
  25. public AnyType top() {
  26. if (topOfStack != -1)
  27. return theArray[topOfStack];
  28. return null;
  29. }
  30. public AnyType pop() {
  31. if (topOfStack != -1)
  32. size--;
  33. return theArray[topOfStack--];
  34. }
  35. public void clear() {
  36. topOfStack = -1;
  37. ensureCapacity(DEFAULT_CAPACITY);
  38. }
  39. public boolean isEmpty() {
  40. return topOfStack == -1;
  41. }
  42. public void ensureCapacity(int newCapacity) {
  43. if (newCapacity < size)
  44. return;
  45. AnyType[] old = theArray;
  46. theArray = (AnyType[]) new Object[newCapacity];
  47. for (int i = 0; i < size; i++)
  48. theArray[i] = old[i];
  49. }
  50. }
  51. /**
  52. * 类名:LinkedStack 说明:链表实现栈
  53. */
  54. class LinkedStack<AnyType> {
  55. private static class Node<AnyType> {
  56. Node(AnyType data, Node<AnyType> prev) {
  57. this.data = data;
  58. this.prev = prev;
  59. }
  60. private AnyType data;
  61. private Node<AnyType> prev;
  62. }
  63. private Node<AnyType> bottom;
  64. public LinkedStack() {
  65. clear();
  66. }
  67. public void clear() {
  68. bottom = new Node<AnyType>(null, null);
  69. }
  70. public void push(AnyType x) {
  71. bottom = new Node<AnyType>(x, bottom);
  72. }
  73. public AnyType top() {
  74. if (bottom != null)
  75. return bottom.data;
  76. return null;
  77. }
  78. public AnyType pop() {
  79. if (bottom != null) {
  80. bottom = bottom.prev;
  81. return bottom.data;
  82. }
  83. return null;
  84. }
  85. public boolean isEmpty() {
  86. return bottom.prev == null;
  87. }
  88. }
  89. public class StackTest {
  90. /**
  91. * 方法名:StackText.java 说明:测试
  92. */
  93. public static void main(String[] args) {
  94. // TODO Auto-generated method stub
  95. ArrayStack stack1 = new ArrayStack();
  96. for (int i = 1; i < 100; i++)
  97. stack1.push(i);
  98. // stack1.clear();
  99. System.out.println(stack1.top());
  100. while (!stack1.isEmpty())
  101. System.out.println(stack1.pop());
  102. ArrayStack stack2 = new ArrayStack();
  103. stack2.push(1);
  104. stack2.push(2);
  105. stack2.push(3);
  106. stack2.push(4);
  107. // stack2.clear();
  108. System.out.println(stack2.top());
  109. while (!stack2.isEmpty())
  110. System.out.println(stack2.pop());
  111. }
  112. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/581691
推荐阅读
相关标签
  

闽ICP备14008679号