队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
队列的入队和出队过程
图片(注:百度搜索而来的图片)链接: link.
java代码实现
public class Queue<E> {
int size;
private Node head;
private Node tail;
public Queue() {
this.size = 0;
this.head = null;
this.tail = null;
}
public static void main(String[] args) throws Exception {
int[] arr = new int[]{27, 24, 16, 42 ,41, 25, 20, 37, 20, 13, 14, 2, 39, 5, 16};
Queue<Integer> queue = new Queue<>();
for (int i = 0; i < arr.length; i++) {
System.out.println("将要入队元素 : " + arr[i]);
queue.enqueue(arr[i]);
if (i % 5 == 4) {
System.out.println("head : " + queue.front());
System.out.println("dequeue : " + queue.dequeue());
}
System.out.println("size : " + queue.getSize());
System.out.println(queue.toString());
System.out.println("--------------------------------------------------------");
}
}
/**
* 入队
*
* @param e
*/
public void enqueue(E e) {
if (this.tail == null) {
this.tail = new Node(e);
this.head = this.tail;
} else {
Node newNode = new Node(e);
this.tail.next = newNode;
this.tail = this.tail.next;
}
this.size++;
}
/**
* 出队
*
* @return
*/
public E dequeue() throws Exception {
if (this.isEmpty()) {
throw new Exception("queue already empty!");
}
Node retNode = this.head;
this.head = this.head.next;
retNode.next = null;
if (this.head == null) {
this.tail = null;
}
this.size--;
return retNode.e;
}
/**
* 获得队首元素
*
* @return
*/
public E front() {
if (this.head != null) {
return this.head.e;
} else {
return null;
}
}
public int getSize() {
return this.size;
}
public boolean isEmpty() {
return this.size == 0;
}
@Override
public String toString() {
return _toString();
}
private String _toString() {
Node copyNode = this.head;
StringBuffer stringBuffer = new StringBuffer();
while (copyNode != null) {
stringBuffer.append(copyNode.e).append(" => ");
copyNode = copyNode.next;
}
return stringBuffer.toString();
}
/**
* 节点
*/
private class Node {
private E e;
private Node next;
public Node(E e) {
this.e = e;
this.next = null;
}
public String toString() {
return this.e.toString();
}
}
}
将要入队元素 : 27
size : 1
27 =>
--------------------------------------------------------
将要入队元素 : 24
size : 2
27 => 24 =>
--------------------------------------------------------
将要入队元素 : 16
size : 3
27 => 24 => 16 =>
--------------------------------------------------------
将要入队元素 : 42
size : 4
27 => 24 => 16 => 42 =>
--------------------------------------------------------
将要入队元素 : 41
head : 27
dequeue : 27
size : 4
24 => 16 => 42 => 41 =>
--------------------------------------------------------
将要入队元素 : 25
size : 5
24 => 16 => 42 => 41 => 25 =>
--------------------------------------------------------
将要入队元素 : 20
size : 6
24 => 16 => 42 => 41 => 25 => 20 =>
--------------------------------------------------------
将要入队元素 : 37
size : 7
24 => 16 => 42 => 41 => 25 => 20 => 37 =>
--------------------------------------------------------
将要入队元素 : 20
size : 8
24 => 16 => 42 => 41 => 25 => 20 => 37 => 20 =>
--------------------------------------------------------
将要入队元素 : 13
head : 24
dequeue : 24
size : 8
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 =>
--------------------------------------------------------
将要入队元素 : 14
size : 9
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 =>
--------------------------------------------------------
将要入队元素 : 2
size : 10
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 =>
--------------------------------------------------------
将要入队元素 : 39
size : 11
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 39 =>
--------------------------------------------------------
将要入队元素 : 5
size : 12
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 39 => 5 =>
--------------------------------------------------------
将要入队元素 : 16
head : 16
dequeue : 16
size : 12
42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 39 => 5 => 16 =>
--------------------------------------------------------