数组实现队列-java

package com.suanfa;

import java.lang.reflect.Array;
import java.util.Scanner;

public class ArrayQueue<T> {
    private int front = 0;//头部指针,空
    private int rear = 0;//尾部指针,有值
    private int maxCapacity = 10;//容量,默认给10
    private T[] array;//数组,存储内容

    public ArrayQueue(Class<T> clazz, int maxCapacity) {
        this.maxCapacity = maxCapacity;
        array = (T[]) Array.newInstance(clazz, maxCapacity);
    }

    /*
      isEmpty()    判空
      isFull()     判满
      add(T a)     入队
      get()        出队
      getSize()    获取队列个数
      getInfo()    获取队列存储信息
      readHead()   读取头部元素
      showAll()    显示队列所有内容
      clear()      清空队列
      destroy()    销毁队列释放内存

     */
    public boolean isEmpty() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        return front == rear;
    }

    public boolean isFull() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        return (rear + 1) % maxCapacity == front;
    }

    public void add(T a) {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        if (isFull()) {
            System.out.println("满,添加失败!");
            return;
        }
        rear = (rear + 1) % maxCapacity;
        array[rear] = a;
    }

    public T get() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        if (isEmpty()) {
            System.out.println("空,获得失败!");
            return null;
        }
        front = (front + 1) % maxCapacity;
        return array[front];
    }

    public int getSize() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        int size = 0;
        if (rear >= front) {//包括空队列
            size = rear - front;
        } else {
            size = (maxCapacity - front - 1) + (rear + 1);
        }
        return size;
    }

    public String getInfo() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        return "front:" + front + "\t" +
                "rear:" + rear + "\t" +
                "maxCapacity-1:" + (maxCapacity - 1);
    }

    public T readHead() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        if (isEmpty()) {
            System.out.println("空");
            return null;
        } else {
            int head = (front + 1) % maxCapacity;
            return array[head];
        }
    }

    public void showAll() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        if (isEmpty()) {
            System.out.println("{}");
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            if (rear >= front) {
                for (int i = front + 1; i <= rear; i++) {
                    sb.append(array[i] + ",");
                }
            } else {
                for (int i = front + 1; i <= maxCapacity - 1; i++) {
                    sb.append(array[i] + ",");
                }
                for (int i = 0; i <= rear; i++) {
                    sb.append(array[i] + ",");
                }
            }
            sb.deleteCharAt(sb.lastIndexOf(","));
            sb.append("}");
            System.out.println(sb.toString());

        }
    }

    public void clear() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        front = 0;
        rear = 0;
    }

    public void destroy() {
        if(this.array==null){
            throw new RuntimeException("队列已销毁!");
        }
        this.array = null;
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> arrayQueue = new ArrayQueue<Integer>(Integer.class, 5);
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("1:添加");
            System.out.println("2:读取");
            System.out.println("3:实际size");
            System.out.println("4:队列属性");
            System.out.println("5:readHead");
            System.out.println("6:showAll");
            System.out.println("7:清空队列");
            System.out.println("8:销毁队列");
            int key = sc.nextInt();
            switch (key) {
                case 1:
                    arrayQueue.add(sc.nextInt());
                    break;
                case 2:
                    System.out.println(arrayQueue.get());
                    break;
                case 3:
                    System.out.println(arrayQueue.getSize());
                    break;
                case 4:
                    System.out.println(arrayQueue.getInfo());
                    break;
                case 5:
                    System.out.println(arrayQueue.readHead());
                    break;
                case 6:
                    arrayQueue.showAll();
                    break;
                case 7:
                    arrayQueue.clear();
                    break;
                case 8:
                    arrayQueue.destroy();
                    break;
                default:
                    break;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值