Complete LinkedListSet.java in java programming language.
package Homework3;
public class LinkedListSet extends LinkedListCollection {
LinkedListSet() {
}
public boolean add(T element) {
// Code here
return true;
}
}
Below is the LinkedListCollection.java code. Use to complete LinkedListSet.java.
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
//setting head as it's next element, now head is the second element
and node links to head
node.setLink(head);
//setting node as the new head, so that node is called wherever
head is neaded
head = node;
return true;
}
public boolean remove(T target) {
Node<T> cursor = head;
Node<T> previous = head;
while (cursor != null) {
// Code here
// if the current Node's Info is equal to Target,
//then we link previous Node to the next NOde, unlinking self from
the chain
if(cursor.getInfo() == target){
//Linking next Node to the previous
previous.setLink(cursor.getLink());
//returning true, returning stops the function
//so no need to worry about loop anymore
return true;
}
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
//creating loop to traverse node by node till there is no next
node
while (cursor != null) {
// if the current Node's Info is equal to Target,
if(cursor.getInfo() == target){
//then we link previous Node to the next NOde, unlinking self from
the chain
previous.setLink(cursor.getLink());
//setting cursor to next node to traverse further
cursor = cursor.getLink();
//setting found to true
found = true;
//continue directly starts the next iteration of loop
//doing this because we already set the next no need to execute
further
continue;
}
//in case target does not match current nodes data
//then we set current node as previoous
//and set next nose as current
previous = cursor;
cursor = cursor.getLink();
}
return found;
}
public void removeDuplicate() {
// Remove any duplicated elements
// Code here
//Creating another Linked List that will not hold any duplicate
data
LinkedListCollection <T> other = new LinkedListCollection
<T>();
//setting this LL's cursor
Node<T> cursor_this = head;
//setting No duplicate LL's cursor
Node<T> cursor_other = head;
//Traversing through the LL with duplicate entries
while (cursor_this != null){
//if (LL with no duplicates) does not have the Info of current node
of (LL that have duplicate)
if(!other.contains(cursor_this.getInfo())){
//then we add this link to the LL with no Duplicates
cursor_other.setLink(cursor_this);
//updating cursor of No duplicate LL i.e, other
//so that we can further add to it whenever new non-duplicate node
appears
cursor_other= cursor_other.getLink();
}
//setting next node of duplicate as current node in
traversing
cursor_this = cursor_this.getLink();
}
}
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
//traversing through the LL
while (cursor != null) {
//adding the count by 1 on every element tha is traversed
c++;
//setting next node as the current node
cursor = cursor.getLink();
}
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
//creating cursor to traverse through list
Node<T> cursor= this.head;
//running infinite loop
while (true) {
//if next node is null
//that means we have reached the last of 'this' LL
if(cursor.getLink() == null){
//linking the head of 'that' as the last node's next of
'this'
cursor.setLink(that.head);
//stoping the loop
break;
}
//setting next node as the current node
cursor = cursor.getLink();
}
}
public void clear() {
// Remove all elements in the collection
//code here
//setting head of the LL to null
//every LL starts from head, if head is null there is no LL
this.head = null;
}
public boolean contains(T target) {
// Return true if target is found
boolean found = false;
Node<T> cursor = head;
// Code here
//traversing every node of the LL
while (cursor != null) {
//matching the nodes info to target
if(cursor.getInfo() == target){
//detting found = true so that true is returned
found= true;
}
//setting next node as the current node
cursor = cursor.getLink();
}
return found;
}
}
The completed code is given below. Please do rate the answer if it helped. Thank you
package Homework3;
public class LinkedListSet extends LinkedListCollection {
LinkedListSet() {
}
public boolean add(T element) {
if(!contains(element)){
add(element);
return
true;
}
else
return
false;
}
}
Complete LinkedListSet.java in java programming language. package Homework3; public class LinkedListSet extends LinkedListCollection { LinkedListSet() {...
Complete LinkedListCollection.java with following methods: public LinkedListCollection(); public LinkedListCollection(int size); public boolean isEmpty(); public int size(); // Return number of elements in the Collection. public String toString(); public boolean add(T element); // Add an element into the Collection, return true if successful public boolean remove(T target); // Remove the target from Collection, return true if successful public boolean removeAll(T target); // Remove all occurrences of Target, return if successful public void removeDuplicate(); // Remove duplicated element(s) public boolean equals(LinkedListCollection that);...
Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...
When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> { Node<T> itsFirstNode; Node<T> itsLastNode; private int size; public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; } public Iterator<T> getIterator() { return new Iterator(this); } // THIS WILL NEED...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 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 accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...