Following code has the start of a class creating a doubly-linked list (but NOT i
ID: 3862626 • Letter: F
Question
Following code has the start of a class creating a doubly-linked list (but NOT implementing List). You will need to complete two of the methods which are needed to add data to the list:
addBetween() -- should add a new Entry ("node") containing elem so that it appears BETWEEN prior and follower
addToFront() -- should add a new Entry ("node") containing elem so that it appears AT THE HEAD of the list
______________________________________________________________________________________________________
Explanation / Answer
/** * Adds a new node to the middle of the linked list. This node will need to be created and then setup so that it * appears between the specified nodes already in the list. Finally, update any instance variable so that they reflect * the newly added node. * * @param prior Node that will come before the one being created in this method. * @param elem Element we are adding to the linked list * @param follower Node that comes after the one being created in this method. */ private void addBetween(Entry prior, E elem, Entry follower) { // create new Entry object with E elem as Entry entry = new Entry(elem); entry.prev = prior; //point prior next to elem prior.next = entry; //point Entry object next to follower entry.next = follower; follower.prev = entry; } /** * Adds a new node at the start of the linked list. This node will need to be created and then setup so that it comes * before the current head node. Finally, update any instance variable so that they properly reflect the addition of * this new first node. * * @param elem Element we are adding to the front of the linked list */ private void addToFront(E elem) { Entry entry = new Entry(elem); entry.next = head; head.prev = entry; entry.prev = null; }