顺序栈:

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.mycompany.data.structure.line.stack;
  7. /**
  8. *
  9. * @author wyman
  10. */
  11. public class SequenceStack {
  12. public int base, top = 0;
  13. public int stacksiz = 0;
  14. public int STACK_INIT_SIZE = 100;
  15. public int STACKINCREMENT = 10;
  16. public Object[] stackArr;
  17. public SequenceStack(int stack_init_size, int stackincrement) {
  18. this.STACKINCREMENT = stackincrement;
  19. this.STACK_INIT_SIZE = stack_init_size;
  20. stackArr = new Object[STACK_INIT_SIZE];
  21. stacksiz = STACK_INIT_SIZE;
  22. }
  23. public SequenceStack() {
  24. stackArr = new Object[STACK_INIT_SIZE];
  25. stacksiz = STACK_INIT_SIZE;
  26. }
  27. public SequenceStack InitStack(int... size) {
  28. if (size.length == 2) {
  29. return new SequenceStack(size[0], size[1]);
  30. } else if (size == null) {
  31. return new SequenceStack();
  32. } else {
  33. return null;
  34. }
  35. }
  36. public void destoryStack() {
  37. }
  38. public void ClearStack() {
  39. this.top = this.base;
  40. }
  41. public boolean StackEmpty() {
  42. if (this.top == this.base) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. public int StackLength() {
  48. return this.top;
  49. }
  50. public Object getTop() {
  51. return stackArr[this.top];
  52. }
  53. public void push(Object obj) {
  54. if (this.top == this.stacksiz) {
  55. Object[] newArr = new Object[this.stacksiz + this.STACKINCREMENT];
  56. System.arraycopy(stackArr, 0, newArr, 0, stacksiz);
  57. this.stacksiz += this.STACKINCREMENT;
  58. stackArr = newArr;
  59. }
  60. stackArr[this.top] = obj;
  61. this.top++;
  62. }
  63. public Object pop() {
  64. if (this.top == this.base) {
  65. return null;
  66. } else {
  67. this.top--;
  68. return this.stackArr[this.top];
  69. }
  70. }
  71. public void stackTraverse() {
  72. }
  73. }

链栈:

  1. /**
  2. *
  3. * @author wyman
  4. */
  5. public class StackElem {
  6. public Object data;
  7. public StackElem next;
  8. public StackElem(Object data, StackElem next) {
  9. this.data = data;
  10. this.next = next;
  11. }
  12. public StackElem() {
  13. }
  14. }
  15. ===========================================
  16. /**
  17. *
  18. * @author wyman
  19. */
  20. public class LinkStack {
  21. public int stacksiz = 0;
  22. public StackElem top;
  23. public StackElem base;
  24. public LinkStack() {
  25. this.top = new StackElem();
  26. this.base = this.top;
  27. }
  28. public LinkStack InitStack() {
  29. return new LinkStack();
  30. }
  31. public void destoryStack() {
  32. this.top = null;
  33. }
  34. public void ClearStack() {
  35. this.top = this.base;
  36. }
  37. public boolean StackEmpty() {
  38. if (this.top == this.base) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. public int StackLength() {
  44. return stacksiz;
  45. }
  46. public Object getTop() {
  47. if (this.stacksiz > 0) {
  48. return this.top;
  49. }
  50. return null;
  51. }
  52. public void push(Object obj) {
  53. this.top.data=obj;
  54. StackElem se = new StackElem();
  55. se.next=this.top;
  56. this.top = se;
  57. stacksiz++;
  58. }
  59. public Object pop() {
  60. if (this.stacksiz <= 0) {
  61. return null;
  62. } else {
  63. this.top = this.top.next;
  64. stacksiz--;
  65. return this.top;
  66. }
  67. }
  68. public void stackTraverse() {
  69. }
  70. }