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

Write a code that delete all occurrences of a given key in a linked list using t

ID: 3907320 • Letter: W

Question

Write a code that delete all occurrences of a given key in a linked list using the code given that only removes a single instance. (Java) thank you!

public void sortedRemove(Object item) throws NoSuchElementException //find node to specified item SortedNode SortedNode while(curr prevnull: curr-head; !-null && ((comparable) item) . compareTo(curr . ?tem) > e){ prev-curr curr curr.next; if(curr if(prevnull) //if found at head remove head else //remove appropriate node except head numberOfItems-//then decrement list numitems else //throw execption null && ((Comparable)item).compareTo(curr.item) -e) head -head.next; prev.next-curr.next; throw new NoSuchElementException(item "is not found!");

Explanation / Answer

void deleteAllOccurences(Object item) {

SortedNode curr = head;
SortedNode temp = curr;

while( curr != null ) {

try{
temp = curr.next;
sortedRemove(curr.item);
curr = temp;
}catch(Exception e){
curr = curr.next;
}


}


}



Thanks, PLEASE UPVOTE if helpful

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote