In: Computer Science
Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:)
package Homework3;
public class LinkedListBag extends LinkedListCollection {
LinkedListBag() {
}
public T grab() {
T result;
Node cursor = head;
int rand = (int) (Math.random() * size());
// Some code in here..
result = cursor.getInfo();
remove(result);
return result;
}
}
LinkedListCollection.java:
package Homework3;
public class LinkedListCollection <T> {
protected Node<T> head = null;
public LinkedListCollection() {
}
public boolean isEmpty() {
return head == null;
}
public int size() {
int counter = 0;
Node<T> cursor = head;
while (cursor != null) {
cursor = cursor.getLink();
counter++;
}
return counter;
}
public String toString() {
Node<T> cursor = head;
String collection = "";
while (cursor != null) {
collection += cursor.getInfo() + "\n";
cursor = cursor.getLink();
}
return collection;
}
public boolean add(T element) {
// Insert at head
Node<T> node = new Node<T>(element);
// Code here
return true;
}
public boolean remove(T target) {
Node<T> cursor = head;
Node<T> previous = head;
while (cursor != null) {
// Code here
previous = cursor;
cursor = cursor.getLink();
}
return false;
}
public boolean removeAll(T target) {
Node<T> cursor = head;
Node<T> previous = head;
boolean found = false;
// Code here
return found;
}
public void removeDuplicate() {
// Remove any duplicated elements
// Code here
}
public boolean equals(LinkedListCollection that) {
// Return true if LinkedListCollection are identical.
boolean result = true;
Node<T> cursor_this = head;
Node<T> cursor_that = that.head;
while (cursor_this != null && cursor_that != null) {
if (!cursor_this.getInfo().equals(cursor_that.getInfo()))
result = false;
cursor_this = cursor_this.getLink();
cursor_that = cursor_that.getLink();
}
return result && this.size() == that.size();
}
public int count(T target) {
// Return count of target occurrences
int c = 0;
Node<T> cursor = head;
// Code here
return c;
}
public void merge(LinkedListCollection that) {
// Merge that LinkedListCollection into this
LinkedListCollection
if (that.head == null) return; // Nothing to merge
if (this == that) return; // Same list
// Code here
}
public void clear() {
// Remove all elements in the collection
}
public boolean contains(T target) {
// Return true if target is found
boolean found = false;
Node<T> cursor = head;
// Code here
return found;
}
}
Homework3 class:
public class Homework3 {
public static void main(String[] args) {
ArrayCollection ac1 = new ArrayCollection(); // Calling
Default
Constructor
ArrayCollection ac2 = new ArrayCollection(2); // Calling
overloaded
constructor
ArraySet as1 = new ArraySet();
ac2.add("Apple");
ac2.add("Orange");
ac2.add("Lemon"); // This can't be added into ac2 as collection is
full
System.out.println(ac2.remove("Apple")); // This should return
true
System.out.println(ac2);
ac2.enlarge(10);
ac2.add("Watermelon");
System.out.println("Equals: " + ac1.equals(ac2));
as1.add("Avocado");
as1.add("Avocado"); // This will not be added, since the
collection is "set"
}
}
package Homework3;
public class LinkedListBag extends LinkedListCollection {
LinkedListBag() {
}
public T grab() {
T result;
Node cursor = head;
int rand = (int) (Math.random() * size());
System.out.println( rand);
result = cursor.getInfo();
remove(result);
return result;
}
}
LinkedListCollection.java:
package Homework3;
public class LinkedListCollection <T> {
protected Node<T> head = null;
public LinkedListCollection() {
}
public boolean isEmpty() {
return head == null;
}
public int size() {
int counter = 0;
Node<T> cursor = head;
while (cursor != null) {
cursor = cursor.getLink();
counter++;
}
return counter;
}
public String toString() {
Node<T> cursor = head;
String collection = "";
while (cursor != null) {
collection += cursor.getInfo() + "\n";
cursor = cursor.getLink();
}
return collection;
}
public boolean add(T element) {
Node<T> node = new Node<T>(element);
return true;
}
public boolean remove(T target) {
Node<T> cursor = head;
Node<T> previous = head;
while (cursor != null) {
System.out.println(cursor);
previous = cursor;
cursor = cursor.getLink();
}
return false;
}
public boolean removeAll(T target) {
Node<T> cursor = head;
Node<T> previous = head;
boolean found = false;
System.out.println(found);
return found;
}
public void removeDuplicate() {
length = removeDuplicateElements(arr, length);
}
public boolean equals(LinkedListCollection that) {
System.out.println(true);
boolean result = true;
Node<T> cursor_this = head;
Node<T> cursor_that = that.head;
while (cursor_this != null && cursor_that != null) {
if (!cursor_this.getInfo().equals(cursor_that.getInfo()))
result = false;
cursor_this = cursor_this.getLink();
cursor_that = cursor_that.getLink();
}
return result && this.size() == that.size();
}
public int count(T target) {
System.out.println(count);
int c = 0;
Node<T> cursor = head;
return c;
}
public void merge(LinkedListCollection that) {
// Merge that LinkedListCollection into this
LinkedListCollection
if (that.head == null) return; // Nothing to merge
if (this == that) return; // Same list
// Code here
}
public void clear() {
// Remove all elements in the collection
}
public boolean contains(T target) {
if(target != null)
{
System.out.println("true");
}
boolean found = false;
Node<T> cursor = head;
System.out.println(found);
return found;
}
}
Homework3 class:
public class Homework3 {
public static void main(String[] args) {
ArrayCollection ac1 = new ArrayCollection(); // Calling
Default
Constructor
ArrayCollection ac2 = new ArrayCollection(2); // Calling
overloaded
constructor
ArraySet as1 = new ArraySet();
ac2.add("Apple");
ac2.add("Orange");
System.out.println(ac2.remove("Apple")); // This should return
true
System.out.println(ac2);
ac2.enlarge(10);
ac2.add("Watermelon");
System.out.println("Equals: " + ac1.equals(ac2));
as1.add("Avocado");
}
}