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

add two new methods named removeLast and addLast inside the SinglyLinkedList Mov

ID: 3601419 • Letter: A

Question

add two new methods named removeLast and addLast inside the SinglyLinkedList Movie class to remove and the last element in the linkedlist. Please don't create a new linkedlist, use the one below.

class Node<E> {

   E data;

   Node<E> next;

  

   Node(E data, Node next) {

       this.data = data;

       this.next = next;

   }

   public String toString() {

       return data + "";

   }

}

package test3;

import java.util.Scanner;

public class Movies {

   public static void main(String[] args) {

       Node front = null;

       System.out.println(front);

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter a movie name");

       String movie = sc.nextLine();

       front = addToFront(front, movie);

       System.out.println(front);

       front = removeFirst(front);

       System.out.println(front);

      

   }

   public static Node addToFront(Node front, String movie) {

       Node temp = new Node(movie, null);

       temp.next = front;

       front = temp;

       return front;

   }

   public static Node removeFirst(Node front) {

       if(front != null) {

       return front = front.next;

       }

       else {

       return front;

       }

   }

}

Explanation / Answer

Pleaase find my implementation.

import java.util.Scanner;

public class Movies {

   public static void main(String[] args) {

       Node front = null;

       System.out.println(front);

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter a movie name");

       String movie = sc.nextLine();

       front = addToFront(front, movie);

       System.out.println(front);

       front = removeFirst(front);

       System.out.println(front);

   }

   public static Node addToFront(Node front, String movie) {

       Node temp = new Node(movie, null);

       temp.next = front;

       front = temp;

       return front;

   }

   public static Node removeFirst(Node front) {

       if(front != null) {

           return front = front.next;

       }

       else {

           return front;

       }

   }

   public static Node removeLast(Node front) {

       if(front == null || front.next == null)

           return null;

       Node curr = front;

       while(curr.next.next !=null)

           curr = curr.next;

       // removing last

       curr.next = null;

       return front;

   }

  

   public static Node addLast(Node front, String movie) {

      

       Node temp = new Node(movie, null);

      

       if(front == null)

           return temp;

       Node curr = front;

       while(curr.next != null)

           curr = curr.next;

      

       curr.next = temp;

       return front;

   }

}

class Node<E> {

   E data;

   Node<E> next;

   Node(E data, Node next) {

       this.data = data;

       this.next = next;

   }

   public String toString() {

       return data + "";

   }

}