Java language. included what we have as the SinglyLinkedList class
Write a syntatically correct, generic Java method named toString Using Nodes() that . is a member of the SinglyLinked List class, and returns a String of all of the elements in a SinglyLinkedList by traversing the list using a temporary Node reference. does not add any instance variables to the SinglyLinkedList class does not modify any of the methods presented in the textbook for the singlyLinkedList class hested use Java Dype parameter E Our implementation also take a private Node class within the lic SinglyLinked List class. Code Fragment 3.14 presents the Node de derlying details about nodes and links. This design also allows Janatos nested class provides strong encapsulation, shielding users of our class this node type from forms of nodes we may define for use in other stru. public class SinglyLinkedList
{ 2 - nested Node class 3 private static class Node { private E element 5 private Node next; public Node(E e, Node n) { // reference to the element stored // reference to the subsequent nodes 4 6 elemente next = n; } public EgetElement() { return element; } public Node getNext() { return next; } public void setNext(Node n) { next = n; } 13 } -- end of nested Node class ... rest of SinglyLinkedList class will follow Code Fragment 3.14: A nested Node class within the SinglyLinkedList diesel remainder of the SinglyLinked List class will be given in Code Fragment 8 9 10 11 12 2.2. Singl Linked Lists public class SinglyLinkedList { 127 (nested Node dass goes here) 14 1/instance variables of the SinglyLinkedlist 15 private Node head null 16 private Node tail = null; 1/ head node of the list for empty) 17 private int size = 0; 1/ last mode of the list of empty) 18 number of Bodes in the list public SinglyLinkedList() {} 19 // access methods 1/ constructs an initially emptylist 20 public int size) { return size) 21 public boolean isEmpty() { return size == 0; } 22 public E first // returns (but does not remove the first dement 23 if (isEmpty()) return null; 24 return head.getElement(): 25 } 26 public E last() { 1/ returns (but does not remove the last element 27 if (isempty()) return null; 28 return tail.getElement(): 29 } 30 // update methods 31 public void addFirst(E e) { Wadds elemente to the front of the list 32 head = new Node(e, head): /create and link a new node 33 if (size == 0) 34 tailhead, // special case: new node becomes tail also 3S size++ 36 37 public void addLast(E e) { V/ adds elemente to the end of the list 38 Node newest = new Nodele, null); // node will eventually be the tail 39 if (isEmpty) 40 head = newest: W/ special case previously empty list 41 else 42 tail.setNext(newest): new node after existing tail 43 tail = newest: new node becomes the tail 44 size++ 45 } 46 public E removeFirst() { // removes and returns the first element 47 if (isEmpty()) return null; // nothing to remove 48 E answer head.getElement(); 19 head head.getNext(); // will become null if list had only one node 50 size- 51 if (size =0) 52 tail = null; W special case as list is now empty 53 return answer, 54 55 ) Code Fragment 3.15: The SinglyLinkedList class definition (when combined with the nested Node class of Code Fragment 3.14)