Question: LinkedCollection: import support.LLNode; public class LinkedCollection implements CollectionInterface { protected LLNode head; // head of the linked list protected int numElements = 0; // number

 LinkedCollection: import support.LLNode; public class LinkedCollection implements CollectionInterface { protected LLNode

LinkedCollection:

import support.LLNode; public class LinkedCollection implements CollectionInterface { protected LLNode 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 location; // node containing target, if found protected LLNode previous; // node preceding location public LinkedCollection() { numElements = 0; head = null; } public boolean add(T element) // Adds element to this collection. { LLNode 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 } } 

LLNode:

public class LLNode { protected LLNode 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 link){this.link = link;} public LLNode getLink(){ return link;} } 

CollectionInterface:

public interface CollectionInterface { boolean add(T element); // Attempts to add element to this collection. // Returns true if successful, false otherwise.

T get(T target); // Returns an element e from this collection such that e.equals(target). // If no such e exists, returns null.

boolean contains(T target); // Returns true if this collection contains an element e such that // e.equals(target); otherwise returns false.

boolean remove (T target); // Removes an element e from this collection such that e.equals(target) // and returns true. If no such e exists, returns false.

boolean isFull(); // Returns true if this collection is full; otherwise, returns false.

boolean isEmpty(); // Returns true if this collection is empty; otherwise, returns false. int size(); // Returns the number of elements in this collection. }

30. Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by ac- cessing the internal variables of the LinkedCollection, not by calling the previ- ously defined methods of the class. a. String toString () creates and returns a string that correctly represents the current collection. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stored element already provides its own reasonable toString method. int count (T target) returns a count of the number of elements e in the collection such that e. equals (target) is true. void removeA11 (T target) removes all elements e from the collection such that e.equals (target) is true. b. c

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!