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: 3863074 • 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

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

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


package listclasses;
public class doublelinkedlist {
   doublelinkedlist privious;
   doublelinkedlist next;
   int data;
   public doublelinkedlist(int data){
      
       this.data=data;
   }
public    doublelinkedlist(doublelinkedlist privious,int data,doublelinkedlist next){
       this.privious=privious;
       this.data=data;
       this.next=next;
   }
      
   public String tostring(){
       return data+"";
   }
}


linkedlistmain.java

package listclasses;
import java.util.*;
public class linkedlistmain {
   static doublelinkedlist head;
   static int size=0;
   static {
       head=null;
       size=0;
      
   }
   public static void main(String[] args) {
   insertfront(10);
   // display();
   insertfront(20);
   insertfront(30);
        
   insertanywhere(70,2);
   insertfront(101);
   insertrear(550);
     
        
   display();
  
   }
public static void insertfront(int data){
       if(head==null){
           head=new doublelinkedlist(null,data,null);
       }
           else{
               doublelinkedlist newlist=new doublelinkedlist(null,data,head);
               head.privious=newlist;
           head=newlist;
           }
          
       size++;
   }
   public static void insertrear(int data){
       if(head==null)
           head=new doublelinkedlist(null,data,null);
       else{
           doublelinkedlist current;
           current=head;
           while(current.next!=null){
               current=current.next;
           }
           doublelinkedlist newlist=new doublelinkedlist(current,data,null);
           current.next=newlist;
       }
       size++;
      
   }
public static void insertanywhere(int data,int index){
       int i=1;
       if(head==null)
           return;
       if( index<1 || index>size)
           return;
       doublelinkedlist curent;
       curent=head;
       while(i<index){
           curent=curent.next;
           i++;
          
       }
       if(curent.privious==null){
           doublelinkedlist newlist=new doublelinkedlist(null,data,curent);
           curent.privious=newlist;
           head=newlist;
           size++;
       }
       else
       {
           doublelinkedlist newlist=new doublelinkedlist(curent.privious,data,curent);
           curent.privious.next=newlist;
           curent.privious=newlist;
           size++;
       }
      
   }
   public static void display(){
       while(head!=null){
           System.out.println(head.data);
           head=head.next;
       }
       System.out.println("size:"+size);
   }
   public static void isempty(){
       if(size<=0)
           System.out.println("empty");
       else
           System.out.println("not empty:");
      
   }
}
output:
101
30
70
20
10
550
size:6
size:6

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