Java代码实现队列(数组和链表)

数组实现队列

import org.apache.poi.ss.formula.functions.T;

/**
 * @Author: jerrold
 * @Date: 2021/08/03
 * @Version 1.0
 * @description: 数组实现栈
 */

public class MyQueue {
    
    

    private int cnt;//当前队列长度
    private int queueSize;//队列总长度
    private T[] arr;//数组存储

    //初始化  initQueue
    public MyQueue(int capacity){
    
    
        this.cnt = 0;
        this.queueSize = capacity;
        this.arr = (T[]) new Object[capacity];
    }

    /*队列是否为空*/
    public boolean isEmpty(){
    
    
        return this.cnt == 0;
    }

    /*返回队头元素*/
    public T getHead() throws Exception {
    
    
        if(isEmpty()){
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        return arr[0];
    }

    /*返回队头元素并删除*/
    public T deQueue() throws Exception {
    
    
        if(isEmpty()){
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        T temp = arr[0];//临时存储
        System.arraycopy(arr,1,arr,0,cnt-1);//移除队头元素 调用arraycopy方法
        this.cnt--;//当前队列长度减1
        return temp;
    }

    /*队尾插入新元素*/
    public void enQueue(T value) throws Exception {
    
    
        if (queueSize == cnt){
    
     //队列已满
            throw new Exception("ERROR");
        }
        arr[cnt] = value;
        this.cnt++;//当前队列长度加1
    }

    /*返回队列长度*/
    public int getLength(){
    
    
        return this.cnt;
    }

    /*清空队列*/
    public void clearQueue(){
    
    
        //遍历赋值为null
        for (int i = 0; i < cnt; i++) {
    
    
            arr[cnt] = null;
        }
        this.cnt = 0;//长度置为0
    }

}

链表实现队列

import org.apache.poi.ss.formula.functions.T;

/**
 * @Author: jerrold
 * @Date: 2021/08/03
 * @Version 1.0
 * @description: 数组实现栈
 */

public class MyQueue1 {
    
    

    private int cnt;//当前队列长度
    private Node head;//头结点
    private Node tail;//尾结点

    //定义结点类
    private class Node {
    
    
        private T value;
        private Node next;

        public Node(T t, Node next) {
    
    
            this.value = t;
            this.next = next;
        }

        public Node(T t) {
    
    
            this(t, null);
        }
    }

    //初始化  initQueue
    public MyQueue1() {
    
    
        this.cnt = 0;
        this.head = null;
    }


    /*队列是否为空*/
    public boolean isEmpty() {
    
    
        return this.cnt == 0;
    }

    /*返回队头元素*/
    public T getHead() throws Exception {
    
    
        if (isEmpty()) {
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        return head.value;
    }

    /*返回队头元素并删除*/
    public T deQueue() throws Exception {
    
    
        if (isEmpty()) {
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        T res = head.value; //临时存储头结点的值
        head = head.next;//删除当前结点
        this.cnt--;//队列长度减1
        return res;
    }

    /*队尾插入新元素*/
    public void enQueue(T value) throws Exception {
    
    
        if (isEmpty()){
    
     //当长度为0时 头和尾结点指向同一结点
            head = new Node(value);
            tail = head;
        }else {
    
    
            Node curr = new Node(value); //创建要插入的结点
            tail.next = curr; //尾结点指向新结点
            tail = curr;
        }
        this.cnt++;
    }

    /*返回队列长度*/
    public int getLength() {
    
    
        return this.cnt;
    }

    /*清空队列*/
    public void clearQueue() {
    
    
        this.head = null;//头结点置为null即可 当然也可以将全部结点置为null 加快GC回收
        this.cnt = 0;//长度置为0
    }

}

猜你喜欢

转载自blog.csdn.net/MAKEJAVAMAN/article/details/119357257