栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

队列学习、Java实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

队列学习、Java实现

文章目录

1. 队列(双向链表实现)2. 队列(栈实现)3. 双端队列(双向链表实现)4. 循环队列5. 循环双端队列

思维导图:

1. 队列(双向链表实现)

接口设计

int size(); // 元素的数量
boolean isEmpty(); // 是否为空
void clear(); // 清空
void enQueue(E element); // 入队
E deQueue(); // 出队
E front(); // 获取队列的头元素

优先使用双向链表实现,因为队列主要是往头尾操作元素

public class Queue {

    private final List list=new linkList<>();

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

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

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

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

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

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

2. 队列(栈实现)

思路:

实现:

public class _232_用栈实现队列 {

    private final Stack inStack = new Stack<>();
    private final Stack outStack = new Stack<>();

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        checkOutStack();
        return outStack.pop();
    }

    //获取队头元素
    public int peek() {
        checkOutStack();
        return outStack.peek();
    }

    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    private void checkOutStack() {
        if (outStack.isEmpty()) {
            //将inStack所有元素逐一弹出,push到outStack
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
    }
}
3. 双端队列(双向链表实现)

双端队列是能在头尾两端添加、删除的队列。英文 deque 是 double ended queue 的简称

接口设计:

int size(); // 元素的数量
boolean isEmpty(); // 是否为空
void clear(); // 清空
void enQueueRear(E element); // 从队尾入队
E deQueueFront(); // 从队头出队
void enQueueFront(E element); // 从队头入队
E deQueueRear(); // 从队尾出队
E front(); // 获取队列的头元素
E rear(); // 获取队列的尾元素

实现:

public class DeQue {;
    private final List list = new linkedList<>();
    
    public int size(){
        return list.size();
    }

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

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

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

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

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

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

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

4. 循环队列

循环队列,没有固定的头

public class CircleQueue {

    private int size;
    private E[] elements;
    private int front;//记录队列首元素的位置
    private static final int DEFAULT_CAPACITY = 10;//默认队列大小

    public CircleQueue() {
        elements = (E[]) new Object[DEFAULT_CAPACITY];
    }

    
    public int size() {
        return size;
    }

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

    
    public void enQueue(E element) {
        ensureCapacity(size + 1);
        elements[(front + size) % elements.length] = element;
        size++;
    }

    
    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (capacity < oldCapacity) return;
        //扩容1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[(i + front) % elements.length];
        }
        elements = newElements;
        //重置front的位置
        front = 0;
    }

    
    public E deQueue() {
        E element = elements[front];
        elements[front] = null;
        front = (front + 1) % elements.length;
        size--;
        return element;
    }

    
    public E front() {
        return elements[front];
    }

    
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[index(i)] = null;
        }
        front = 0;
        size = 0;
    }

    
    private int index(int index) {
        index += front;
        return index - (index >= elements.length ? elements.length : 0);
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("capacity=").append(elements.length)
                .append(" size=").append(size)
                .append(" front=").append(front)
                .append(", [");
        for (int i = 0; i < elements.length; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(elements[i]);
        }
        string.append("]");
        return string.toString();
    }
}

5. 循环双端队列

实现:

public class CircleDeQueue {
    private int size;
    private E[] elements;
    private int front;//记录队列首元素的位置
    private static final int DEFAULT_CAPACITY = 10;//默认队列大小

    public CircleDeQueue() {
        elements = (E[]) new Object[DEFAULT_CAPACITY];
    }

    
    public E deQueueRear() {
        int rearIndex = index(size - 1);
        E element = elements[rearIndex];
        elements[rearIndex] = null;
        size--;
        return element;
    }

    
    public void enQueueRear(E element) {
        ensureCapacity(size + 1);
        elements[index(size)] = element;
        size++;
    }

    
    public E deQueueFront() {
        E element = elements[front];
        elements[front] = null;
        front = index(1);
        size--;
        return element;
    }

    
    public void enQueueFront(E element) {
        ensureCapacity(size + 1);
        front = index(-1);
        elements[front] = element;
        size++;
    }

    
    public E front() {
        return elements[front];
    }

    
    public E rear() {
        return elements[index(size - 1)];
    }


    
    public int size() {
        return size;
    }

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

    
    private int index(int index) {
        index += front;
        if (index < 0) {
            index += elements.length;
        }
        return index - (index >= elements.length ? elements.length : 0);
    }

    
    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (capacity < oldCapacity) return;
        //扩容1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[index(i)];
        }
        elements = newElements;
        //重置front的位置
        front = 0;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("capacity=").append(elements.length)
                .append(" size=").append(size)
                .append(" front=").append(front)
                .append(", [");
        for (int i = 0; i < elements.length; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(elements[i]);
        }
        string.append("]");
        return string.toString();
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号