-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLinkQueue.java
98 lines (81 loc) · 2.45 KB
/
LinkQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ds;
/**
* 链式队列(基于链表实现的队列)
* <p>
* 注:可以向队列里面无限添加元素
*
* @author yangyi 2018年12月02日16:03:12
*/
public class LinkQueue {
private int count;
static class Node {
Node next;
Object object;
}
private Node head;
private Node tail;
public boolean enqueue(Node newNode) {
if (newNode == null) {
return false;
}
//tail==null表示这个队列内目前还一无所有
if (tail == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = tail.next;
}
count++;
return true;
}
public Object dequeue() {
if (count == 0) {
return null;
}
if (head == null) {
return null;
}
Object object = head.object;
head = head.next;
//出队完如果head==null了就说明此时队列中已经没有东西了
if (head == null) {
tail = null;
}
count--;
return object;
}
public int getSize() {
return count;
}
public void printlnAll() {
for (Node p = head; p != null; p = p.next) {
System.out.println(p.object);
}
}
public static void main(String[] args) {
LinkQueue linkQueue = new LinkQueue();
//先准备50个节点塞到这个链式队列当中
for (int i = 0; i < 50; i++) {
Node node = new Node();
node.object = i;
linkQueue.enqueue(node);
}
linkQueue.printlnAll();
System.out.println("————————————朴素的分割线——————————————");
//先出队20个看看结果
for (int i = 0; i < 20; i++) {
System.out.println("出队的元素为:" + linkQueue.dequeue());
}
System.out.println("————————————朴素的分割线——————————————");
linkQueue.printlnAll();
//再往队列里塞30个新元素
for (int i = 0; i < 30; i++) {
Node node = new Node();
node.object = "new" + i;
linkQueue.enqueue(node);
}
System.out.println("————————————朴素的分割线——————————————");
linkQueue.printlnAll();
}
}