Implement a (NON-RECURSIVE) method to delete ALL occurrences of an item from a c
ID: 3798445 • Letter: I
Question
Implement a (NON-RECURSIVE) method to delete ALL occurrences of an item from a circular linked list. Your method should return a reference to the last node of the resulting list. public class Node {public T data; public Node next; public Node(T data, Node next) {this.data = data; this.next = next;}}//Deletes all occurrences of item from a circular linked list (last node//pointed to by rear), without using recursion//Returns the rear of the resulting linked list//If the item is not in the list, throws a NoSuchElementException public static Node deleteAll(T item, Node rear) throws NoSuchElementException {//COMPLETE THIS METHODExplanation / Answer
public class Node<T>
{
public T data;
public Node<T> next;
public Node(T data,Node<T> next)
{
this.data = data;this.next =next;
}
public Node<T> remove_allOccures(T data,Node<T> head)
{
Node<T> tail =head,temp = head,prev=head;//pointing head..
int c=0;
while(tail.next!=head)tail =tail.next;//finding tail
while(temp.next!=head)
{
if(temp.data==data)
{
c=1;
if(temp==head)
{
head = head.next;//if it is head
tail.next =head;//chainging head
}
else
{
prev.next = temp.next;
}
temp=temp.next;
}
else
{
prev = temp;
temp=temp.next;
}
}
if(c==0)
{
System.out.println("No such element:::");
}
return head;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.