Part III: Recursion Given the classes Node and SinglyLinkedList in this util.zip package, do the following changes: - Re

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Part III: Recursion Given the classes Node and SinglyLinkedList in this util.zip package, do the following changes: - Re

Post by answerhappygod »

Part Iii Recursion Given The Classes Node And Singlylinkedlist In This Util Zip Package Do The Following Changes Re 1
Part Iii Recursion Given The Classes Node And Singlylinkedlist In This Util Zip Package Do The Following Changes Re 1 (23.33 KiB) Viewed 37 times
Node.java
package util;
public class Node<E> { public E data; public Node<E> next;
@Override public String toString() { return data + ""; } public Node(E data, Node<E> next) { this.data = data; this.next = next; } public Node(E data) { this.data = data; this.next = null; }}
SinglyLinkedList.java
package util;
public class SinglyLinkedList<E> { public Node<E> head; //constructor #1 public SinglyLinkedList() { this.head = null;//creates an empty linkedlist! } //constructor #2 public SinglyLinkedList(Node<E> head) { this.head = head;//creates a linked list with agiven head! } @Override public String toString() { if (head == null)// base case return ""; String result = head + ""; if (head.next != null) result += " -> "; SinglyLinkedList<E> rest = newSinglyLinkedList<E>(head.next);
return result + rest.toString(); }}
Part III: Recursion Given the classes Node and SinglyLinkedList in this util.zip package, do the following changes: - Reimplement the toString method of SinglyLinkedList so that it uses recursion to generate the string representation of the linked list. - Using recursion, implement the size method that gets no parameter and returns the number of elements stored in the linked list.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply