一、队列
队列是一种线性数据结构,也是一种操作受限的数据结构,先进先出,后进后出,只能从head出队,从tail入队。
二、队列的实现
1. 顺序队列「数组实现」
1 import java.util.Arrays;
2
3 public class ArrayQueue {
4 private String[] queue;
5 //队列元素个数
6 private int n;
7 //头位置
8 int head;
9 //尾位置
10 int tail;
11
12 public ArrayQueue(int n) {
13 queue = new String[n];
14 this.n = n;
15 this.head = 0;
16 this.tail = 0;
17 }
18
19 @Override
20 public String toString() {
21 return "ArrayQueue{" +
22 "queue=" + Arrays.toString(queue) +
23 ", n=" + n +
24 ", head=" + head +
25 ", tail=" + tail +
26 '}';
27 }
28
29 /**
30 * 数据入队
31 *
32 * @return
33 */
34 public boolean enqueue(String item) {
35
36 if (tail == n) {
37 //队列已满
38 if (head == 0) {
39 return false;
40 } else {
41 //队列满了,但是可以移动数据继续插入数据
42 for (int i = 0; i < tail - head; i++) {
43 queue[i] = queue[head+i];
44 }
45 tail = tail - head;
46 head = 0;
47 }
48 }
49
50 queue[tail] = item;
51 tail++;
52 return true;
53
54 }
55
56 /**
57 * 数据出队
58 *
59 * @return
60 */
61 public String dequeue() {
62 //如果头尾节点相同,证明队列没数据
63 if (head == tail) {
64 return null;
65 }
66
67 String tmp = queue[head];
68 head++;
69 return tmp;
70 }
71
72 public static void main(String[] args) {
73 ArrayQueue arrayQueue = new ArrayQueue(3);
74
75 arrayQueue.enqueue("a");
76 System.out.println(arrayQueue);
77
78 arrayQueue.enqueue("b");
79 System.out.println(arrayQueue);
80
81
82 arrayQueue.enqueue("c");
83 System.out.println(arrayQueue);
84
85 arrayQueue.dequeue();
86 System.out.println(arrayQueue);
87
88 arrayQueue.enqueue("d");
89 System.out.println(arrayQueue);
90
91 }
92 }
2. 链式队列「链表实现」
定义节点
1 public class Node {
2 public String data;
3 public Node next;
4
5 public Node() {
6 }
7
8 public Node(String data) {
9 this.data = data;
10 }
11
12 public Node(String data, Node next) {
13 this.data = data;
14 this.next = next;
15 }
16
17 @Override
18 public String toString() {
19 return "Node{" +
20 "data='" + data + '\'' +
21 '}';
22 }
23 }
实现
1 public class LinkedQueue {
2 Node head;
3 Node tail;
4
5 public LinkedQueue() {
6 this.head = null;
7 this.tail = null;
8 }
9
10 @Override
11 public String toString() {
12 Node tmp = head;
13 StringBuffer sb = new StringBuffer();
14 while (tmp != null) {
15 sb.append(tmp.data).append(",");
16 tmp = tmp.next;
17 }
18 sb.append("\t");
19 sb.append("head:").append(head.data).append("\t");
20 sb.append("tail:").append(tail.data);
21 return sb.toString();
22 }
23
24 public boolean enqueue(String item) {
25 tail = new Node(item, null);
26 if (head == null) {
27 head = tail;
28 } else {
29 Node tmp = head;
30 while (tmp.next != null) {
31 tmp = tmp.next;
32 }
33 tmp.next = tail;
34 }
35
36 return true;
37 }
38
39 public String dequeue() {
40 if (head==tail) {
41 return null;
42 }
43 String tmp = head.data;
44 head = head.next;
45 return tmp;
46 }
47
48 public static void main(String[] args) {
49 LinkedQueue linkedQueue = new LinkedQueue();
50 linkedQueue.enqueue("a");
51 System.out.println(linkedQueue);
52
53 linkedQueue.enqueue("b");
54 System.out.println(linkedQueue);
55
56 linkedQueue.enqueue("c");
57 System.out.println(linkedQueue);
58
59 linkedQueue.enqueue("d");
60 System.out.println(linkedQueue);
61
62 linkedQueue.dequeue();
63 System.out.println(linkedQueue);
64
65 linkedQueue.dequeue();
66 System.out.println(linkedQueue);
67 }
68
69 }
3. 环形队列「数组实现」
1 import java.util.Arrays;
2
3 //环形队列
4 public class CircleQueue {
5 String[] queue;
6 int head;
7 int tail;
8 //队列的长度
9 int n;
10
11 public CircleQueue(int n) {
12 this.queue = new String[n];
13 this.n = n;
14 this.head = 0;
15 this.tail = 0;
16 }
17
18
19 @Override
20 public String toString() {
21 return "CircleQueue{" +
22 "queue=" + Arrays.toString(queue) +
23 ", head=" + head +
24 ", tail=" + tail +
25 ", n=" + n +
26 '}';
27 }
28
29 public boolean enqueue(String item) {
30 if ((tail + 1) % n == head) {
31 return false;
32 }
33
34 queue[tail] = item;
35 tail = (tail + 1) % n;
36 return true;
37 }
38
39 public String dequeue() {
40 if (head == tail) {
41 return null;
42 }
43 String tmp = queue[head];
44 head = (head + 1) % n;
45 return tmp;
46 }
47
48 public static void main(String[] args) {
49 CircleQueue circleQueue = new CircleQueue(3);
50
51 circleQueue.enqueue("a");
52 System.out.println(circleQueue);
53
54 circleQueue.enqueue("b");
55 System.out.println(circleQueue);
56
57 circleQueue.enqueue("c");
58 System.out.println(circleQueue);
59
60 circleQueue.dequeue();
61 System.out.println(circleQueue);
62
63 circleQueue.dequeue();
64 System.out.println(circleQueue);
65
66 circleQueue.enqueue("d");
67 System.out.println(circleQueue);
68
69 circleQueue.dequeue();
70 System.out.println(circleQueue);
71
72 circleQueue.dequeue();
73 System.out.println(circleQueue);
74
75
76 circleQueue.enqueue("e");
77 System.out.println(circleQueue);
78 }
79 }
码无止境