队列是很多高级数据结构和算法的基础,比如集合的List、Queue...
以下demo基本包含了队列的基本方法:
普通队列-数组实现 ArrayQueueCommon.java
/**
* @Author: ltx
* @Description: 普通队列-数组实现
*/
public class ArrayQueueCommon {
private int[] arr;
private int maxSize;//队列大小
private int head;//头
private int rear;//尾
public ArrayQueueCommon(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
head = rear = -1;
}
/**
* 队列是否满了
*
* @return
*/
private Boolean isFull() {
return rear == maxSize - 1;
}
/**
* 判空
*
* @return
*/
private Boolean isEmpty() {
return rear == head;
}
/**
* 队尾添加元素
*
* @param value
*/
private void add(int value) {
if (isFull()) {
System.out.println("队列满了...");
return;
}
arr[++rear] = value;
}
/**
* 队列头移除元素
*
* @return
*/
private int delete() {
if (isEmpty()) {
throw new RuntimeException("队列空...");
}
return arr[++head];
}
/**
* 遍历队列里面有效数据
*/
private void show() {
int i = head;
while (i < rear) {
System.out.print(arr[++i] + ", ");
}
System.out.println();
}
public static void main(String[] args) {
ArrayQueueCommon arrayQueue = new ArrayQueueCommon(5);
arrayQueue.add(2);
arrayQueue.add(1);
arrayQueue.add(5);
arrayQueue.add(3);
arrayQueue.add(4);
arrayQueue.add(7);
arrayQueue.show();
System.out.println("移除元素: " + arrayQueue.delete());
System.out.println("移除元素: " + arrayQueue.delete());
System.out.println("移除元素: " + arrayQueue.delete());
arrayQueue.add(8);
arrayQueue.add(9);
arrayQueue.show();
}
}
环形队列-数组实现 ArrayQueueLoop.java
/**
* 环形队列-数组实现
*/
public class ArrayQueueLoop {
private int[] arr;
private int maxSize;//队列最大长度
private int head;//头
private int rear;//尾
private int size;//队列长度
/**
* 初始化
*
* @param maxSize
*/
public ArrayQueueLoop(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}
/**
* 队列是否满了
*
* @return
*/
private Boolean isFull() {
return size == maxSize;
}
/**
* 判空
*
* @return
*/
private Boolean isEmpty() {
return size == 0;
}
/**
* 队尾添加元素
*
* @param value
*/
private void add(int value) {
if (isFull()) {
System.out.println("队列满了...");
return;
}
arr[rear] = value;
rear = (rear + 1) % maxSize;
size++;
}
/**
* 队列头移除元素
*
* @return
*/
private int delete() {
if (isEmpty()) {
throw new RuntimeException("队列空...");
}
int res = arr[head];
head = (head + 1) % maxSize;
size--;
return res;
}
/**
* 遍历队列里面有效数据
*/
private void show() {
int i = 0, j = head;
while (i < size) {
i++;
System.out.print(arr[j] + ", ");
j = (j + 1) % maxSize;
}
System.out.println();
}
public static void main(String[] args) {
ArrayQueueLoop arrayQueue = new ArrayQueueLoop(5);
arrayQueue.add(2);
arrayQueue.add(1);
arrayQueue.add(5);
arrayQueue.add(3);
arrayQueue.add(4);
arrayQueue.add(7);
arrayQueue.show();
System.out.println("移除元素: " + arrayQueue.delete());
System.out.println("移除元素: " + arrayQueue.delete());
System.out.println("移除元素: " + arrayQueue.delete());
arrayQueue.add(8);
arrayQueue.add(9);
arrayQueue.show();
}
}
队列-链表实现 LinkedQuque.java
/**
* 节点
*/
class SignLinkedNode implements Serializable {
public Integer value;
//下一个节点
public SignLinkedNode next;
public SignLinkedNode() {
}
public SignLinkedNode(Integer value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(value);
}
}
/**
* @Author: ltx
* @Description: 队列-链表实现
*/
public class LinkedQuque {
//头指针
SignLinkedNode head;
//尾指针
SignLinkedNode rear;
//队列实际长度
int size;
//最大长度
int maxSize;
/**
* 构造方法-初始化
*
* @param maxSize
*/
public LinkedQuque(int maxSize) {
this.maxSize = maxSize;
}
/**
* 队列是否满了
* @return
*/
private Boolean isFull() {
return size == maxSize;
}
/**
* 队列判空
* @return
*/
private Boolean isEmpty() {
return size == 0;
}
/**
* 队尾添加元素
* @param node
*/
private void add(SignLinkedNode node) {
if (isFull()) {
System.out.println("队列满了...");
return;
}
//第一个元素
if (head == null) {
head = node;
rear = node;
} else {
rear.next = node;
rear = node;
}
size++;
}
/**
* 队列头删除元素
* @return
*/
private SignLinkedNode delete() {
if (isEmpty()) {
throw new RuntimeException("队列空...");
}
SignLinkedNode temp = head;
head = head.next;
size--;
return temp;
}
/**
* 遍历队列里面有效数据
*/
private void show() {
SignLinkedNode temp = head;
while (temp != null) {
System.out.print(temp + ", ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedQuque arrayQueue = new LinkedQuque(5);
arrayQueue.add(new SignLinkedNode(2));
arrayQueue.add(new SignLinkedNode(1));
arrayQueue.add(new SignLinkedNode(5));
arrayQueue.add(new SignLinkedNode(3));
arrayQueue.add(new SignLinkedNode(4));
arrayQueue.add(new SignLinkedNode(7));
arrayQueue.show();
System.out.println("移除元素: " + arrayQueue.delete());
System.out.println("移除元素: " + arrayQueue.delete());
System.out.println("移除元素: " + arrayQueue.delete());
arrayQueue.add(new SignLinkedNode(8));
arrayQueue.add(new SignLinkedNode(9));
arrayQueue.show();
}
}