Question: client (5 Points) Write a method add that appends the given value to the end of the list. Also write a toString method that returns

(5 Points) Write a method add that appends the given value to the end of the list. Also write a toString method that returns a string, in the form of "data1 ->data2 -> ... > dataN -> null" when the list is not empty and returns "11" when the list is empty (10 Points) Write a method delete 2ndBack that deletes the 2nd to last value (from the back of the list) and returns the deleted value. If the list is empty or the size is less than 2 your method should throw a NoSuchElementException For example, suppose that a variable list is as follows: front-> 1->2 -> 3 -> 4 -> null the call delete 2ndBack(); should rearrange the list as follows and return 3: front-> 1 -> 2 -> 4 -> null Then the 2nd call delete 2ndBack(): should rearrange the list as follows and return 2: front-> 1 -> 4 -> null The 3rd call delete 2ndBack(): should rearrange the list as follows and return 1: front-> 4 -> null Finally the 4th delete2ndBack(); should not rearrange the list and throw a NoSuchElementException, list should be as follows: front-> 4 -> null Add the methods to the LinkedintList class we have worked in class. You may not call any other methods of the class to solve this problem, you may not construct any new nodes, and you may not use any auxiliary data structures to solve this problem (such as array, ArrayList.Queue. String, etc.). You also may not change any data fields of the nodes. You must solve this problem by rearranging the links of the list. 1 public class LinkedIntListclienti 2 public static void main(String [] args) { 3 LinkedIntList list = new LinkedIntList(); System.out.println ("Adding 1 -> 2 -> 3 -> 4 -> null to the list... " ); list.add (1); list.add (2); list.add(3); 8 list.add (4); System.out.println (list); 10 System.out.println("calling delete2ndBack... "); 11 list.delete2ndBack(); 12 System.out.println (list); 13 System.out.println("calling delete2ndBack... "); 14 list.delete2ndBack(); 15 System.out.println (list); 16 System.out.println("calling delete2ndBack... "); 17 list.delete2ndBack(); 18 System.out.println (list); 19 System.out.println("calling delete2ndBack... "); try{ 21 list.delete2ndBack(); 22 } 23 catch (Exception e) { 24 System.out.println (e); 25 } 26 System.out.println("Final List : + list); 27 } 28 L 20 NN 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
