zoukankan      html  css  js  c++  java
  • 队列结构的实现

    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();
    
    
        }
    
    
    
    
    
    
    }
  • 相关阅读:
    hdu 4283 You Are the One ( dp 2012 ACM/ICPC Asia Regional Tianjin Online )
    hdu 4268 Alice and Bob (贪心 2012 ACM/ICPC Asia Regional Changchun Online)
    2sat 讲解
    高斯消元 Java 高精度版 HDU 2449 Gauss Elimination
    poj 3207 Ikki's Story IV Panda's Trick (2sat)
    poj 2723 Get Luffy Out (2 sat + 二分)
    hdu 2604 Queuing (矩阵乘法 求递推式)
    poj 1753 Flip Game (高斯消元 + 枚举 自由变量)
    poj 3683 Priest John's Busiest Day (2sat 模版)
    vs2008新特性
  • 原文地址:https://www.cnblogs.com/yangxiaohui227/p/13582121.html
Copyright © 2011-2022 走看看