zoukankan      html  css  js  c++  java
  • 栈(Java实现)

    package ch03;
    
    public class MyStack {
        // 底层实现是一个数组
        private long[] arr;
        private int top;
    
        /**
         * 默认的构造方法
         */
        public MyStack() {
            arr = new long[10];
            top = -1;
        }
    
        /**
         * 带参数构造方法,参数为数组初始化大小
         */
        public MyStack(int maxsize) {
            arr = new long[maxsize];
            top = -1;
        }
    
        /**
         * 添加数据
         */
        public void push(int value) {
            arr[++top] = value;
        }
    
        /**
         * 移除数据
         */
        public long pop() {
            return arr[top--];
        }
    
        /**
         * 查看数据
         */
        public long peek() {
            return arr[top];
        }
    
        /**
         * 判断是否为空
         */
        public boolean isEmpty() {
            return top == -1;
        }
    
        /**
         * 判断是否满了
         */
        public boolean isFull() {
            return top == arr.length - 1;
        }
    }
    package ch03;
    
    public class TestMyStack {
        public static void main(String[] args) {
            MyStack ms = new MyStack(4);
            ms.push(23);
            ms.push(12);
            ms.push(1);
            ms.push(90);
            System.out.println(ms.isEmpty());
            System.out.println(ms.isFull());
            
            System.out.println(ms.peek());
            System.out.println(ms.peek());
            
            while(!ms.isEmpty()) {
                System.out.print(ms.pop() + ",");
            }
            
            System.out.println(ms.isEmpty());
            System.out.println(ms.isFull());
        }
    }
  • 相关阅读:
    apache虚拟主机三种不同配置方式
    搭建http服务器及配置
    学校ftp服务器搭建
    vsftpd搭建使用
    nginx使用
    pxe+kickafkstart (二)转
    pxe批量网络装机
    bash中()使用特性
    ansible使用
    javascript 之 Object.defineProperty
  • 原文地址:https://www.cnblogs.com/tangxlblog/p/9973201.html
Copyright © 2011-2022 走看看