215k views
2 votes
10 peints) Given the Pceitional Liat clak belaw (implemented with a doubly linked liat), cemplete its resove. areliod. Yoo are givea 12 blank lise (numbered below) to mier your oode, which is mare than is neveded. public elaad Linkedponitionalliatco inpleaantin Poaitionalisate ( private atatic claaa Nodecp Iaplenenta Poeitioncto i 1/ Full Iaplesentation detaile caiteed: andy public function signaturea are 1 sated pablie Node (s. Nodese P, Node(// code oniteed ∗/) publie E getelapent () throvs Il1ogalstateErcoption ( / code opitced */ ) pablie Nadecs getprev( ) (/e code omitted */) publie Nodeeb getVext ()(/ e code oeited * ) public vold netElenest (E o) (/∗ code onitted //) pubiic void aetPrev (Nodof/4 code oniteed //5 private NodecE header: private Nodece trailer; private int aize =0t If You may use thia validate function. You don't need to ispleaent thia funetion. 1. Implenent thi function in Java public E remove (PositioncE> p) throus I1logalArgumentExceptica f

1 Answer

5 votes

Final answer:

To complete the implementation, you need to implement the remove function in Java. In the remove function, you will be given a Position object p and you need to remove the element at the position p from the list.

Step-by-step explanation:

The given code is a partial implementation of a positional list implemented with a doubly linked list. The code contains various functions such as getElement, getPrev, getNext, addElement, and setPrev. To complete the implementation, you need to implement the remove function in Java.

In the remove function, you will be given a Position object p. You need to remove the element at the position p from the list.

Here is a possible implementation of the remove function:

public E remove(Position<E> p) throws IllegalArgumentException {
if (!(p instanceof Node<E>)) {
throw new IllegalArgumentException("Invalid Position object");
}
Node<E> node = (Node<E>) p;
E element = node.getElement();
Node<E> prevNode = node.getPrev();
Node<E> nextNode = node.getNext();
prevNode.setNext(nextNode);
nextNode.setPrev(prevNode);
node.setNext(null);
node.setPrev(null);
node.setElement(null);
size--;
return element;
}
User Pritesh Mhatre
by
7.3k points