• 队列结构的实现


    public class ArrayQueue {
    
        private int maxsize=16;
        private int last;
        private int front;
        private int[] arr;
    
        public ArrayQueue(int maxsize) {
            if(maxsize<=0){
                throw new RuntimeException("长度需要大于0");
            }
            this.maxsize = maxsize;
            arr=new int[maxsize];
            this.last=-1;
            this.front=-1;
        }
        private void push(int value){
            //先判断队列是否已满
            if(this.isFull()){
                throw new RuntimeException("队列已满");
            }
            if(last==maxsize-1){
                //已经是最后一个索引了
                int[] newArr=new int[maxsize];
                int j=0;
                for(int k=front+1;k<=last;k++){
                    newArr[j]=arr[k];
                    j++;
                }
                last=last-front-1;
                front=-1;
                arr=newArr;
            }
            last++;
            arr[last]=value;
    
        }
        private boolean isFull(){
            if(front==-1){
                return this.last==maxsize-1;
            }
            return this.last-this.front==maxsize-1;
        }
    
        private boolean isEmpty(){
            return front>=last;
        }
    
        public int pop(){
            if(this.isEmpty()){
                throw new RuntimeException("队列没有数据");
            }
            front++;
            int k=arr[front];
            return k;
        }
        public int peek(){
            if(isEmpty()){
                throw new RuntimeException("队列没有数据");
            }
            int value=arr[front+1];
            return value;
        }
    
        public void printIndex(String prefix){
            System.out.println(prefix);
            System.out.println("last:"+last);
            System.out.println("front:"+front);
        }
    
        public static void main(String[] args) {
            ArrayQueue queue = new ArrayQueue(3);
            queue.push(1);
            queue.push(3);
            queue.push(9);
            System.out.println(queue.pop());
            System.out.println(queue.pop());
            System.out.println(queue.pop());
            queue.push(8);
            queue.push(8);
            queue.push(6);
            System.out.println(queue.pop());
            System.out.println(queue.pop());
            queue.push(8);
            System.out.println(queue.pop());
    
        }
    
    
    
    
    }


    第二种是循环数组来实现

    package com.yang.xiao.hui.queue;
    
    public class CircleQueue {
    
        private int maxsize=16;
        private int last; //最后一个元素的下一个位置
        private int front;//当前第一个元素
        private int[] arr;
    
        public CircleQueue(int maxsize) {
            if(maxsize<=0){
                throw new RuntimeException("长度需要大于0");
            }
            this.maxsize = maxsize;
            arr=new int[maxsize];
            this.last=0;
            this.front=0;
        }
        private void push(int value){
            //先判断队列是否已满
            if(this.isFull()){
                throw new RuntimeException("队列已满");
            }
            int index=last % maxsize;
            arr[index]=value;
            last++;
    
        }
        private boolean isFull(){
    
            return this.last-this.front==maxsize;
        }
    
        private boolean isEmpty(){
            return front==last;
        }
    
        public int pop(){
            if(this.isEmpty()){
                throw new RuntimeException("队列没有数据");
            }
            int index=front % maxsize;
            int k=arr[index];
            front++;
            return k;
        }
        public int peek(){
            if(isEmpty()){
                throw new RuntimeException("队列没有数据");
            }
            int index=front % maxsize;
            int value=arr[index];
            return value;
        }
        public void println(){
            int start=front % maxsize;
            int end=(last-1) % maxsize;
            for(int i=start;i<=end;i++){
                System.out.println(arr[i]);
            }
        }
        public static void main(String[] args) {
            CircleQueue circleQueue = new CircleQueue(3);
            /*int i=0;
            while (true){
                circleQueue.push(1);
                System.out.println(circleQueue.pop());
                circleQueue.push(2);
                System.out.println(circleQueue.pop());
                circleQueue.push(3);
                System.out.println(circleQueue.pop());
    
                i++;
                if(i>100){
                    break;
                }
            }*/
            circleQueue.push(1);
            circleQueue.push(2);
            circleQueue.push(3);
            circleQueue.println();
    
    
        }
    
    
    
    
    
    
    }
  • 相关阅读:
    工作流-1
    net core体系-Xamarin-2概要(lignshi)
    net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第二章 入门篇-快速入门ASP.NET Core看这篇就够了
    手机支持NFC
    net core体系-Standard-1概述
    运营-赵本山最近有点烦:二人转产业链滑铁卢 关联公司IPO预披露
    MSSql-1内部数据库版本号
    (JAVA保留小数问题,基础)Probability hdu2131
    (stripTrailingZeros)A == B hdu2054
    (reverse)Palindromes hdu2163
  • 原文地址:https://www.cnblogs.com/yangxiaohui227/p/13582121.html
Copyright © 2020-2023  润新知