-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeList.java
More file actions
43 lines (38 loc) · 745 Bytes
/
SnakeList.java
File metadata and controls
43 lines (38 loc) · 745 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
//the snake's list of segments in its body
public class SnakeList {
Node head;
Node tail;
SnakeList() {
head = new Node();
tail = new Node();
tail.prev = head;
head.next = tail;
}
void addFirst(Node p) {
p.prev = head;
p.next = head.next;
head.next.prev = p;
head.next = p;
}
Node getFirst() {
if (head.next != tail)
return head.next;
else
return null;
}
Node removeLast() {
if (tail.prev != head) {
Node temp = tail.prev;
tail.prev.prev.next = tail;
tail.prev = tail.prev.prev;
return temp;
} else
return null;
}
void addLast(Node n) {
n.prev = tail.prev;
n.next = tail;
tail.prev = n;
tail.prev.next = n;
}
}