Angel Mikaela E.
Malaluan
COM221
Line of Code Code Function
1 void deleteNode(int target) Defines a function named
"deleteNode" that takes an
integer parameter "target"
to specify the value to
remove from the list.
2 { Beginning of the
"deleteNode" function.
3 //Function and Purpose of This block checks if the
this block. linked list is empty. If it is, it
displays an error or
success message and
exits the function, because
there's nothing to delete in
an empty list.
4 if (!head) Check if the linked list is
empty by checking if the
head pointer is a null
pointer.
5 { Beginning of the block for
an empty linked list.
6 // Display error or success Display a message saying
message. that the linked list is empty,
making it impossible to
delete a node.
7 return; Exit the function since
there’s nothing to delete in
an empty list.
8 } End of the block for an
empty linked list.
9 //Function and Purpose of This block checks if the
this block. target value is in the head
node. If it is, it creates a
temporary pointer (temp),
updates the head to the
next node, deletes the
original head node, and
then exits the function after
the deletion.
10 if (head->data == target) Check if the value in the
head node matches the
target value.
11 { Beginning of the block for
deletion when the target is
in the head node.
12 Node* temp = head; Create a temporary pointer
and set it to the head
node.
13 head = head->next; Move the head pointer to
the next node.
14 delete temp; Free the memory of the old
head node that has been
removed.
15 return; Exit the function after
deletion.
16 } End of the block for
deletion when the target is
in the head node.
17 // Function and Purpose of This block sets up a loop
this block to traverse the linked list
until the target value is
found or the end of the list
is reached. It uses a
temporary pointer (temp)
to navigate through the
nodes.
18 Node* temp = head; Create another temporary
pointer and set it to the
new head node.
19 while (temp->next && Iterate through the list until
temp->next->data != the target value is found or
target) the end of the list is
reached.
20 { Beginning of the loop.
21 temp = temp->next; Move to the next node in
the list.
22 } End of the loop.
23 if (temp->next != nullptr) Check if the target value
was found in the list.
24 { Beginning of the block if
the target value is found.
25 // Function and Purpose of This block executes if the
this block target value is found in a
non-head node. It creates
a temporary pointer
(toDelete) to the node to
be deleted, adjusts the
next pointer of the
preceding node to skip the
node to be deleted, and
then deletes the node with
the target value.
26 Node* toDelete = temp- Create a pointer to the
>next; node to be deleted.
27 temp->next = toDelete- Adjust the next pointer of
>next; the preceding node to skip
the node to be deleted.
28 delete toDelete; Delete the node with the
target value.
29 } End of the block for
deletion when the target is
in a non-head node.
30 } End of the "deleteNode"
function.