zoukankan      html  css  js  c++  java
  • 数组模拟队列

    /**
     * 使用数组模拟队列
     * @author Administrator
     */
    
    public class ArrayQueueDemo {
        public static void main(String[] args) {
            ArrayQueue arrayQueue = new ArrayQueue(3);
            arrayQueue.addQueue(2);
            arrayQueue.addQueue(4);
            arrayQueue.addQueue(6);
            arrayQueue.showQueue();
        }
        
    }
    
    //使用数组模拟一个有序队列
    class ArrayQueue{
        private int front = -1;//队列头
        private int rear = -1;//队列尾
        private int maxSize;//表示数组的最大容量
        private int[] arr ;//该数组用于存放数据,模拟队列
        
        //构造函数初始化队列
        public ArrayQueue(int maxSize){
            this.maxSize = maxSize;
            arr = new int[maxSize];
        }
        
        //判断是否满了
        public boolean fullQueue(){
            return rear == maxSize-1;
        }
        
        //判断是否为空
        public boolean isEmpty(){
            return rear == front;
        }
        
    //添加元素
        public void addQueue(int n){
            //校验是否满了
            if (fullQueue()) {
                System.out.println("队列已经满了");
                return;
            }
            //添加到队列中
            rear++;
            arr[rear] = n;
        }
        
        //去除元素
        public int removeQueue(){
            //校验是否为空
            if (isEmpty()) {
                throw new RuntimeException("当前为空不能去除元素!");
            }
            front++;
            return arr[front]; 
        }
        
        public void showQueue(){
            if (!isEmpty()) {
                for (int i = 0; i < arr.length; i++) {
                    System.err.println(arr[i]);
                }
            }
            
        }
        
         
    }
  • 相关阅读:
    sql语句添加查询字段
    SqlServer Case when then用法总结
    单例与多线程
    HttpSession详解
    范式
    SQL语句中的Having子句与where子句
    HTTP无状态
    字节流与字符流的区别
    选择排序
    ReentrantLock VS synchronized
  • 原文地址:https://www.cnblogs.com/keiyoumi520/p/13617345.html
Copyright © 2011-2022 走看看