java 数据结构 队列的简单实现

1.什么是队列

同栈一样,队列也是表,不过与栈的先进后出不同, 队列是先进先出
这里写图片描述

2.怎么实现

我们使用java,我使用了2中方式实现,一种是数组, 一种是链表

链表实现:

对链表结构有疑问的可以看看我另外的一篇博客:
http://blog.csdn.net/lqx_sunhan/article/details/79043644
因为是链表实现,所以内部肯定有一个Node类。

   private static class Node{

        private Object data;

        private Node next;

        public Node(Object o, Node next){
            this.data = o;
            this.next = next;
        }
    }

我们使用frontrear来代表队头队尾

 /**
     * 队头
     */
    private Node front;

    /**
     * 队尾
     */
    private Node rear;

    /**
     * 长度
     */
    private int theSize;

这里写图片描述

完整代码:

public class QueueLink {

    private static class Node{

        private Object data;

        private Node next;

        public Node(Object o, Node next){
            this.data = o;
            this.next = next;
        }
    }

    /**
     * 队头
     */
    private Node front;

    /**
     * 队尾
     */
    private Node rear;

    /**
     * 长度
     */
    private int theSize;

    public QueueLink(){
        front = null;
        rear = null;
        theSize = 0;
    }

    public boolean enqueue(Object o){
        Node node = new Node(o, null);
        /**
         * 如果是第一次入队
         */
        if(theSize == 0){
            front = rear = node;
        } else {
            rear.next = node;
            rear = node;
        }
        theSize++;
        return true;
    }

    public Object dequeue(){
        if(theSize == 0){
            throw new NullPointerException("队列已空!");
        }
        Object data = front.data;
        front = front.next;
        theSize--;
        return data;
    }

    public int size(){
        return theSize;
    }
}

数组实现

数组实现的话有个问题,因为数组长度是固定的.比如一个长度为10的数组,我们在入队10次后,再次入队便超过数组下标了,而队列不一定是满的,因为可能在这之间有出队的操作.为了解决这个问题,我们使用循环数组的思想.接下来看实现步骤:

  • 初始化队头front,队尾rear都为-1.
  • 为了使元素入队,我们使rear++,然后theArray[rear] = object,若是第一次入队,则队头队尾下标相等,所以front++,最后别忘记theSize++
  • 在rear == theArray.length - 1,说明队尾到了数组尾部,我们使rear = 0来让队尾回到数组开端.如果这时rear == front,说明队尾碰上了队头,则队列满了.
  • 出队时则return theArray[front],然后front++,theSize- -,若front == theArray.length 说明队头到了数组底部,则使front = 0来让队头返回数组开头

这里写图片描述

完整代码:

public class QueueArray {

    /**
     * 数组
     */
    private Object[] theArray;

    /**
     * 队列中的元素数量
     */
    private int theSize;

    /**
     * 队头
     */
    private int front;

    /**
     * 队尾
     */
    private int rear;

    public QueueArray(){
        theArray = new Object[5];
        theSize = 0;
        front = -1;
        rear = -1;
    }

    public boolean enqueue(Object o){
        /**
         * 如果是第一次入队
         */
        if(front == -1 && rear == -1){
            front++;
        }
        /**
         * 如果队尾到达了数组尾端, 则返回数组头部,若队列长度==数组长度,则队列满了
         */
        if(rear == theArray.length - 1){
            rear = -1;
        }
        rear++;
        if(theSize == theArray.length){
            throw new RuntimeException("队列已经满了!");
        }
        theArray[rear] = o;
        theSize++;
        return true;
    }

    public Object dequeue(){
        if(theSize == 0){
            throw new NullPointerException("队列已空!");
        }
        /**
         * 如果到达了数组尾巴,则返回头部
         */
        if(front == theArray.length){
            front = 0;
        }
        Object o = theArray[front];
        theArray[front] = null;
        front++;
        theSize--;
        return o;
    }

    public int size(){
        return theSize;
    }
}

猜你喜欢

转载自blog.csdn.net/lqx_sunhan/article/details/79057090