-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeDLL.java
More file actions
63 lines (49 loc) · 1.25 KB
/
SnakeDLL.java
File metadata and controls
63 lines (49 loc) · 1.25 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
58
59
60
61
62
63
import java.util.*;
public class SnakeDLL{
public class Cell {
public int x, y;
Cell next, prev;
Cell (int x, int y, Cell n, Cell p) {
this.x=x; this.y=y; next=n; prev=p;
}
Cell() { // used only to create header cell
next=prev=this;
}
}
public Cell header = new Cell();
public void addBefore(Cell p, int x, int y) {
p.prev.next = new Cell(x, y, p, p.prev);
p.prev = p.prev.next;
}
public void addLast(int x, int y) {
addBefore(header, x, y);
/*header.prev.next = new Cell(s, header, header.prev);
header.prev = header.prev.next;
*/
}
public void addFirst(int x, int y) {
addBefore(header.next, x, y);
//header.next = new Cell (s, header.next, header);
}
public void delete(Cell p) {
p.next.prev = p.prev;
p.prev.next = p.next;
}
public void removeLast() {
// if (isEmpty()) throw new RuntimeException("Deleting from empty Snake");
delete(header.prev);
}
public boolean isEmpty() {
return header.next == header;
}
public int getX(Cell p) {
if (p!=null)
return p.x;
else return -1;
}
public int getY(Cell p) {
if (p!=null)
return p.y;
else return -1;
}
}