当前位置:   article > 正文

Java中栈的实现(数组、链表)

Java中栈的实现(数组、链表)

定义

  1. 栈是一个先入后出(FILO-First In Last Out)的有序列表
  2. 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。
  3. 允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
  4. 根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,
  5. 最后放入的元素最先删除,最先放入的元素最后删除

入栈

出栈

应用场景

  • 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中; (函数栈)
  • 处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数、区域变量等数据存入堆栈中;(函数栈)
  • 表达式的转换[中缀表达式转后缀表达式]与求值(实际解决);
  • 二叉树的遍历
  • 图形的深度优先(depth一first)搜索法

实现

实现方式包括:

  • 数组:ArrayStack
  • 链表
    • 头插法:LinkedListStackHead (推荐) 
    • 尾插法:LinkedListStackTail

数组

  1. class ArrayStack {
  2. private int maxSize; //栈大小
  3. private int[] stack; //数组模拟栈
  4. private int top = -1; //栈顶
  5. public ArrayStack(int maxSize) {
  6. this.maxSize = maxSize;
  7. stack = new int[this.maxSize];
  8. }
  9. // 栈满
  10. public boolean isFull() {
  11. return top == maxSize - 1;
  12. }
  13. // 栈空
  14. public boolean isEmpty() {
  15. return top == -1;
  16. }
  17. // 入栈
  18. public void push(int value) {
  19. if (isFull()) {
  20. System.out.println("栈满");
  21. return;
  22. }
  23. top++;
  24. stack[top] = value;
  25. }
  26. // 出栈
  27. public int pop() {
  28. if (isEmpty()) {
  29. throw new RuntimeException("栈空");
  30. }
  31. /* int value = stack[top];
  32. top--;
  33. return value;*/
  34. return stack[top--];
  35. }
  36. // 遍历
  37. public void list() {
  38. if (isEmpty()) {
  39. throw new RuntimeException("栈空");
  40. }
  41. for (int i = top; i >= 0; i--) {
  42. System.out.printf("stack[%d]=%d\n", i, stack[i]);
  43. }
  44. }
  45. }

链表

定义节点

  1. //定义链表节点
  2. class Node {
  3. public int val;
  4. public Node next;
  5. public Node(int val) {
  6. this.val = val;
  7. this.next = null;
  8. }
  9. }

头插法

  1. class LinkedListStackHead {
  2. private int maxSize;
  3. private Node head;
  4. public LinkedListStackHead(int maxSize) {
  5. this.maxSize = maxSize;
  6. this.head = null;
  7. }
  8. // 栈满
  9. public boolean isFull() {
  10. boolean flag = false;
  11. int size = 0;
  12. Node temp = head;
  13. while (true) {
  14. if (temp == null) {
  15. break;
  16. }
  17. size++;
  18. temp = temp.next;
  19. }
  20. if (size == maxSize) {
  21. flag = true;
  22. }
  23. return flag;
  24. }
  25. // 栈空
  26. public boolean isEmpty() {
  27. return head == null;
  28. }
  29. // 入栈
  30. public void push(int value) {
  31. if (isFull()) {
  32. System.out.println("栈满");
  33. return;
  34. }
  35. Node node = new Node(value);
  36. node.next = head;
  37. head = node;
  38. }
  39. // 出栈
  40. public int pop() {
  41. if (isEmpty()) {
  42. throw new RuntimeException("栈空");
  43. }
  44. int res = head.val;
  45. head = head.next;
  46. return res;
  47. }
  48. // 遍历
  49. public void list() {
  50. if (isEmpty()) {
  51. System.out.println("栈空");
  52. return;
  53. }
  54. Node temp = head;
  55. while (temp != null) {
  56. System.out.println(temp.val);
  57. temp = temp.next;
  58. }
  59. }
  60. }

尾插法

  1. class LinkedListStackTail {
  2. private int maxSize;
  3. private Node bottom = new Node(0); //栈底位置,固定
  4. public LinkedListStackTail(int maxSize) {
  5. this.maxSize = maxSize;
  6. }
  7. // 栈满
  8. public boolean isFull() {
  9. boolean flag = false;
  10. int size = 0;
  11. Node temp = bottom.next;
  12. while (true) {
  13. if (temp == null) {
  14. break;
  15. }
  16. size++;
  17. temp = temp.next;
  18. }
  19. if (size == maxSize) {
  20. flag = true;
  21. }
  22. return flag;
  23. }
  24. // 栈空
  25. public boolean isEmpty() {
  26. return bottom.next == null;
  27. }
  28. // 入栈
  29. public void push(int value) {
  30. if (isFull()) {
  31. System.out.println("栈满");
  32. return;
  33. }
  34. Node newNode = new Node(value);
  35. Node temp = bottom;
  36. while (true) {
  37. if (temp.next == null) {
  38. break;
  39. }
  40. temp = temp.next;
  41. }
  42. temp.next = newNode;
  43. }
  44. // 出栈
  45. public int pop() {
  46. if (isEmpty()) {
  47. throw new RuntimeException("栈空");
  48. }
  49. Node temp = bottom;
  50. int res = 0;
  51. while (temp != null) {
  52. if (temp.next.next == null) {
  53. res = temp.next.val;
  54. temp.next = null;
  55. break;
  56. }
  57. temp = temp.next;
  58. }
  59. return res;
  60. }
  61. // 遍历-逆序---偷懒操作(利用了java中的stack),也可自行实现
  62. public void list() {
  63. if (isEmpty()){
  64. throw new RuntimeException("栈空");
  65. }
  66. Stack<Integer> stack = new Stack<>();
  67. Node temp = bottom.next;
  68. while (temp != null) {
  69. stack.push(temp.val);
  70. temp = temp.next;
  71. }
  72. while (stack.size() > 0) {
  73. System.out.println(stack.pop());
  74. }
  75. }
  76. }

 测试

  1. package com.frank.stack;
  2. import java.util.Scanner;
  3. import java.util.Stack;
  4. public class LinkedStackDemo {
  5. public static void main(String[] args) {
  6. // ArrayStack stack = new ArrayStack(4);
  7. // LinkedListStackHead stack = new LinkedListStackHead(4);
  8. LinkedListStackTail stack=new LinkedListStackTail(4);
  9. String key = "";
  10. boolean loop = true;
  11. Scanner scanner = new Scanner(System.in);
  12. while (loop) {
  13. System.out.println("show: 显示");
  14. System.out.println("exit: 退出");
  15. System.out.println("push: 入栈");
  16. System.out.println("pop: 出栈");
  17. key = scanner.next();
  18. switch (key) {
  19. case "show":
  20. try {
  21. stack.list();
  22. } catch (Exception e) {
  23. System.out.println(e.getMessage());
  24. }
  25. break;
  26. case "push":
  27. System.out.println("输入数:");
  28. int value = scanner.nextInt();
  29. stack.push(value);
  30. break;
  31. case "pop":
  32. try {
  33. int res = stack.pop();
  34. System.out.printf("出栈数据%d\n", res);
  35. } catch (Exception e) {
  36. System.out.println(e.getMessage());
  37. }
  38. break;
  39. case "exit":
  40. scanner.close();
  41. loop = false;
  42. break;
  43. default:
  44. break;
  45. }
  46. }
  47. System.out.println("程序退出");
  48. }
  49. }

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

闽ICP备14008679号