-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDList.java
97 lines (87 loc) · 3.12 KB
/
DList.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
public class DList {
protected int size; // number of elements
protected DNode header, trailer; // sentinels
/** Constructor that creates an empty list */
public DList() {
size = 0;
header = new DNode(null, null, null); // create header
trailer = new DNode(null, header, null); // create trailer
header.setNext(trailer); // make header and trailer point to each other
}
/** Returns the number of elements in the list */
public int size() { return size; }
/** Returns whether the list is empty */
public boolean isEmpty() { return (size == 0); }
/** Returns the first node of the list */
public DNode getFirst() throws IllegalStateException {
if (isEmpty()) throw new IllegalStateException("List is empty");
return header.getNext();
}
/** Returns the last node of the list */
public DNode getLast() throws IllegalStateException {
if (isEmpty()) throw new IllegalStateException("List is empty");
return trailer.getPrev();
}
/** Returns the node before the given node v. An error occurs if v
* is the header */
public DNode getPrev(DNode v) throws IllegalArgumentException {
if (v == header) throw new IllegalArgumentException
("Cannot move back past the header of the list");
return v.getPrev();
}
/** Returns whether a given node has a previous node */
public boolean hasPrev(DNode v) {
return v != header;
}
/** Returns whether a given node has a next node */
public boolean hasNext(DNode v) {
return v != trailer;
}
/** Returns the node after the given node v. An error occurs if v
* is the trailer */
public DNode getNext(DNode v) throws IllegalArgumentException {
if (v == trailer) throw new IllegalArgumentException
("Cannot move forward past the trailer of the list");
return v.getNext();
}
/** Inserts the given node z before the given node v. An error
* occurs if v is the header */
public void addBefore(DNode v, DNode z) throws IllegalArgumentException {
DNode u = getPrev(v); // may throw an IllegalArgumentException
z.setPrev(u);
z.setNext(v);
v.setPrev(z);
u.setNext(z);
size++;
}
/** Inserts the given node z after the given node v. An error occurs
* if v is the trailer */
public void addAfter(DNode v, DNode z) {
DNode w = getNext(v); // may throw an IllegalArgumentException
z.setPrev(v);
z.setNext(w);
w.setPrev(z);
v.setNext(z);
size++;
}
/** Inserts the given node at the head of the list */
public void addFirst(DNode v) {
addAfter(header, v);
}
/** Inserts the given node at the tail of the list */
public void addLast(DNode v) {
addBefore(trailer, v);
}
/** Removes the given node v from the list. An error occurs if v is
* the header or trailer */
public void remove(DNode v) {
DNode u = getPrev(v); // may throw an IllegalArgumentException
DNode w = getNext(v); // may throw an IllegalArgumentException
// unlink the node from the list
w.setPrev(u);
u.setNext(w);
v.setPrev(null);
v.setNext(null);
size--;
}
}