当前位置:   article > 正文

java实现栈(分别基于数组和基于链表)_此栈

此栈
基于数组如下:
  1. package com.ma.stack;
  2. /**
  3. * @author xiaoma
  4. *此栈实现基于数组,初始栈时已经设置栈大小
  5. */
  6. public class MyArrayStack {
  7. private int size;
  8. private int top = -1;
  9. private int[] arr;
  10. MyArrayStack(){
  11. arr =new int[3]; //默认栈大小为3
  12. }
  13. MyArrayStack(int size) {
  14. if(size > 0){
  15. this.size = size;
  16. arr = new int[size];
  17. }
  18. else throw new RuntimeException("数组长度值不正确");
  19. }
  20. //==============================================================================
  21. //入栈
  22. public void push(int data){
  23. if(top >= size-1){
  24. System.out.println("栈已满");
  25. return;
  26. }
  27. arr[++top] = data;
  28. }
  29. //==============================================================================
  30. //出栈
  31. public int pop(){
  32. if(top < 0){
  33. throw new RuntimeException("栈为空");
  34. }
  35. else{
  36. int temp = arr[top];
  37. arr[top] = 0;
  38. top--;
  39. return temp;
  40. }
  41. }
  42. //查看栈顶元素但不出栈
  43. public int peek(){
  44. if(top < 0){
  45. throw new RuntimeException("栈为空");
  46. }
  47. else{
  48. return arr[top];
  49. }
  50. }
  51. //==============================================================================
  52. public boolean isEmpty(){
  53. return top == -1;
  54. }
  55. //==============================================================================
  56. public static void main(String[] args) {
  57. MyArrayStack stack = new MyArrayStack(3);//初始栈大小为3
  58. stack.push(2);
  59. stack.push(5);
  60. stack.push(10);
  61. //stack.push(11);
  62. System.out.println(stack.pop());
  63. System.out.println(stack.pop());
  64. System.out.println(stack.pop());
  65. //System.out.println(stack.pop());
  66. // System.out.println(stack.peek());
  67. // System.out.println(stack.peek());
  68. System.out.println(stack.isEmpty());
  69. }
  70. }

基于链表如下:

  1. package com.ma.stack;
  2. /**
  3. * @author xiaoma
  4. * 此栈实现基于链表,初始栈时未设置栈大小(栈大小可变)
  5. *此栈是将新元素插到链表头部(链表头部是栈顶)
  6. */
  7. public class MyLinkStack {
  8. private Node top;//记录栈顶元素
  9. private int size = 0;//记录栈大小
  10. public MyLinkStack() {
  11. top = null;
  12. }
  13. class Node{
  14. public int data; //记录数据
  15. public Node next;
  16. Node(int data){
  17. this.data = data;
  18. }
  19. }
  20. //=============================================================================================
  21. //入栈
  22. public void push(int data){
  23. Node newNode = new Node(data);
  24. if(top == null){
  25. top =newNode;
  26. }
  27. else{
  28. newNode.next = top;//将新元素插到链表头部
  29. top = newNode;
  30. }
  31. size++;
  32. }
  33. //=============================================================================================
  34. //出栈
  35. public Integer pop(){
  36. if(top == null) throw new RuntimeException("栈为空");
  37. else{
  38. int val = top.data;
  39. top = top.next;
  40. size--;
  41. return val;
  42. }
  43. }
  44. //查看栈顶元素但不出栈
  45. public Integer peek(){
  46. if(top == null) throw new RuntimeException("栈为空");
  47. else{
  48. return top.data;
  49. }
  50. }
  51. //=============================================================================================
  52. public boolean isEmpty(){
  53. return top == null;
  54. }
  55. public int getSize(){
  56. return size;
  57. }
  58. //=============================================================================================
  59. public static void main(String[] args) {
  60. MyLinkStack my = new MyLinkStack();
  61. my.push(4);
  62. my.push(5);
  63. my.push(6);
  64. System.out.println("当前size:"+my.getSize());
  65. System.out.println(my.isEmpty());
  66. System.out.println(my.pop());
  67. System.out.println("当前size:"+my.getSize());
  68. System.out.println(my.pop());
  69. System.out.println(my.pop());
  70. System.out.println(my.isEmpty());
  71. //System.out.println(my.pop());
  72. }
  73. }


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

闽ICP备14008679号