Write a function to included in s short linked class called delete At position t
ID: 3824818 • Letter: W
Question
Write a function to included in s short linked class called delete At position that will receive an int parameter and delete a node at the position specified by the parameter of the parameter is a negative number or a number large than the number of nodes inthe list print a message consider efficirncy Write a function to included in s short linked class called delete At position that will receive an int parameter and delete a node at the position specified by the parameter of the parameter is a negative number or a number large than the number of nodes inthe list print a message consider efficirncyExplanation / Answer
Hi Friend, You have not mentioned about programming language.
Please try to provide all required details.
I have implemented in Java.
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Given a reference (pointer to pointer) to the head of a list
and a position, deletes the node at the given position */
void deleteNode(int position)
{
if(position < 0){
System.out.println("position can not be negative");
return;
}
// If linked list is empty
if (head == null)
return;
// Store head node
Node temp = head;
// If head needs to be removed
if (position == 0)
{
head = temp.next; // Change head
return;
}
// Find previous node of the node 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){
System.out.println("position is more than number of nodes in list");
return;
}
// Node temp->next is the node to be deleted
// Store pointer to the next of node to be deleted
Node next = temp.next.next;
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.