队列是一种先入先出的数据结构一般有数组和链表两种实现方式

数组实现:

import java.util.Arrays;
/**
 * 测试
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {

        ArrayQueue queue = new ArrayQueue();

        queue.get();//没有数据时获取数据
        /**
         * 往里面添加数据
         * 并测试数组充满后是否还能继续添加
         */
        for (int i = 0; i < 6; i++) {
            queue.add(i + 1);
        }

        queue.list();//打印全部数据
        queue.get();//第一次获取数据
        queue.add(6);//取出数据后是否能继续添加
        queue.list();//打印队列查看有没有问题
        /**
         *
         * 测试数据是否是先入先出的特点
         */
        for (int i = 0; i < 6; i++) {
            queue.get();
        }

    }
}

/**
 * 使用数组实现队列
 */
class ArrayQueue {

    private int top;

    private int bottom;

    /**
     * 为了方便测试
     * 我将数组的长度设置成五
     */

    private int[] arr = new int[5];

    public ArrayQueue() {
    }

    /**
     * 添加数据
     *
     * @param i
     */
    public void add(int i) {
        if (isFull()) {
            System.out.println("队列已满,不能添加");
            return;
        }
        arr[top % arr.length] = i;
        top += 1;
    }

    public void get() {
        if (isNull()) {
            System.out.println("队列为空,没有数据可以取出");
            return;
        }
        int result = arr[bottom % arr.length];
        bottom += 1;
        System.out.println(result);
    }

    /**
     * 是否存满数据
     *
     * @return
     */
    public boolean isFull() {
        if ((top % arr.length) == (bottom % arr.length) && top != bottom) {
            return true;
        }
        return false;
    }

    /**
     * 遍历数据
     */
    public void list() {
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 是否为空
     * @return
     */
    public boolean isNull() {
        if (top == bottom) {
            return true;
        }
        return false;
    }
    
    
}

链表实现

/**
 * 测试
 */
public class LinkedQueueTest {

    public static void main(String[] args) {
        LinkedQueue queue = new LinkedQueue();
        queue.add("1. abc");
        queue.add("2. zxc");
        queue.add("3. qwe");
        queue.add("4. bcd");
        System.out.println(queue.get());
        System.out.println("-----------------");
        queue.list();
        System.out.println("-----------------");
        System.out.println(queue.get());
        System.out.println(queue.get());
        System.out.println(queue.get());

    }

}

class LinkedQueue {

    private LinkedQueueBase head;

    public LinkedQueue() {
    }

    public void add(String name) {
        if (head == null) {
            head = new LinkedQueueBase(name);
            return;
        }
        LinkedQueueBase temp = head;
        while (true) {
            if (temp.next == null) {
                temp.next = new LinkedQueueBase(name);
                break;
            }
            temp = temp.next;
        }
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean isNull() {
        if (head == null || head.getName() == null) {
            return true;
        }
        return false;
    }

    /**
     * 遍历数据
     */
    public void list() {
        if (isNull()) {
            System.out.println("没有数据");
            return;
        }
        LinkedQueueBase temp = head;
        while (true) {
            if (temp.next == null) {
                System.out.println(temp.getName());
                return;
            }
            System.out.println(temp.getName());
            temp = temp.next;
        }
    }

    /**
     * 获取数据
     *
     * @return
     */
    public String get() {

        if (isNull()) {
            return "队列数据为空";
        }
        String name = head.getName();
        if (head.next == null) {
            head.setName(null);
        } else {
            head = head.next;
        }
        return name;
    }
}


class LinkedQueueBase {

    private String name;
    public LinkedQueueBase next;

    public LinkedQueueBase(String name) {

        this.name = name;
    }

    @Override
    public String toString() {
        return "LinkedBase{" +
                ", name='" + name + '\'' +
                '}';
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}