-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
48 lines (40 loc) · 946 Bytes
/
LinkedQueue.java
File metadata and controls
48 lines (40 loc) · 946 Bytes
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
package sjjg;
public class LinkedQueue<T> implements Queue {
private Node<T> front , rear;
public LinkedQueue() {
this.front = this.rear = null;
}
@Override
public boolean isEmpty() {
// TODO 自动生成的方法存根
return this.front == null && this.rear == null;
}
@Override
public boolean add(Object x) {
// TODO 自动生成的方法存根
if(x == null)
return false;
Node<T> q = new Node<T>((T) x,null);
if(this.front == null)
this.front = q;
else this.rear.next = q;
this.rear = q;
return true;
}
@Override
public Object peek() {
// TODO 自动生成的方法存根
return this.isEmpty()?null:this.front.data;
}
@Override
public Object poll() {
// TODO 自动生成的方法存根
if(isEmpty())
return null;
T x = this.front.data;
this.front = this.front.next;
if(this.front == null)
this.rear = null;
return x;
}
}