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());
        }
    
    }
    
  • 相关阅读:
    Docker基本命令及工作原理
    Docker安装
    linux命令
    MTPuTTy使用
    SpringBoot--swagger搭建、配置及使用
    idea使用技巧
    Idea插件
    IDEA开发工具使用 git 创建项目、拉取分支、合并分支
    git命令
    javbus爬虫-老司机你值得拥有
  • 原文地址:https://www.cnblogs.com/qxlxi/p/12860750.html
Copyright © 2011-2022 走看看