顺序结构队列 代码实现

package com.wangdao.day20.queue;

import com.cskaoyan.exception.EmptyQueueException;
import com.cskaoyan.exception.OutOfMaxSizeException;

import java.util.EmptyStackException;

/**
 * @author Yuechao Yang
 * @version 2019-04-16-20:47
 * 顺序队列的实现
 */
public class MyQueue {
    //fields
    private int DEFAULT_CAPACITY = 10;
    private int MAX_CAPACITY = Integer.MAX_VALUE - 8;
    private String[] elements;
    private int front;      //队头所在的元素下标
    private int rear;       //队尾后面一个位置的下标
    private int size;       //标示队列里元素的数量

    //constructors
    public MyQueue() {
        elements = new String[DEFAULT_CAPACITY];
    }

    public MyQueue(int capacity) {
        if (capacity < 0 || capacity > MAX_CAPACITY) {
            throw new IllegalArgumentException("capacity = " + capacity);
        }
        elements = new String[capacity];
    }

    //methods
    //判空
    public boolean isEmpty(){
        return size == 0;
    }

    //入队
    public void enqueue(String s){
        int minCapacity = size+1;
        if(minCapacity > MAX_CAPACITY || minCapacity < 0){
            throw new OutOfMaxSizeException("minCapacity = " + minCapacity);
        }
        if(minCapacity > elements.length){
            int newCapacity = calculate(minCapacity);       //扩容数组
            grow(newCapacity);                              //制造新数组
        }
        elements[rear] = s;
        rear = (rear + 1) % elements.length;
        size++;                                             //先赋值,再加一
    }

    private void grow(int newCapacity) {
        //创建一个新的字符串数组
        String[] newArr = new String[newCapacity];
        //遍历数组并把元素复制过去
        for(int i = 0; i < size; i++){
            int index = (front + i)% elements.length;
            newArr[i] = elements[index];
        }
        //指向新数组
        elements = newArr;
        front = 0;
        rear = size;
    }

    private int calculate(int minCapacity) {
        int len = elements.length;  //重新定义一个len变量
        len = len + (len >> 1);     //这个len变量是长度的1.5倍
        if (len > MAX_CAPACITY || len < 0) {    //如果这个新建的长度超出了最大容量
            len = MAX_CAPACITY;                 //新建长度是最大容量
        }
        return len > minCapacity ? len : minCapacity;   //返回这个新建长度和minCapacity两者最大的值
    }

    //出队
    public String dequeue(){
        if(isEmpty()){                      //先判空
            throw new EmptyQueueException();
        }
        String s = elements[front];
        elements[front] = null;
        front = (front + 1 )%elements.length;
        size--;
        return s;
    }
}

下面两段代码是自己写的异常类,在队列中抛出异常需要调用

package com.wangdao.day20.exception;

/**
 * @author Yuechao Yang
 * @version 2019-04-16-21:06
 * 自己定义的异常类继承的运行时异常
 */
public class OutOfMaxSizeException extends RuntimeException{
    public OutOfMaxSizeException() {
    }
    public OutOfMaxSizeException(String message){
        super(message);
    }
}

package com.wangdao.day20.exception;

/**
 * @author Yuechao Yang
 * @version 2019-04-16-21:44
 */
public class EmptyQueueException extends RuntimeException {
    public EmptyQueueException() {
    }
    public EmptyQueueException(String message) {
        super(message);
    }
}

你可能感兴趣的:(Java代码实现,数据结构及Java集合框架)