You have the following class that has the ability to be a node of doubly linked
ID: 3827995 • Letter: Y
Question
You have the following class that has the ability to be a node of doubly linked list. Each node can keep track of the radius of a circle. public class CircleNode { double radius = 1.0; CircleNode next; CircleNode prev; Circle Node() {} CircleNode (double r) { radius = r; } double get Area(){ double area = 3.14159 * radius * radius; return area } a) Write a method that removes the object located in a certain index of a doubly linked list of CircleNode objects. The method must have two input parameters(i) the head of a linked list of CircleNode objects and (ii) the index of an object that the methods should remove. public static CircleNode removeNode (CircleNode head, int position) {Explanation / Answer
public static CircleNode removeNode(CircleNode **head, int position)
{
// If the linked list is empty
if (*head == NULL)
return;
// Store head node
CircleNode * temp = *head;
// If head needs to be removed
if (position == 0)
{
*head= temp->next; // Change head
free(temp); // free old head
return;
}
// Find previous node of the node that needs to be deleted
for (int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;
// If position is more than number of ndoes
if (temp == NULL || temp->next == NULL)
return;
// Node temp->next is the node that needs to be deleted
// Store pointer to the next of node to be deleted
CircleNode *next = temp->next->next;
// Unlink the node from linked list
free(temp->next); // Free memory
temp->next = next; // Unlink the deleted node from list
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.