forked from BinaryBall/java-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkQueue.java
More file actions
57 lines (49 loc) · 1.07 KB
/
LinkQueue.java
File metadata and controls
57 lines (49 loc) · 1.07 KB
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
package com.jamal;
/**
* 基于链表的队列
*/
public class LinkQueue {
// 指向队首
private Node head;
// 指向队尾
private Node tail;
/**
* 入队操作
* @param data
* @return
*/
public int enqueue(String data){
Node node = new Node(data,null);
// 判断队列中是否有元素
if (tail == null) {
tail = node;
head = node;
}else {
tail.next = node;
tail = node;
}
return 1;
}
/**
* 出队操作
* @return
*/
public String dequeue(){
if (head==null) return null;
String data = head.data;
head = head.next;
// 取出元素后,头指针为空,说明队列中没有元素,tail也需要制为空
if (head == null){
tail = null;
}
return data;
}
class Node{
private String data;
private Node next;
public Node(String data,Node node){
this.data = data;
next = node;
}
}
}