zoukankan      html  css  js  c++  java
  • 实现一个简单的栈(底层数组)

    栈的特点

    1. 先进后出(FILO)或者  后进先出(LIFO)
    2. 增删元素皆是在栈顶操作
    3. 一次只能删除一个数据项:当前栈顶元素
    4. 只允许访问一个数据项:当前栈顶元素

    所需元素

    1. 因为底层用数组实现,所以需要一个数组 stackArray
    2. 需要一个指向栈顶的指针top
    3. 需要指定数组的大小maxSize

    分析实现

    1. 需要在创建自定义栈类的时候,就确定好一些初始化操作,例如确定数组的大小并初始化数组;
    2. 确定栈具有的功能:入栈push()、出栈pop()、查看栈顶元素getTop()、判空isEmpty()、判满isFull()、判长length()、清空clear()

    代码实现

     1 public class Stack {
     2 
     3     private int maxSize;
     4     private int top;
     5     private Object[] stackArr;
     6 
     7     /**
     8      * 利用构造函数初始化数组
     9      *
    10      * @param maxSize
    11      */
    12     public Stack(int maxSize) {
    13         this.maxSize = maxSize;
    14         stackArr = new Object[maxSize];
    15         // 相当于栈的指针
    16         top = 0;
    17     }
    18 
    19     /**
    20      * 元素出栈
    21      *
    22      * @param i
    23      */
    24     public void push(Object i) {
    25         if (isFull()) {
    26             throw new RuntimeException("栈已满!");
    27         }
    28         stackArr[top++] = i;
    29     }
    30 
    31     /**
    32      * 元素入栈
    33      *
    34      * @return
    35      */
    36     public Object pop() {
    37         if(isEmpty()) {
    38             throw new RuntimeException("空栈!");
    39         }
    40         // 这里先自减是因为top数组长度,而索引从0开始
    41         return stackArr[--top];
    42     }
    43 
    44     /**
    45      * 获取栈顶元素,只是查看,不删除
    46      *
    47      * @return
    48      */
    49     public Object getTop() {
    50         return stackArr[top - 1];
    51     }
    52 
    53     /**
    54      * 判断栈是否为空
    55      *
    56      * @return
    57      */
    58     public boolean isEmpty() {
    59         return (top == 0);
    60     }
    61 
    62     /**
    63      * 判断栈是否已满
    64      *
    65      * @return
    66      */
    67     public boolean isFull() {
    68         return (top == maxSize);
    69     }
    70 
    71     /**
    72      * 回去栈元素的数量
    73      *
    74      * @return
    75      */
    76     public int length() {
    77         return top;
    78     }
    79 
    80     /**
    81      * 清空栈
    82      *
    83      * @return
    84      */
    85     public void clear() {
    86         while (top != 0) {
    87             pop();
    88         }
    89     }
    90 }

    测试

     1 public class StackTest {
     2 
     3     public static void main(String[] args) {
     4 
     5         Stack stack = new Stack(10);
     6 
     7         System.out.println( "栈是否为空? " + stack.isEmpty());
     8 
     9         stack.push(2);
    10         stack.push(1);
    11         stack.push(6);
    12         stack.push(3);
    13         stack.push(5);
    14 
    15         System.out.println("栈长: " + stack.length());
    16 
    17         System.out.println("栈顶元素: " + stack.getTop());
    18 
    19         System.out.println("栈满? " + stack.isFull());
    20 
    21         // 清空栈
    22         stack.clear();
    23 
    24         System.out.println( "栈是否为空? " + stack.isEmpty());
    25 
    26         stack.push(2);
    27         stack.push(1);
    28         stack.push(6);
    29         stack.push(3);
    30         stack.push(5);
    31         // 取出栈元素,并打印
    32         while(!stack.isEmpty()){
    33             Object pop = stack.pop();
    34             System.out.print(pop + "	");
    35         }
    36         System.out.println();
    37     }
    38 }

    结果

    栈是否为空? true
    栈长: 5
    栈顶元素: 5
    栈满? false
    栈是否为空? true
    5    3    6    1    2    

     

    总结

    1. 底层用数组实现,简单,但是一开始就要固定栈的大小,并且后期扩容困难;
    2. 插入、查找、删除所需时间都是O(1),因为都是对栈顶元素操作。

    对比链接:使用链表实现栈


  • 相关阅读:
    Go 语言机制之逃逸分析
    类型转换和类型断言
    浅析rune数据类型
    Go 文件操作(创建、打开、读、写)
    字符编码笔记:ASCII,Unicode 和 UTF-8
    cmd.exe启动参数详解
    linux下.so、.ko、.a的区别
    Python 和C#的交互
    Innodb表压缩过程中遇到的坑(innodb_file_format)
    更改mysql的加密方式和密码策略
  • 原文地址:https://www.cnblogs.com/shadowdoor/p/9234242.html
Copyright © 2011-2022 走看看