/*function to delete all occurrences of the given key \'x\' */ void deleteAllocc
ID: 3705217 • Letter: #
Question
/*function to delete all occurrences of the given key 'x' */ void deleteAlloccurOfX(struct Node** head_ref, int x) /* if list is empty if ((*head-ref) == NULL) return; struct Node* current- *head_ref; struct Node* next; /* traverse the list up to the end * while (current != NULL) { /* if node found with the valuex'*/ if (current->data x) i *save current's next node in the pointer 'next'*/ next current->next *delete the node pointed to by deleteNode (head ref, current); /* update current */ current ' current - next / else simply move to the next node else current = current->nextExplanation / Answer
Code in java goes like this just think of structure in C++ as class in java
void deleteAlloccurOfX(Node headref,int x) // here Node is a class and headref is kind of an object which is pointing to the start node
{
Node current=new Node();
Node next=new Node();
if(headref==null)
return;
current=headref;
while(current!=null)
{
if(current.data==x)
{
next=current.next;
deleteNode(headref,current);
current=next;
}
else
current=current.next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.