Java Homework please help comments are the requirements pack

Java Homework, please help! (comments are the requirements)

package hw3 public class LinkedList your private fields go here. It is highly recommended that you use a sentinel node at the front of the list to make the methods easier to implement create an empty list public LinkedList 0 solution here

Solution

HI, Please find my implementation.

Please let me know in case of any issue.

public class LinkedList {

   private ListNode head;

  

   public LinkedList() {

       head = new ListNode(null); // sentinel node

   }

  

   public Object remove(int i){

      

       ListNode temp = head;

       ListNode result = null;

       int count = 0;

       while(temp.getNext() != null && count < i){

           count++;

           temp = temp.getNext();

       }

      

       if(temp.next == null)

           return null;

       result = temp.getNext();

       temp.setNext(temp.getNext().getNext());

       return result;

   }

  

   public void append(Object d){

       ListNode temp = head;

       while(temp.getNext() != null){      

           temp = temp.getNext();

       }

      

       temp.setNext(new ListNode(d));

      

   }

  

   public Object get(int i){

       ListNode temp = head;

       ListNode result = null;

       int count = 0;

       while(temp.getNext() != null && count < i){

           count++;

           temp = temp.getNext();

       }

       if(temp.next == null)

           return null;

       result = temp.getNext();

       return result;

   }

  

   public void move(int i, int j){

       ListNode temp = head;

       ListNode ith = null, jth = null;

       int count = 0;

       while(temp.getNext() != null && count < i){

           count++;

           temp = temp.getNext();

       }

      

       ith = temp.getNext();

      

       temp = head;

       count = 0;

       while(temp.getNext() != null && count < j){

           count++;

           temp = temp.getNext();

       }

       jth = temp.getNext();

      

       Object data = ith.data;

       ith.setData(jth.getData());

       jth.setData(data);

   }

  

   public Object[] toArray(){

      

       ListNode temp = head;

       int count = 0;

       while(temp.getNext() != null){

           count++;

           temp = temp.getNext();

       }

      

       // creating an array of size count

       Object[] arr = new Object[count];

      

       temp = head;

       int i = 0;

       while(temp.getNext() != null){

           arr[i++] = temp.getNext().getData();

           temp = temp.getNext();

       }

      

       return arr;

   }

  

  

   private class ListNode{

       private Object data;

       private ListNode next;

      

       public ListNode(Object data){

           this.data = data;

           this.next = null;

       }

       public Object getData() {

           return data;

       }

       public ListNode getNext() {

           return next;

       }

       public void setData(Object data) {

           this.data = data;

       }

       public void setNext(ListNode next) {

           this.next = next;

       }

      

       @Override

       public String toString() {

           return data.toString();

       }

      

   }

}

import java.util.Arrays;

public class LinkedListTest {

  

   public static void main(String[] args) {

      

       LinkedList list = new LinkedList();

      

       System.out.println(Arrays.toString(list.toArray()));

      

       list.append(100);

       list.append(200);

       list.append(300);

      

       System.out.println(Arrays.toString(list.toArray()));

      

       System.out.println(\"Removed: \"+list.remove(1));

      

       System.out.println(Arrays.toString(list.toArray()));

      

       System.out.println(\"Get(3): \"+list.get(3));

       System.out.println(\"Get(3): \"+list.get(1));

      

       list.move(0, 1);

       System.out.println(Arrays.toString(list.toArray()));

      

       list.append(400);

       list.append(500);

       System.out.println(Arrays.toString(list.toArray()));

       list.move(0, 3);

       System.out.println(Arrays.toString(list.toArray()));

      

   }

}

/*

Sample run:

[]

[100, 200, 300]

Removed: 200

[100, 300]

Get(3): null

Get(3): 300

[300, 100]

[300, 100, 400, 500]

[500, 100, 400, 300]

*/

Java Homework, please help! (comments are the requirements) package hw3 public class LinkedList your private fields go here. It is highly recommended that you u
Java Homework, please help! (comments are the requirements) package hw3 public class LinkedList your private fields go here. It is highly recommended that you u
Java Homework, please help! (comments are the requirements) package hw3 public class LinkedList your private fields go here. It is highly recommended that you u
Java Homework, please help! (comments are the requirements) package hw3 public class LinkedList your private fields go here. It is highly recommended that you u
Java Homework, please help! (comments are the requirements) package hw3 public class LinkedList your private fields go here. It is highly recommended that you u
Java Homework, please help! (comments are the requirements) package hw3 public class LinkedList your private fields go here. It is highly recommended that you u

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site