Data structures: java queue

Order queue

concept:

队列是一种先进先出的线性表,只允许在一端插入,另一端删除。允许插入的一端称为队尾,允许删除的一端称为队头

Order the queue implemented:

import org.junit.jupiter.api.Test;

/**
 *   顺序队列
 * @author wydream
 *
 */

public class QueueSequence {

    private String[] arr;//队列数组
    private int end=0;//队尾标志

    //向队列中添加元素
    public void push(String[] arr,String value) {
        if(end<arr.length) {
            arr[end]=value;
            end++;
            return;
        }else {
            System.out.println("队列已经满了");
            return;
        }

    }

    //取出队列元素
    public String pop(String[] arr) {
        String rs;
        if(arr[0]==null) {
            System.out.println("队列为空,请先向队列中添加元素");
            return null;
        }else {
            rs=arr[0];
            arr[0]=null;
            move(arr);
            return rs;
        }
    }

    //队列元素向前移动
    public void move(String[] arr) {
        for(int i=0;i<arr.length-1;i++) {
            if(arr[i+1]!=null) {
                arr[i]=arr[i+1];
            }else{
                arr[i]=null;
                break;
            }
        }
    }




    @Test
    public void test() {
        String[] arr=new String[10];
        push(arr,"北京");
        push(arr,"上海");
        push(arr,"广东");
        push(arr,"杭州");
        push(arr,"苏州");
        push(arr,"扬州");
        pop(arr);
        pop(arr);
        pop(arr);
        pop(arr);
    }

}

Circular queue

concept:

  • Lack of the queues: queue during the sequential inserting operation, can be directly inserted in the tail, in this time complexity is O (1), but is listed in the first team, i.e. position 0 of the subscript, also queue means that all elements have to move forward, the time complexity of this time is 0 (n), less efficient.
  • Column lists the team when all the elements are not required to move to the introduction of two pointers, a first pointer pointing to the head elements front, a rear tail pointer points to the tail element, only this time the column lists the team to move the pointer . But in this case there will be one kind of an overflow condition (see below), then in any case there is room to queue element can be stored, but the tail pointer has overflowed, so there is a circular queue.
    Here Insert Picture Description
    Here Insert Picture Description
  • HOL pointing front, rear point to the next location at the tail; team empty judgment: frontREAR; team for full determination: (rear + 1)% MAXSIZEfront

To achieve the cycle queue:

/**
 *   java实现循环队列
 * @author wydream
 *
 */

import org.junit.jupiter.api.Test;

public class QueueArray {

    Object[] arr=new Object[10];;//对象数组,队列最多存储a.length-1个对象 
    int front=0;//队首下标
    int rear=0;//队尾下标

    /**
     *  将一个对象追加到队列尾部
     */
    public boolean enqueue(Object obj) {
        if((rear+1)%arr.length==front) {
            return false;
        }
        arr[rear]=obj;
        rear=(rear+1)%arr.length;
        return true;

    }

    //出队列
    public Object dequeue() {
        if(rear==front) {
            return null;
        }
        Object obj=arr[front];
        front=(front+1)%arr.length;
        return obj;
    }

    @Test
    public void test() {
        QueueArray q=new QueueArray();
        System.out.println(q.enqueue("北京"));
        System.out.println(q.enqueue("上海"));
        System.out.println(q.enqueue("广东"));
        System.out.println(q.enqueue("深圳"));
        for(int i=0;i<4;i++){   
            System.out.println(q.dequeue());   
        }   
    }
}
Published 156 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42590334/article/details/103635130