zoukankan      html  css  js  c++  java
  • 数组实现不超过固定大小的队列(环形数组)

    package arithmetic;
    
    public class MyQueue {
        private int[] arr;
        private int pushi;
        private int polli;
        private int size;
        private final int limit;
    
        public MyQueue(int limit) {
            arr = new int[limit];
            pushi = 0;
            polli = 0;
            size = 0;
            this.limit = limit;
        }
    
        public void push(int value) {
            if (size == limit) {
                throw new RuntimeException("栈满了,不能再加了");
            }
            size++;
            arr[pushi] = value;
            pushi = nextIndex(pushi);
        }
    
        public int pop() {
            if (size == 0) {
                throw new RuntimeException("栈空了,不能再拿了");
            }
            size--;
            int res = arr[polli];
            polli = nextIndex(polli);
            return res;
        }
    
        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1 : 0;
        }
    }
  • 相关阅读:
    bzoj1027
    bzoj1069
    poj2079
    poj2187
    bzoj2281
    bzoj2285
    bzoj1558
    bzoj1822
    bzoj1559
    bzoj1570
  • 原文地址:https://www.cnblogs.com/yanghailu/p/12774593.html
Copyright © 2011-2022 走看看