Pull the complete code to the bottom
I. Introduction
As the name implies, queues are like queues in our lives, first in first out.
As shown in the figure above, 25, 16, 5 and 9 are in the queue in turn, and the data in order are 25, 26, 5 and 9, respectively.
II. Realization Process and Ideas
The bottom layer uses arrays to realize the functions of inserting data to the end of the queue, removing the data of the head of the queue, checking the data of the head of the queue, judging whether the queue is empty or not, and judging whether the queue is full or not.
The elements of the queue are stored in an interval of the array, and the queue is continuous in the array, so variables are used to mark the position of the queue in the array.
1. Writing classes and attributes
We can use elements variables to mark the number of elements in the queue, front variables to mark the index of the head element in the array, and end variables to mark the index of the tail element in the array.
public class MyQueue { private Object[] arr;//An array of queue elements private int elements;//Number of queue elements private int front;//Index of header elements in an array private int end;//Index of tail elements in an array 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. Is the queue empty
The variable elements marking the number of queue elements are null if they are zero
public boolean isEmpty() { return elements == 0; }
3. Is the queue full?
Full if the number of queue elements is equal to the length of the array
public boolean isFull() { return elements == arr.length; }
4. Getting the team head element
Gets the element indexed from the array
public Object peek() { return arr[front]; }
5. Remove the head element
Each time an element indexed as front is removed from the array, the next element becomes the head of the queue, that is front+1, and the number of elements in the queue is - 1. There are three situations to consider. If the queue is empty, there is no need to do anything. If it is the last element, you can reset the variable of the marker position directly. Others operate normally.
public Object remove() { if (isEmpty()) { throw new RuntimeException("The queue is empty, so use it."); } Object value = arr[front++]; //If it's the last element, reset the pointer 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.
(1) The array of storage queues is full (the queue is full), which is a good understanding. When the queue is full, it is impossible to add elements to the end of the queue.
(2) Because the queue is continuous in the array, if the elements of the queue are at the end of the array, it is necessary to move the elements from the beginning to the end of the queue to the first place in the array, that is, to leave the position behind (see figure below).
public void insert(Object value) { //Check if the queue is full if (isFull()) { throw new RuntimeException("The elements in the queue have reached the set length"); } //If there is no empty space behind, place the remaining elements at the head of the array if (elements > 1 && end == arr.length - 1) { int i = 0; for (; i < elements; i++, front++) { arr[i] = arr[front]; } front = 0; end = i-1; } //Adding elements back normally in other cases 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()); } }
Complete Code
package com.jikedaquan.datastruct; public class MyQueue { private Object[] arr; private int elements;//Number of queue elements private int front;//Index of header elements in an array private int end;//Index of tail elements in an array 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; } //Insert from the end of the team public void insert(Object value) { //Check if the queue is full if (isFull()) { throw new RuntimeException("The elements in the queue have reached the set length"); } //If there is no empty space behind, place the remaining elements at the head of the array 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++; } //Delete data from the team head public Object remove() { if (isEmpty()) { throw new RuntimeException("The queue is empty, so use it."); } Object value = arr[front++]; //If it's the last element, reset the pointer if (elements == 1) { end = -1; front = 0; elements = 0; } else { elements--; } return value; } //View the data from the team head public Object peek() { return arr[front]; } //Judge whether it is empty 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()); } } }