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

You will need to complete two of the methods which are needed to add data to the

ID: 3863304 • Letter: Y

Question

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

You do not need any additional instance variables and so should not be creating any.

------------------------------------------------------------------------------------------------------------------

package edu.buffalo.cse116;

/**
* This defines a few basic methods in a doubly linked-based List. This is now much closer to being a working List
* implementation. Students will be writing the code to demonstrate they understand how to add nodes to a doubly linked
* list.
*
* @author Matthew Hertz
* @param <E> Type of data held in this collection
*/
public class DoublyLinkedList<E> {

/** Reference to the first node in our linked list. This will be null if the list is empty. */
private Entry<E> head;

/** Reference to the last node in our linked list. This will be null if the list is empty. */
private Entry<E> tail;

/**
* This size instance variable specifies the number of elements in the List. We could instead recompute this as
* needed, but adding it costs a little space and makes our code much more efficient. This space v. time tradeoff is a
* common issue in computer science.
*/
private int size;

private Entry<E> ans;

/**
* Creates an empty list.
*/
public DoublyLinkedList() {
reset();
}

/**
* This method, which is only used within the DoublyLinkedList class, returns the instance to its initial state. This
* call will reset both head and tail to be null and sets the size to be 0.
*/
private void reset() {
head = null;
tail = null;
size = 0;
}

/**
* 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<E> prior, E elem, Entry<E> follower) {
  
  
}

/**
* 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) {
  
}

/**
* Returns the number of elements currently in this list.
*
* @return the number of elements in the list
*/
public int size() {
return size;
}
}

Explanation / Answer

Hi I have written the required methods.

Please note that the code you given here is not enough for compiling however enough to write logic for req methods.

I assumed below methods or similar functionality methods in Entry<E> class.

getNext() -> will return next node

getPrev()->will return pre node

getData()->will return data of respective node

setNext()=>set next value to node

setPrev()=> set prev data to node

Please find below the required methods:

private void addToFront(E elem) {
//Creating the new Node
ans=new Entry(elem);
//checking first if haed is null(list is empty)
if(head==null){
ans=head;
   tail=head;
   size++;
   return;
}
//setting the next data of new node to head
ans.setNext(head);
//setting the pre data of head to new Node
head.setPrev(ans);
//changing the head to new node
head=ans;
//updating tail data
tail=ans;
size++;
}

private void addBetween(Entry<E> prior, E elem, Entry<E> follower) {
//creating new node
ans=new Entry(elem);
//takes two temp variable and assigned to head
Entry<E> temp1=head;
Entry<E> temp2=head;
   //iterating through list until tail element found
while(temp1!=tail){
       //if temp1 node data matches to prior data
       if(temp1.getData().equals(prior.getData())){
       //then it will copy the prior data to temp2
           temp2=temp1;
           break;
       }
       //it will go next untill data found or not
       temp1=temp1.getNext();
}
//here setting the new node next ref to follwer node
ans.setNext(temp2.getNext());
//setting ref prev node of new Node to prior node
ans.setPrev(temp2);
//setting ref next node of prior node to new node
temp2.setNext(ans);
//setting ref follower pre node to new node
temp2.getNext().setPrev(ans);
//increassing the size
size++;
}

in case of any doubt please do comment

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote