Data Structure (3): principles and implementations of the queue

The complete code pulled the bottom

I. INTRODUCTION

As the name suggests queue as we live in the same queue, FIFO.

image

As shown above, 25,16,5,9 sequentially in the queue in the order is out of 25,26,5,9 data respectively.

Second, the implementation process and thinking

Use bottom arrays to implement, the functions have the tail inserted into the data, the data is removed first team, first team to view the data, it is determined whether the queue is empty, it is determined whether the queue is full.

The store queue element in a certain section of the array, the array is continuous in the queue, so the use of variable mark position in the queue array.

1, writing classes and attributes

We can use variable elements mark the number of elements in the queue, using the front variable tag team first element in the array index, end index variable tag team the last element in the array.

image

public class MyQueue {
    
    private Object[] arr;//存放队列元素的数组
    private int elements;//队列元素数量
    private int front;//队头元素在数组中的索引
    private int end;//队尾元素在数组中的索引
    
    
    public MyQueue() {
        arr = new Object[10];
        elements = 0;
        front = 0;
        end = -1;
    }

    public MyQueue(int maxSize) {
        arr = new Object[maxSize];
        elements = 0;
        front = 0;
        end = -1;
    }    
}

2, the queue is empty

Variable number of elements mark queue elements is zero to null

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

3, if the queue is full

The length of the array and the number of elements in the queue that is equal to the full

public boolean isFull() {
    return elements == arr.length;
}

4, to obtain the head elements

Gets the array index for the frontelements

public Object peek() {
    return arr[front];
}

5, the team removed the first element

Each time removing the array index frontelement, next element becomes the first team, that is front+1, the number of elements in the queue elements-1. There are three cases to consider, if the queue is empty then no need to do anything, if you have the last element directly to the variable reset to mark the location of other normal operating conditions.

public Object remove() {
    if (isEmpty()) {
        throw new RuntimeException("队列已经是空的,放心使用吧");
    }
    Object value = arr[front++];
    //如果已经是最后一个元素了,将指针重置即可
    if (elements == 1) {
        end = -1;
        front = 0;
        elements = 0;
    } else {
        elements--;
    }
    return value;
}

6, insert

We write a continuously available queue, so consider the following situation.

(1) storage array queue is full (queue is full), this is easy to understand, full of the elements can not be added to the tail.

(2) because the queue in the array is continuous, if the queue element in the array Finally, elements from the head of the queue is moved to the tail of an array, i.e. the rear position vacant (refer to FIG. ).

public void insert(Object value) {
    //检测队列是否已经满了
    if (isFull()) {
        throw new RuntimeException("队列内元素已达到设定长度");
    }

    //如果后面没有空位置,将余下元素放到数组的头
    if (elements > 1 && end == arr.length - 1) {
        int i = 0;
        for (; i < elements; i++, front++) {
            arr[i] = arr[front];
        }
        front = 0;
        end = i-1;
    }
    //其他情况正常向后添加元素
    arr[++end] = value;
    elements++;
}

7, test

public static void main(String[] args) {
    MyQueue queue = new MyQueue(4);
    queue.insert(11);
    queue.insert(12);
    queue.insert(13);
    queue.insert(14);

    queue.remove();
    queue.remove();
    queue.insert(16);
    //queue.remove();
    //queue.remove();
    
    //queue.insert(19);
    //queue.insert(20);
    queue.remove();

    queue.remove();

    queue.insert(21);
    queue.insert(22);

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

Third, the complete code

package com.jikedaquan.datastruct;

public class MyQueue {
    private Object[] arr;
    private int elements;//队列元素数量
    private int front;//队头元素在数组中的索引
    private int end;//队尾元素在数组中的索引

    public MyQueue() {
        arr = new Object[10];
        elements = 0;
        front = 0;
        end = -1;
    }

    public MyQueue(int maxSize) {
        arr = new Object[maxSize];
        elements = 0;
        front = 0;
        end = -1;
    }

    //从队尾插入
    public void insert(Object value) {
        //检测队列是否已经满了
        if (isFull()) {
            throw new RuntimeException("队列内元素已达到设定长度");
        }

        //如果后面没有空位置,将余下元素放到数组的头
        if (elements > 1 && end == arr.length - 1) {
            int i = 0;
            for (; i < elements; i++, front++) {
                arr[i] = arr[front];
            }
            front = 0;
            end = i-1;
        }
        arr[++end] = value;
        elements++;

    }

    //删除数据,从队头删除
    public Object remove() {
        if (isEmpty()) {
            throw new RuntimeException("队列已经是空的,放心使用吧");
        }
        Object value = arr[front++];
        //如果已经是最后一个元素了,将指针重置即可
        if (elements == 1) {
            end = -1;
            front = 0;
            elements = 0;
        } else {
            elements--;
        }
        return value;
    }

    //查看数据,从队头查看
    public Object peek() {
        return arr[front];
    }

    //判断是否为空
    public boolean isEmpty() {
        return elements == 0;
    }

    public boolean isFull() {
        return elements == arr.length;
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue(4);
        queue.insert(11);
        queue.insert(12);
        queue.insert(13);
        queue.insert(14);

        queue.remove();
        queue.remove();
        queue.insert(16);
//        queue.remove();
//        queue.remove();

//        queue.insert(19);
//        queue.insert(20);
        queue.remove();

        queue.remove();

        queue.insert(21);
        queue.insert(22);

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

Guess you like

Origin www.cnblogs.com/AIThink/p/11620724.html