Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help please! (Include DList.java in your solution zip archive) Here is the Java

ID: 3796148 • Letter: H

Question

Help please!

(Include DList.java in your solution zip archive) Here is the Java implementation of three useful methods (which are not currently in Dlist).

/**

* Removes the head node from this DList. It would be inadvisable to call this method on an

* empty list because we do not check for that condition. Returns the data stored in the head

* node. */

protected Integer removeHead() {

Integer data = getHead().getData();

if (getSize() == 1) {

setHead(null); setTail(null); }

else {

getHead().getNext().setPrev(null);

setHead(getHead().getNext());

}

setSize(getSize() - 1);

return data;

}

/** * Removes an interior node pNode from this DList. It would be inadvisable to call this method

* when pNode is null because we do not check for that condition. Returns the data stored in

* pNode. */

protected Integer removeInterior(Node pNode) {

Integer data = pNode.getData();

pNode.getPrev().setNext(pNode.getNext());

pNode.getNext().setPrev(pNode.getPrev());

setSize(getSize() - 1); return data;

}

/** * Removes the tail node from this DList. It would be inadvisable to call this method on an

* empty list because we do not check for that condition. Returns the data stored in the tail

* node. */

protected Integer removeTail() {

Integer data = getTail().getData();

if (getSize() == 1) {

setHead(null);

setTail(null);

}

else {

getTail().getPrev();.setNext(null);

setTail(getTail().getPrev());

}

setSize(getSize() - 1);

return data;

}

Using these three methods, rewrite the provided remove(index) method to make the code in that method simpler and more readable (my new and improved remove() method is half-a-dozen lines of code).

Be sure to run the test cases in DListTest to ensure that your new remove() method still works correctly. Also, make sure remove() throws an IndexOutOfBoundsException if pIndex is less than 0 or greater than or equal to getSize().

Explanation / Answer

/* program displaying removing head node interior node and tail node1*/

protected Integer rh() {
Integer data = get().getData();
if (getSize() == 1) {
set(null); setTail(null); }
else {
get().getNext().setPrev(null);
set(get().getNext());
}
setSize(getSize() - 1);
return data;
}

protected Integer ri(Node pNode) {
Integer data = pNode.getData();
pNode.getPrev().setNext(pNode.getNext());
pNode.getNext().setPrev(pNode.getPrev());
setSize(getSize() - 1); return data;
}

protected Integer rt() {
Integer data = get().getData();
if (getSize() == 1) {
set(null);
set(null);
}
else {
get().getPrev();.setNext(null);
set(get().getPrev());
}
setSize(getSize() - 1);
return data;
}