zoukankan      html  css  js  c++  java
  • 【数据结构与算法之美】1. 基于数组实现的栈

    1.基于数组实现的栈

    package com.ncst.stack;
    
    import com.ncst.array.Array;
    
    /**
     * @author i
     * @create 2019/12/19 19:47
     * @Description
     */
    public class ArrayStack{
    
        private Integer [] data;//存储数据
    
        private Integer length;//长度
    
        private Integer capacity;//容量
    
        public ArrayStack(Integer capacity){
            data = new Integer[capacity];
            length = capacity;
            this.capacity = 0;
        }
    
        public void push(Integer value){
            if (capacity == length){
                throw new RuntimeException("statkc id is full!");
            }
            data[capacity] = value;
            capacity++;
        }
    
        public Integer pop(){
            if (capacity == 0){
                throw  new RuntimeException("stack id empty!");
            }
            Integer value = data[capacity-1];
            capacity--;
            return value;
        }
    
        public static void main(String[] args) {
            ArrayStack arrayStack = new ArrayStack(5);
            arrayStack.push(1);
            arrayStack.push(2);
            arrayStack.push(3);
    
            System.out.println(arrayStack.pop());
            System.out.println(arrayStack.pop());
            System.out.println(arrayStack.pop());
        }
    
    }
    
  • 相关阅读:
    mock数据
    Vuex
    React生命周期
    Vue基础知识
    前端面试题
    NodeJS巅峰之作
    Oracle数据库
    CSS Bootstrap jsp开发 前端遇到的一些问题。
    如何寻找node.js 与win7兼容的版本?eclipse中引入bootstrap。
    Window 常用命令
  • 原文地址:https://www.cnblogs.com/qxlxi/p/12860750.html
Copyright © 2011-2022 走看看