链队列

参考:https://www.cnblogs.com/lixiaolun/p/4646312.html

1、链队列的实现

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
package Queue;

public class LinkQueue {

Node front;
Node rear;
Node head;

public void init() {

head=new Node();
front=new Node();
rear=new Node();

front=head;
rear=head;

}

//入队
public void enQueue(Object e) {
Node node=new Node();
node.data=e;

//第一次入队
if(rear==head) {
head.next=node;
front.next=node;
rear=node;
}else {
rear.next=node;
rear=node;
}
}

public void deQueue() {
if(rear==head) {
//队列为空
return;
}else {
//队中只有一个元素
if(front.next==rear) {
rear=front;
}else {
front.next=front.next.next;
}
}
}

public void printQueue() {
Node temp=head;
while(temp!=rear) {
System.out.println(temp.next.data);
temp=temp.next;
}
}

class Node{
Object data;
Node next;
}
}

2、测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package Queue;

public class TestQueue {

public static void main(String[] args) {
// TODO Auto-generated method stub

LinkQueue qu=new LinkQueue();
qu.init();
qu.enQueue("ddd");
qu.enQueue(8);
qu.enQueue("ct");
qu.enQueue(99);
//qu.deQueue();
qu.printQueue();
}

}