Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help!! Thanks in advance public class StringLinkedBag { private StringNod

ID: 3909001 • Letter: P

Question

Please help!! Thanks in advance
public class StringLinkedBag { private StringNode head; private StringNode tail; private int numElements;
/** * No-arg Constructor */ public StringLinkedBag( ) { head = null; tail = null; numElements = 0; }
/** * The getSize method returns the linked bag's current size. * @return The value in the numElements field. */ public int getSize() { return numElements; }
/** * The add method adds a String to the linked bag * This version just sticks the value at the end of the list * @param newElement The String to be added to the linked bag. */ public void add(String newElement) { if (tail == null) // is list empty? { head = new StringNode(newElement, null); tail = head; } else { tail.setLink(new StringNode(newElement,null)); tail = tail.getLink(); } numElements++; }    /** * The exists method looks for a String in the linked bag * @param target The String to be found in the linked bag. * @return a boolean to indicate if target is found in the linked bag. */ public boolean exists(String target) { boolean found = false; StringNode cursor = head;
while (cursor!= null && !found) { if (cursor.getData().equalsIgnoreCase(target)) found = true; else cursor = cursor.getLink(); } return found; // although not "efficient", this method could contain just 1 line: // return (countOccurences(target) > 0); }    /** * The countOccurrences method looks for a String in the linked bag * @param target The String to be found in the linked bag. * @return an int with the number of times target is found in the linked bag. */ public int countOccurrences(String target) { int numOccurrences = 0; StringNode cursor;
for (cursor = head; cursor!= null; cursor = cursor.getLink()) if (cursor.getData().equalsIgnoreCase(target)) numOccurrences++;
return numOccurrences; }    /** * The remove method looks for a String in the linked bag and removes it * this version of remove maintains order * @param target The String to be removed from the linked bag. * @return a boolean to indicate if the target was removed from the bag. */ public boolean remove(String target) { boolean found = false; StringNode cursor = head, previous = null;
while (cursor!= null && !found) { if (cursor.getData().equalsIgnoreCase(target)) found = true; else { previous = cursor; cursor = cursor.getLink(); } }
if (found && cursor != null) { if (previous == null) head = head.getLink( ); else previous.setLink(cursor.getLink( ));
if (tail == cursor) tail = previous; numElements--; } return found; }

/** * the iteratorPrototype method "copies" the linked list and passes * the copied linked list to a new ListerPrototype2 * @return a ListerPrototype2 using a copy of the linked list */ public ListerPrototype2 iteratorPrototype() { // declare variables StringNode headOfListToReturn; // beginning of new "copied" list StringNode cursorOfListToCopy; // active node of list to copy StringNode lastNodeOfListToReturn; // end of new "copied" list
// establish the copied list headOfListToReturn = null;
if (head != null) { // create the head of the new list headOfListToReturn = new StringNode(head.getData(),null); // use lastNodeOfListToReturn as a pointer to the last node in the copied list lastNodeOfListToReturn = headOfListToReturn; /* for debugging: if (head == tail) System.out.println("T: "+head.getData()); */ // use currentCursor as the pointer to the existing list cursorOfListToCopy = head.getLink(); // if we have a node... while (cursorOfListToCopy != null) { // create a new node from the end of the new list lastNodeOfListToReturn.setLink(new StringNode(cursorOfListToCopy.getData(),null)); // move lastNodeOfListToReturn to the new last node lastNodeOfListToReturn = lastNodeOfListToReturn.getLink(); /* for debugging: if (cursorOfListToCopy == tail) System.out.println("T: "+cursorOfListToCopy.getData()); */ // move the cursorOfListToCopy to the next node cursorOfListToCopy = cursorOfListToCopy.getLink(); } } return new ListerPrototype2(headOfListToReturn); }
}
This lab is to expose you to a singly Linked List of String The Lab The add() method in the StringlinkedBag.java in Canvas, adds to the end of the Linked List. Modify the addl_) method to maintain a.z order. Make sure your revised add( ) method as O(n) in t worst case scenario. Make sure you add your name to the comments before the add method. Griding Rubric A more formal grading rubric will be created as a separate file. In the meantime: .The only comment needed is you add your name to the Javadoc comments for the add me make sure you follow the naming guidelines for variable names make sure your code is properly indenting make sure your StringLinkedBag.java file compiles. . e Submitting the Lab All you need to submit via Canvas is your modified StringLinkedBag java file.

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

public class StringLinkedBag
{
private StringNode head;
private StringNode tail;
private int numElements;

/**
* No-arg Constructor
*/
public StringLinkedBag( )
{
head = null;
tail = null;
numElements = 0;
}

/**
* The getSize method returns the linked bag's current size.
* @return The value in the numElements field.
*/
public int getSize()
{
return numElements;
}

/**
* The add method adds a String to the linked bag
* This version just sticks the value at the end of the list
* @param newElement The String to be added to the linked bag.
*/
public void add(String newElement)
{
StringNode n = new StringNode(newElement, null);


if (tail == null) // is list empty?
{
head = n;
tail = head;
}
else
{
StringNode current = head;
StringNode previous = null;

while(current != null)
{
if(newElement.compareTo(current.getElement()) < 0)
break;
previous = currrent;
current = current.getLink();
}

n.setLink(current);  

if(current == head) //add as first node
head = n;
else
previous.setLink(n);

if(previous == tail) //add as last node
tail = n;
}
numElements++;

}
  
/**
* The exists method looks for a String in the linked bag
* @param target The String to be found in the linked bag.
* @return a boolean to indicate if target is found in the linked bag.
*/
public boolean exists(String target)
{
boolean found = false;
StringNode cursor = head;

while (cursor!= null && !found)
{
if (cursor.getData().equalsIgnoreCase(target))
found = true;
else
cursor = cursor.getLink();
}
return found;
// although not "efficient", this method could contain just 1 line:
// return (countOccurences(target) > 0);
}
  
/**
* The countOccurrences method looks for a String in the linked bag
* @param target The String to be found in the linked bag.
* @return an int with the number of times target is found in the linked bag.
*/
public int countOccurrences(String target)
{
int numOccurrences = 0;
StringNode cursor;

for (cursor = head; cursor!= null; cursor = cursor.getLink())
if (cursor.getData().equalsIgnoreCase(target))
numOccurrences++;

return numOccurrences;
}
  
/**
* The remove method looks for a String in the linked bag and removes it
* this version of remove maintains order
* @param target The String to be removed from the linked bag.
* @return a boolean to indicate if the target was removed from the bag.
*/
public boolean remove(String target)
{
boolean found = false;
StringNode cursor = head, previous = null;

while (cursor!= null && !found)
{
if (cursor.getData().equalsIgnoreCase(target))
found = true;
else
{
previous = cursor;
cursor = cursor.getLink();
}
}

if (found && cursor != null)
{
if (previous == null)
head = head.getLink( );
else
previous.setLink(cursor.getLink( ));

if (tail == cursor)
tail = previous;
numElements--;
}
return found;
}


/**
* the iteratorPrototype method "copies" the linked list and passes
* the copied linked list to a new ListerPrototype2
* @return a ListerPrototype2 using a copy of the linked list
*/
public ListerPrototype2 iteratorPrototype()
{
// declare variables
StringNode headOfListToReturn; // beginning of new "copied" list
StringNode cursorOfListToCopy; // active node of list to copy
StringNode lastNodeOfListToReturn; // end of new "copied" list

// establish the copied list
headOfListToReturn = null;

if (head != null)
{
// create the head of the new list
headOfListToReturn = new StringNode(head.getData(),null);
// use lastNodeOfListToReturn as a pointer to the last node in the copied list
lastNodeOfListToReturn = headOfListToReturn;
/* for debugging:
if (head == tail)
System.out.println("T: "+head.getData());
*/
// use currentCursor as the pointer to the existing list
cursorOfListToCopy = head.getLink();
// if we have a node...
while (cursorOfListToCopy != null)
{
// create a new node from the end of the new list
lastNodeOfListToReturn.setLink(new StringNode(cursorOfListToCopy.getData(),null));
// move lastNodeOfListToReturn to the new last node
lastNodeOfListToReturn = lastNodeOfListToReturn.getLink();
/* for debugging:
if (cursorOfListToCopy == tail)
System.out.println("T: "+cursorOfListToCopy.getData());
*/
// move the cursorOfListToCopy to the next node
cursorOfListToCopy = cursorOfListToCopy.getLink();
}
}
return new ListerPrototype2(headOfListToReturn);
}

}