Queue 队列设计

在头尾两端进行操作,先进先出 FIFO

队列的接口设计

int size(); //队列元素个数
boolean isEmpty(); //队列是否为空
void clear(); //清空队列元素
void enQueue(E element); //入队
E deQueue(); // 出队
E front(); //获取队列的头元素
package com.wztlink1013.ds.queue;

import com.wztlink1013.ds.queue.list.LinkedList;
import com.wztlink1013.ds.queue.list.List;

public class Queue<E> {
    private List<E> list = new LinkedList<>();

    public int size() {
        return list.size();
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public void clear() {
        list.clear();
    }

    public void enQueue(E element) {
        list.add(element);
    }

    public E deQueue() {
        return list.remove(0);
    }

    public E front() {
        return list.get(0);
    }
}

Deque 双端队列设计

双端队列是能在头尾两端添加、删除的队列

package com.wztlink1013.ds.queue;

import com.wztlink1013.ds.queue.list.LinkedList;
import com.wztlink1013.ds.queue.list.List;

public class Deque<E> {
    private List<E> list = new LinkedList<>();

    public int size() {
        return list.size();
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public void clear() {
        list.clear();
    }

    public void enQueueRear(E element) {
        list.add(element);
    }

    public E deQueueFront() {
        return list.remove(0);
    }

    public void enQueueFront(E element) {
        list.add(0, element);
    }

    public E deQueueRear() {
        return list.remove(list.size() - 1);
    }

    public E front() {
        return list.get(0);
    }

    public E rear() {
        return list.get(list.size() - 1);
    }
}