This commit is contained in:
Jacob Signorovitch
2024-12-03 15:47:18 -05:00
parent c5f494f55f
commit fd2b17ed0f

View File

@@ -17,26 +17,20 @@ class Examples {
} }
// A doubly-linked list element. // A doubly-linked list element.
abstract class ANode<PointsTo> { abstract class ANode<T> {
PointsTo nxt; // Next element. ANode<T> nxt; // Next element.
PointsTo pre; // Previous element. ANode<T> pre; // Previous element.
ANode(PointsTo nxt, PointsTo pre) { ANode(ANode<T> nxt, ANode<T> pre) {
this.nxt = nxt; this.nxt = nxt;
this.pre = pre; this.pre = pre;
} }
}
// Self-referential node. void assignNode(ANode<T> nxt, ANode<T> pre) {
abstract class ASRefNode { throw new UnsupportedOperationException(
"Can't call assignNode() on non-Node."
ASRefNode nxt; );
ASRefNode pre;
ASRefNode() {
this.nxt = this;
this.pre = this;
} }
} }
@@ -52,8 +46,10 @@ class Deque<A> {
class Sentinel<T> extends ANode<ANode<T>> { class Sentinel<T> extends ANode<ANode<T>> {
Sentinel() { Sentinel() {
// To create an empty list, this Sentinel points to itself.
super(null, null); super(null, null);
this.nxt = this; this.nxt = this;
this.pre = this;
} }
} }
@@ -65,4 +61,15 @@ class Node<T> extends ANode<Node<T>> {
super(null, null); super(null, null);
this.val = val; this.val = val;
} }
Node(T val, ANode<T> nxt, ANode<T> pre) {
this(val);
this.assignNode(nxt, pre);
}
void assignNode(ANode<T> nxt, ANode<T> pre) {
throw new UnsupportedOperationException(
"Can't call assignNode() on non-Node."
);
}
} }