【手写系列】队列

具体有难度的地方已在文中注释
队列的三种实现

1.数组队列

/**
 * @author Jay
 * @date 2020/7/8 19:39
 * @Description:
 */
public class ArrayQueue<E>  {

    private Array<E> array;

    public ArrayQueue(int capcity){
        array=new Array<>(capcity);
    }

    public ArrayQueue(){
        array=new Array<>();
    }

    public int getsize(){
        return array.getSize();
    }
    public boolean isEmpty (){
        return  array.isEmpty();

    }

    public void enqueue(E e){
        array.addLast(e);
    }

    public E dequeue(){
         return array.removeFirst();
    }

    public E getFront(){
        return array.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue: ");
        res.append('[');
        for (int i = 0; i < array.getSize(); ++i) {
            res.append(array.get(i));
            if (i != array.getSize() - 1)
                res.append(", ");
        }
        res.append("] tail");
        return res.toString();
    }
}

2.循环队列

/**
 * @author Jay
 * @date 2020/7/9 21:46
 * @Description:
 */
public class LoopQueue<E> {

    private E[] data;
    private int tail,front;
    private int size;

    public LoopQueue(){
        this(10);
    }

    @SuppressWarnings("unchecked")
    public LoopQueue(int capcity){
        data= (E[]) new Objects[capcity+1];
        front=0;
        tail=0;
        size=0;
    }
    public int getCapcity(){
        return data.length-1;
    }

    public boolean isEmpty(){
        return size==0;

    }
    public int getSize(){
        return size;
    }

    public void enqueue(E e){
        if ((tail+1)%data.length==front)
            resize(getCapcity()*2);

        data[tail]=e;
        tail=(tail+1)%data.length;
        ++size;
    }
    public E dequeue(){
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeeu from an empty queue.");
        E ret=data[front];
        data[front]=null;
        front=(front+1)%data.length;
        --size;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) //缩容
            resize(getCapacity() / 2);
        return ret;
    }

    public E getFront(){
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeeu from an empty queue.");
        return data[front];
    }

    @SuppressWarnings("unchecked")
    public void resize(int newCapacity){
        E[] newdata= (E[]) new Object[newCapacity+1];
        for (int i = 0; i <size ; ++i)
            newdata[i]=data[(front+i)%data.length];
        data=newdata;
        front=0;
        tail=size;
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size = %d, capacity = %d\n", size, getCapacity()));
        res.append("front [");
        for (int i = front; i != tail; i = (i + 1) % data.length) {
            res.append(data[i]);
            if ((i + 1) % data.length != tail)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }




}


3.链队

/**
 * @author Jay
 * @date 2020/7/9 22:18
 * @Description:
 */
public class LinkQueue<E> {
    private class Node { //链表结点不变
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }
        public Node(E e) {
            this(e, null);
        }
        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node head;
    private Node tail;
    private  int size;

    public LinkQueue(){
        head=null;
        tail=null;
        size=0;
    }

    public int getSize(){
        return size;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public void enqueue(E e){
        if (tail==null){
            tail=new Node(e);
            head=tail;
        }else {
            tail.next = new Node(e);
            tail = tail.next;

        }
        ++size;
    }

    public E dequeue(){
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        Node ret=head;
        head=head.next;
        if (head==null)
            tail=null;

        ret.next=null;
        --size;

        return ret.e;
    }

    public E getFront(){
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        return head.e;

    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue: front ");

        Node cur = head;
        while (cur != null) {
            res.append(cur.e + "->");
            cur = cur.next;
        }
        res.append("NULL tail");
        return res.toString();
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值