JAVA实现队列,双端队列,循环队列,双端循环队列

队列:

package cn.queue;

import java.util.LinkedList;

import java.util.List;

/** Queue 队列
 *  先进先出,后进后出
 *  只能在头尾连段进行操作
 * @author admin
 */
public class Queue<E> {
    /**
     * 使用双向链表存储
     */
    private List<E> list;

    public Queue() {
        this.list =new LinkedList<E>();
    }

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

    /**
     * 判空
     * @return
     */
    boolean isEmpty(){
        return size()==0;
    }

    /**
     * 清除元素
     */
    void clear(){

    }

    /**
     * 入队
     * @param element
     */
    void enQueue(E element){
        list.add(element);
    }

    /**
     * 出队
     * @return
     */
    E deQueue(){
        return list.remove(0);
    }

    /**
     * 获取队头元素
     * @return
     */
    E front(){
        return list.get(0);
    }


    public static void main(String[] args) {
        Queue<Integer> queue=new Queue<>();
        queue.enQueue(10);
        queue.enQueue(20);
        queue.enQueue(30);
        queue.enQueue(40);

        while (!queue.isEmpty()){
            System.out.println(queue.deQueue());
        }

    }
}

双端队列

package cn.queue;

import java.util.LinkedList;
import java.util.List;

/**
 * 双端队列
 * 能在头尾两端进行添加删除操作
 * double ended queue
 * @author admin
 */
public class Deque<E> {
    /**
     * 使用双向链表
     */
    private List<E> list;

    public Deque() {
        this.list = new LinkedList<E>();
    }

    /**
     * 返回元素数量
     * @return
     */
    int size(){
        return list.size();
    }

    /**
     * 判空
     * @return
     */
    boolean isEmpty(){
        return this.size()==0;
    }

    void clear(){

    }

    /**
     * 从队尾插入
     * @param element
     */
    void enQueueRear(E element){
        list.add(element);
    }

    /**
     * 从队头插入
     * @param element
     */
    void enQueueFront(E element){
        list.add(0,element);
    }

    /**
     * 从队尾出队
     * @return
     */
    E deQueueRear(){
        return list.remove(list.size()-1);
    }

    /**
     * 从队头出队
     * @return
     */
    E deQueueFront(){
        return list.remove(0);
    }

    /**
     * 获取队头
     * @return
     */
    E front(){
        return list.get(0);
    }

    /**
     * 获取队尾
     * @return
     */
    E rear(){
        return list.get(list.size()-1);
    }

    public static void main(String[] args) {
        Deque<Integer> deque=new Deque<>();
        deque.enQueueFront(10);
        deque.enQueueFront(20);
        deque.enQueueRear(30);
        deque.enQueueRear(40);

        while (!deque.isEmpty()){
            System.out.println(deque.deQueueRear());
        }
    }
}

循环队列

package cn.queue;

/**
 * 循环队列
 * 队列底层使用动态数组实现
 *
 * @author admin
 */
public class ClicleQueue<E> {
    private int size;
    private E[] elements;
    /**
     * 队头索引
     */
    private int front;
    private static final int DEFAULE_CAPACITY = 10;

    ClicleQueue() {
        this.elements = (E[]) new Object[DEFAULE_CAPACITY];
    }

    /**
     * 判空
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 获取元素数量
     * @return
     */
    public int size() {
        return this.size;
    }

    /**
     * 获取队头
     * @return
     */
    public E front() {
        return this.elements[this.front];
    }

    /**
     * 入队
     * @param element
     */
    public void enQueue(E element) {
        eusureCapacity(this.size + 1);
        this.elements[index(this.size)] = element;
        this.size++;
    }

    /**
     * 获取映射地址
     * @param index
     * @return
     */
    public int index(int index) {
        return (this.front + index) % this.elements.length;
    }

    /**
     * 出队
     * @return
     */
    public E deQueue() {
        E old = this.elements[this.front];
        this.elements[this.front] = null;
        this.front = index(1);
        return old;
    }

    /**
     * 扩容
     * @param capacity
     */
    private void eusureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (capacity >= oldCapacity) {
            return;
        }
        int newCapacity=oldCapacity+(oldCapacity>>1);
        E[] newElementData= (E[]) new Object[newCapacity];
        for(int i=0;i<size;i++){
            newElementData[i]=elements[index(i)];
        }
        elements=newElementData;
        this.front=0;
    }


}

双端循环队列

package cn.queue;

/**
 * 双端循环队列
 * @author admin
 */
public class ClicleDeque<E> {
    private int size;
    private E[] elements;
    private int front;
    private static final int DEFAULE_CAPACITY=10;


    public ClicleDeque() {
        this.elements= (E[]) new Object[DEFAULE_CAPACITY];
    }

    /**
     * 获取元素数量
     * @return
     */
    public int size() {
        return this.size;
    }

    /**
     * 判空
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 获取队头
     * @return
     */
    public E front() {
        return this.elements[this.front];
    }

    /**
     * 获取队尾
     * @return
     */
    public E rear(){
        return this.elements[index(size-1)];
    }

    /**
     * 从队头入队
     * @param element
     */
    public void enQueueFront(E element) {
        ensureCapacity(size + 1);
        front = index(-1);
        elements[front] = element;
        size++;
    }

    /**
     * 从队尾入队
     * @param element
     */
    public void enQueueRear(E element){
        ensureCapacity(size + 1);
        elements[index(size)] = element;
        size++;
    }

    /**
     * 从头部出队
     * @return
     */
    public E deQueueFront() {
        E frontElement = elements[front];
        elements[front] = null;
        front = index(1);
        size--;
        return frontElement;
    }

    /**
     * 从尾部出队
     * @return 
     */
    public E deQueueRear() {
        int rearIndex = index(size - 1);
        E rear = elements[rearIndex];
        elements[rearIndex] = null;
        size--;
        return rear;
    }
    /**
     * 获取映射地址
     * @param index
     * @return
     */
    public int index(int index) {
        return (this.front + index) % this.elements.length;
    }

    /**
     * 出队
     * @return
     */
    public E deQueue() {
        E old = this.elements[this.front];
        this.elements[this.front] = null;
        this.front = index(1);
        return old;
    }

    /**
     * 扩容
     * @param capacity
     */
    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (capacity >= oldCapacity) {
            return;
        }
        int newCapacity=oldCapacity+(oldCapacity>>1);
        E[] newElementData= (E[]) new Object[newCapacity];
        for(int i=0;i<size;i++){
            newElementData[i]=elements[index(i)];
        }
        elements=newElementData;
        this.front=0;
    }
}

/* * 基于双向链表实现双端队列结构 */ package dsa; public class Deque_DLNode implements Deque { protected DLNode header;//指向头节点(哨兵) protected DLNode trailer;//指向尾节点(哨兵) protected int size;//队列中元素的数目 //构造函数 public Deque_DLNode() { header = new DLNode(); trailer = new DLNode(); header.setNext(trailer); trailer.setPrev(header); size = 0; } //返回队列中元素数目 public int getSize() { return size; } //判断队列是否为空 public boolean isEmpty() { return (0 == size) ? true : false; } //取首元素(但不删除) public Object first() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return header.getNext().getElem(); } //取末元素(但不删除) public Object last() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return trailer.getPrev().getElem(); } //在队列前端插入新节点 public void insertFirst(Object obj) { DLNode second = header.getNext(); DLNode first = new DLNode(obj, header, second); second.setPrev(first); header.setNext(first); size++; } //在队列后端插入新节点 public void insertLast(Object obj) { DLNode second = trailer.getPrev(); DLNode first = new DLNode(obj, second, trailer); second.setNext(first); trailer.setPrev(first); size++; } //删除首节点 public Object removeFirst() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = header.getNext(); DLNode second = first.getNext(); Object obj = first.getElem(); header.setNext(second); second.setPrev(header); size--; return(obj); } //删除末节点 public Object removeLast() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = trailer.getPrev(); DLNode second = first.getPrev(); Object obj = first.getElem(); trailer.setPrev(second); second.setNext(trailer); size--; return(obj); } //遍历 public void Traversal() { DLNode p = header.getNext(); while (p != trailer) { System.out.print(p.getElem()+" "); p = p.getNex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值