zoukankan      html  css  js  c++  java
  • 栈(Stack)

    栈(Stack)

    栈(Stack)是一种后进先出的数据结构(LIFO:last in first out),只允许访问栈中的第一个数据项:即最后插入的数据项。移除这个数据项之后,才能看到第二个数据项,以此类推。

    往栈中存入数据称之为压栈(push),移除数据称之为弹栈(pop),此外通常还提供查看栈顶元素的peek方法,此方法可以可以栈顶元素的值,但是并不会将其移除

     java.util.Stack就是JDK提供的一种对栈的实现,这个实现是基于数组的,由于栈非常简单,我们没有必须要分析源码,直接按照以下方法提供一个相同的自己的实现,此外,我们也可以基于链表来实现一个栈

    基于数组的栈的实现

      关于基于数组的栈的实现,只有一点值得注意的地方,其是LIFO,但是我们在使用数组的时候,并不需要每次都将元素插入数组的第一个位置,然后将之前的所有元素后移一个位置,只要用一个变量记住最后一个添加的元素的位置即可,当弹栈时,直接返回这个位置上的数字即可

    1. public class SimpleArrayStack<T> {
    2.     private Object[] array = null;
    3.     private int size = 0;
    4.     private static final int DEFAULR_INITIAL_SIZE = 10;
    5.     private int capacity = 0;
    6.  
    7.     public SimpleArrayStack() {
    8.         this(DEFAULR_INITIAL_SIZE );
    9.     }
    10.  
    11.     public SimpleArrayStack(int initial_size) {
    12.         super();
    13.         this.capacity = initial_size;
    14.         array = new Object[initial_size];
    15.     }
    16.  
    17.     @SuppressWarnings("unchecked" )
    18.     public T pop() {
    19.         if (size < 1) {
    20.             throw new IllegalStateException("no more elements");
    21.         }
    22.         T result = (T) array [--size];
    23.         array[size ] = null;
    24.         return result ;
    25.  
    26.     }
    27.  
    28.     @SuppressWarnings("unchecked" )
    29.     public T peek() {
    30.         if (size < 1) {
    31.             throw new IllegalStateException("no more elements");
    32.         }
    33.         return (T) array [size - 1];
    34.     }
    35.  
    36.     public void push(T e) {
    37.         int index = size++;
    38.         if (index > capacity) { // 扩容
    39.             int new_capacity = capacity + capacity >> 1;
    40.             if (new_capacity <= 0) {
    41.                 new_capacity = Integer.MAX_VALUE;
    42.             }
    43.             array = Arrays.copyOf( array, new_capacity );
    44.         }
    45.  
    46.         array[index ] = e;
    47.     }
    48.  
    49.     public int getSize() {
    50.         return size ;
    51.     }
    52. }

    测试代码

    1. public class SimpleArrayStackTest {
    2.     public static void main(String[] args) {
    3.         SimpleArrayStack<Integer> simpleStack=new SimpleArrayStack<Integer>();
    4.         System. out.print("push: " );
    5.         for (int i = 0; i < 10; i++) {
    6.             simpleStack.push();
    7.             System. out.print(+" " );
    8.         }
    9.         System. out.print(" pop: " );
    10.         while (simpleStack.getSize()>0){
    11.             System. out.print(simpleStack .pop()+" ");
    12.         }
    13.     }
    14. }

    运行程序输出

    push: 0 1 2 3 4 5 6 7 8 9

    pop: 9 8 7 6 5 4 3 2 1 0

    可以看到数据都是按照和插入顺序相反的方式弹出来了

    基于链表的栈的实现

    基于链表的Stack尤为简单,我们可以使用上一节编写SingleLinkList来实现,实际上就是一种委派,并把我们不想提供的方法给屏蔽,这可能没有什么技术含量,但是读者意识到了,一个数据结构可以在另外一个数据结构的基础上编写

    1. public class SimpleLinkedListStack <T>{
    2.     private SingleLinkList<T> singleLinkList =new SingleLinkList<T>();
    3.     public void push(T t){
    4.         singleLinkList.addFirst(t);
    5.     }
    6.  
    7.     public T pop(){
    8.        return singleLinkList.removeFisrt();
    9.     }
    10.     public T peek(){
    11.         return singleLinkList.getFirst();
    12.     }
    13.     public int getSize(){
    14.         return singleLinkList.size();
    15.     }
    16.     
    17. }
  • 相关阅读:
    【arm】arm平台下char默认数据类型与-fsigned-char
    【arm】arm指令集架构和处理器命名规则和历史
    【shell】正则表达式用法:匹配不包含字符串
    【arm】arm后缀.s和.S的区别以及asm.S说明
    【Optimization/x86】内联汇编Inline assembly——基础学习
    【Optimizaiton/x86】x86 SSE Intrinsic: 点乘算法的Intrinsic实现
    【Optimizaition/x86】Intel CPU的CPUID指令获取的C实现
    【linux】Linux中Core Dump分析
    【shell】linux 查看文件夹以及文件大小数目等信息
    【arm】arm-assembly-print-register-value-in-decimal
  • 原文地址:https://www.cnblogs.com/muzhongjiang/p/13672515.html
Copyright © 2011-2022 走看看