rgs LinkedCollection fruits = new LinkedCollection veggies = new LinkedCollection all = fruits.combine(veggies); System.out.println("Combine all" +all.toString()); +veggies.toString());
Explain why the code below does not run when using the test driver shown in screen shot. i also included the LLNode class and collectioninterface interface
public class LinkedCollection<T> implements CollectionInterface<T>
{
protected LLNode<T> head; // head of the linked list
protected int numElements = 0; // number of elements in this collection
// set by find method
protected boolean found; // true if target found, else false
protected LLNode<T> location; // node containing target, if found
protected LLNode<T> previous; // node preceding location
public LinkedCollection()
{
numElements = 0;
head = null;
}
public boolean add(T element)
// Adds element to this collection.
{
LLNode<T> newNode = new LLNode<>(element);
newNode.setLink(head);
head = newNode;
numElements++;
return true;
}
protected void find(T target)
// Searches the collection for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
location = head;
found = false;
while (location != null)
{
if (location.getInfo().equals(target)) // if they match
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}
public int size()
// Returns the number of elements on this collection.
{
return numElements;
}
public boolean contains (T target)
// Returns true if this collection contains an element e such that
// e.equals(target); otherwise, returns false.
{
find(target);
return found;
}
public boolean remove (T target)
// Removes an element e from this collection such that e.equals(target)
// and returns true; if no such element exists, returns false.
{
find(target);
if (found)
{
if (head == location)
head = head.getLink(); // remove first node
else
previous.setLink(location.getLink()); // remove node at location
numElements--;
}
return found;
}
public T get(T target)
// Returns an element e from this collection such that e.equals(target);
// if no such element exists, returns null.
{
find(target);
if (found)
return location.getInfo();
else
return null;
}
public boolean isEmpty()
// Returns true if this collection is empty; otherwise, returns false.
{
return (numElements == 0);
}
public boolean isFull()
// Returns true if this collection is full; otherwise, returns false.
{
return false; // Linked implementation is never full
}
@Override
public String toString() {
//creates and returns a string that correctly represents the current collection
String result = "";
LLNode<T> cursor= head;
while (cursor!= null) {
result += cursor.getInfo() + "\n";
cursor = cursor.getLink();}
return result;
}
public int count(T target){
//return a count of the number of elements e in the collection such that e.equals(target) is true
int count = 0;
LLNode<T> temp = head;
while(temp != null){
if(temp.getInfo().equals(target))
count++;
}
return count;
}
public void removeAll(T target){
//remove all elements e from the collection such that e.equal(T target) is true
LLNode<T> temp = previous;
while(temp != null){
if(temp.getInfo().equals(target))
temp.setLink(temp.getLink());
else
temp = temp.getLink();
remove(target);
}
}
LinkedCollection<T>combine(LinkedCollection<T> other) {
//creates and returns a new LinkedCollection object that is a combination this object and argument object
int index1 = 0, index2 = 0, index3 = 0;
int size1 = size(), size2 = other.size();
LinkedCollection<T> col = new LinkedCollection<>(size1 + size2);
while(index1 < size1 && index2 < size2) {
if(((Comparable)(elements[index1])).compareTo(other.elements[index2]) < 0)
col.add(elements[index1++]);
else
col.add(other.elements[index2++]);
}
while(index1 < size1)
col.add(elements[index1++]);
while(index2 < size2)
col.add(other.elements[index2++]);
return col;
}
}
public class LLNode<T>
{
protected LLNode<T> link;
protected T info;
public LLNode(T info)
{
this.info = info;
link = null;
}
public void setInfo(T info){
this.info = info;
}
public T getInfo(){
return info; }
public void setLink(LLNode<T> link){
this.link = link;
}
public LLNode<T> getLink(){
return link;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps