I am trying exercises from a book on programming and am stuck trying to create a doubly linked list subclass that inherits from a circularly linked superclass. The subclass has a nested class for a list node that separately inherits from a list node in the superclass.
For example:
public Superclass {
protected Node<E> {
E element
Node<E> next
public Node(E e, Node<E> n) {
element = e
next = n
}
// access/update methods such as getElement, getNext, set Next ...
}
protected Node<E> tail
protected int size
// access methods including size(), isEmpty(), first(), last() ...
// update methods including rotate(), addFirst(E e), addLast(E e), removeFirst() ...
// The code for removeFirst which is not overridden is:
// if list not empty:
// Node<E> head = tail.getNext()
// if (head == tail) {
// tail = null
// } else {
// tail.setNext(head.getNext())
// }
// size--
}
And:
public Subclass extends Superclass {
protected InheritedNode<E> extends Superclass.Node {
private InheritedNode prev
public InheritedNode(E e, Node n, InheritedNode p) {
super(e, n)
prev = p
}
// access/update methods including getPrev() and setPrev(InheritedNode<E> p) { prev = p }
}
// Override Superclass method
public void addFirst(E e) {
Create a new InheritedNode if list is null and set prev and next to self
Or
addBetween(e, (InheritedNode<E>) tail, (InheritedNode<E>) tail.getNext())
}
// private update method
private void addBetween(E e, InheritedNode<E> predecessor, InheritedNode<E> successor) {
InheritedNode<E> newest = new InheritedNode<E>(e, predecessor, successor)
predecessor.setNext(newest);
successor.setNeext(newest);
size++;
}
}
The addFirst and addBetween methods in the subclass were copied directly from a previous exercise to create a circular doubly linked list that ran fine, and the code calling the subclass method was unchanged:
public static void main(String[] args) {
Superclass Name = new Superclass();
Name.addFirst("A"); // This method was overridden
Name.addLast("B"); // This method was not overridden
Name.addLast("C");
// And code to print the list contents using the rotate() method
}
There is a problem as soon as the third node with “C” as the element is added that did not manifest in any previous exercise. The output reads as follows:
A
A B
B C B
If a fourth node is added using addLast(“D”), the output is:
C D C D
If the not-overridden removeFirst method from Superclass is called, the output is:
D D D
The list is losing all previous nodes besides the first two somewhere between the superclass and subclass, and I have been unable to determine why.
Using the debug feature of the IDE, I stopped execution in the Subclass addFirst method after the third node was added. The debug feature showed that only the immediate next and prev nodes had the correct elements. The sequence of nodes after the third node was C C B C B C B C … while the sequence of nodes before the third node was C B A C A C A …
chalk_eaux_2911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.