zoukankan      html  css  js  c++  java
  • 队列及循环队列(Java实现)

    package ch03;
    
    /*
     * 队列类
     */
    public class MyQueue {
        // 底层实现是一个数组
        private long[] arr;
        // 有效数据大小
        private int elements;
        // 队头
        private int front;
        // 队尾
        private int end;
    
        /**
         * 默认构造方法
         */
        public MyQueue() {
            arr = new long[10];
            elements = 0;
            front = 0;
            end = -1;
        }
    
        /**
         * 带参数的构造方法,参数为数组的大小
         */
        public MyQueue(int maxsize) {
            arr = new long[maxsize];
            elements = 0;
            front = 0;
            end = -1;
        }
    
        /**
         * 添加数据,从队尾插入
         */
        public void insert(long value) {
            arr[++end] = value;
            elements++;
        }
    
        /**
         * 删除数据,从队头删除
         */
        public long remove() {
            elements--;
            return arr[front++];
        }
    
        /**
         * 查看数据,从队头查看
         */
        public long peek() {
            return arr[front];
        }
    
        /**
         * 判断是否为空
         */
        public boolean isEmpty() {
            return elements == 0;
        }
    
        /**
         * 判断是否满了
         */
        public boolean isFull() {
            return elements == arr.length;
        }
    }
    package ch03;
    
    /*
     * 循环队列类
     */
    public class MyCycleQueue {
        // 底层实现是一个数组
        private long[] arr;
        // 有效数据大小
        private int elements;
        // 队头
        private int front;
        // 队尾
        private int end;
    
        /**
         * 默认构造方法
         */
        public MyCycleQueue() {
            arr = new long[10];
            elements = 0;
            front = 0;
            end = -1;
        }
    
        /**
         * 带参数的构造方法,参数为数组的大小
         */
        public MyCycleQueue(int maxsize) {
            arr = new long[maxsize];
            elements = 0;
            front = 0;
            end = -1;
        }
    
        /**
         * 添加数据,从队尾插入
         */
        public void insert(long value) {
            if (end == arr.length - 1) {
                end = -1;
            }
            arr[++end] = value;
            elements++;
        }
    
        /**
         * 删除数据,从队头删除
         */
        public long remove() {
            long value = arr[front++];
            if (front == arr.length) {
                front = 0;
            }
            elements--;
            return value;
        }
    
        /**
         * 查看数据,从队头查看
         */
        public long peek() {
            return arr[front];
        }
    
        /**
         * 判断是否为空
         */
        public boolean isEmpty() {
            return elements == 0;
        }
    
        /**
         * 判断是否满了
         */
        public boolean isFull() {
            return elements == arr.length;
        }
    }
    package ch03;
    
    public class TestMyQueue {
        public static void main(String[] args) {
            MyCycleQueue mq = new MyCycleQueue(4);
            mq.insert(23);
            mq.insert(45);
            mq.insert(13);
            mq.insert(1);
    
            System.out.println(mq.isFull());
            System.out.println(mq.isEmpty());
    
            System.out.println(mq.peek());
            System.out.println(mq.peek());
    
            while (!mq.isEmpty()) {
                System.out.print(mq.remove() + " ");
            }
            System.out.println();
    
            mq.insert(23);
            mq.insert(45);
            mq.insert(13);
            mq.insert(1);
    
            while (!mq.isEmpty()) {
                System.out.print(mq.remove() + " ");
            }
        }
    }
  • 相关阅读:
    AutoresizingMask草草草
    StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程
    Unbalanced calls to begin/end appearance transitions for XXXX
    XCode常用快捷键
    Xcode使用心得01:断点中断问题和调整编译目标
    reloaddata 后不执行cellForRowAtIndexPath
    Xcode开发调试技巧—断点调试
    Xcode6 ADD Copy Files Build Phase 是灰色的,不能点问题
    duplicate symbols for architecture x86_64
    could not build module uikit
  • 原文地址:https://www.cnblogs.com/tangxlblog/p/9973209.html
Copyright © 2011-2022 走看看