1) General Description Create a new class called “XLinkedList”. This class defin
ID: 3819351 • Letter: 1
Question
1) General Description Create a new class called “XLinkedList”. This class defines an inner (private) class “Node” as the container for the “T obj” data. The main idea is to recode the normal methods of a linked list to recursive ones.
2) Data and Methods of XLinkedList:
1) Data: class Node, Node head & int size.
2) Methods:
a) Implement ALL methods in List Interface and add the following:
b) Default constructor: sets all data members to initial values.
c) Boolean search(T obj): a recursive method that searches the linkedlist for a passed obj and returns a true or a false if the obj was/not found.
d) double sum(): a recursive method that computes the summation of the linkedlist.
e) void clear(): a recursive method that clears the entire linkedlist. Do not use the short code: “head = null;”.
f) void printLeft():a recursive method that prints all objs in the linkedlist left to right.
g) void printRight():a recursive method that prints all objs in the linkedlist right to left.
3) Add a new interface “Measurable” with these methods:
a) public double measure();
4) Modify the Thing class to implement the Measurable interface in (3) above.
5) Add a Driver/Demo/Test class to test ALL methods of XLinkedList.
6) Fill XLinkedList with at least 16 objs.
Please make sure that the code is on a level that I can understand, and that no high-level coding techniques are used (I am aware that those might make it simpler to implement, but I will not be able to understand any of it)
If you could please just write the full code, and add in explanations using "//" that would be amazing. Need to make sure the code runs if I were to just re-write it in java.
I asked this same question about a week ago but got an incorrect answer, could really use this ASAP! my exam is very close. Thank you in advance!!
Explanation / Answer
Below code contains the answer of intial 5 subparts of the question though it was having multiple parts. Main intent of the question was recursive methods. Below code contains all the recursive methods.
Hope this will help you understand how recusive method works. If you have queries please leave the comment below.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/**
* XLinkedList.java
*
* @author arunveersingh
*
* @param <T>
*/
public class XLinkedList<T> implements List<T> {
// size of the list
private int size = 0;
// Node head
private Node<T> head = null;
// Default contructor to set data members to initial values.
public XLinkedList(int size) {
ArrayList<Node<T>> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(new Node<T>(null, null, null));
}
for (int i = 0; i < size - 1; i++) {
list.get(i).next = list.get(i + 1);
list.get(i + 1).prev = list.get(i);
}
}
// a recursive method that searches the linkedlist for a passed obj and
// returns a true or a false if the obj was/not found
boolean search(T obj) {
if (head == null) {
return false;
}
return search(head, obj);
}
boolean search(Node<T> node, T obj) {
if (node == null) {
return false;
}
if (node.item == obj) {
return true;
} else {
search(node.next, obj);
}
return false;
}
// a recursive method that computes the summation of the linkedlist
double sum() {
return sum(head);
}
double sum(Node<T> node) {
if (node == null) {
return 0;
}
return (double) node.item + sum(node.next);
}
// a recursive method that clears the entire linkedlist - this method comes
// from Interface List
@Override
public void clear() {
clear(head);
}
void clear(Node<T> node) {
if (node != null) {
Node<T> nextNode = node.next;
node = null;
clear(nextNode);
}
}
// a recursive method that prints all objs in the linkedlist left to right
void printLeft() {
Node<T> lastNode=null;
while (head.next != null) {
lastNode = head.next;
}
printLeft(lastNode);
}
void printLeft(Node<T> lastNode) {
if (lastNode == null) {
return;
} else {
printRight(lastNode.prev);
System.out.println(lastNode.item);
}
}
// a recursive method that prints all objs in the linkedlist right to left.
void printRight() {
printRight(head);
}
void printRight(Node<T> firstNode) {
if (firstNode == null) {
return;
} else {
printRight(firstNode.next);
System.out.println(firstNode.item);
}
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
// if head is pointing to null, it means XLinkedList is empty.
return head == null;
}
@SuppressWarnings("unchecked")
@Override
public boolean contains(Object o) {
return search((T) o);
}
@Override
public Iterator<T> iterator() {
// TODO
return new LinkedList<T>().iterator();
}
@Override
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<T> x = head; x != null; x = x.next)
result[i++] = x.item;
return result;
}
@Override
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<T> x = (Node<T>) head; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
@Override
public boolean add(T e) {
return this.add(e);
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends T> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public T get(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public T set(int index, T element) {
return null;
}
@Override
public void add(int index, T element) {
// TODO Auto-generated method stub
}
@Override
public T remove(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public int indexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int lastIndexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public ListIterator<T> listIterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public ListIterator<T> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
/**
* Private Inner Class Node
*
* @author arunveersingh
*
* @param <T>
*/
private static class Node<T> {
T item;
Node<T> next;
Node<T> prev;
Node(Node<T> prev, T element, Node<T> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.