Question: In class MyLinkedList.java please fill in the missing parts in a recursive way public class MyLinkedList2 { protected Element head; protected Element tail; public int

In class MyLinkedList.java please fill in the missing parts in a recursive way

public class MyLinkedList2 { protected Element head; protected Element tail; public int length(){ return recursiveLength(head); } private int recursiveLength(Element element){ // your code should be written here } public String toString(){ return recursiveToString(head); } public String recursiveToString(Element e) { // your code should be written here } public Element find(Object obj){ return recFind(obj, head); } private Element recFind(Object o , Element e) { // your code should be here } public void purge(){ head = null; tail = null; } public Element getHead(){ return head; } public Element getTail(){ return tail; } public boolean isEmpty(){ return head == null; } public Object getFirst(){ if(head == null) throw new IllegalArgumentException(); else return head.data; } public Object getLast(){ if(tail == null) throw new IllegalArgumentException(); else return tail.data; } public void prepend(Object obj){ Element element = new Element(obj, head); if(head == null) tail = element; head = element; } public void append(Object obj){ Element element = new Element(obj, null); if(head == null) head = element; else tail.next = element; tail = element; } public void assign(MyLinkedList2 linkedlist){ if(linkedlist != this) { purge(); Element element = linkedlist.head; while (element != null) { append(element.data); element = element.next; } } } public void extract(Object obj){ Element element = head; Element lastElement = null; while(element != null && ! element.data.equals(obj)){ lastElement = element; element = element.next; } if(element == null) throw new IllegalArgumentException("item not found"); if(element == head) head = element.next; else lastElement.next = element.next; if(element == tail) tail = lastElement; } public void extractFirst(){ if(head == null) throw new IllegalArgumentException("item not found"); head = head.next; if(head == null) tail = null; } public void extractLast() { if(tail == null) throw new IllegalArgumentException("item not found"); if(head == tail) head = tail = null; else { Element previous = head; while (previous.next != tail) previous = previous.next; previous.next = null; tail = previous; } } public final class Element{ Object data; Element next; Element(Object obj, Element element){ data = obj; next = element; } public Object getData(){ return data; } public Element getNext(){ return next; } public void insertAfter(Object obj){ next = new Element(obj, next); if(this == tail) tail = next; } public void insertBefore(Object obj){ Element element = new Element(obj, this); if(this == head){ head = element; return; } Element previousElement = head; while(previousElement != null && previousElement.next != this){ previousElement = previousElement.next; } previousElement.next = element; } public void extract(){ Element element = null; if(this == head) head = next; else{ element = head; while(element != null && element.next != this){ element = element.next; } if(element == null) throw new IllegalArgumentException(); element.next = next; } if(this == tail) tail = element; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!