WALKTHROUGH delete node CODE
Case 1: Deleting the Top Node from a Linked List
Begin with the following list:
topNode
Mark
Alex
Patty
Cindy
27
28
29
30
NULL
Step 1) Call function deleteNode, passing in the pointer to the top of the list, the name in the node that should be deleted
from the list, and a boolean flag to pass back whether the deletion was successful.
string stuName = "Mark";
bool deleted;
deleteNode (topNode, stuName, deleted);
Step 2) Enter the function and initialize variables:
listTop
prev
delName
here
garbage
Mark
garbage
success
false
Mark
Alex
Patty
Cindy
27
28
29
30
NULL
© 2008, Regis University
last mod 4/30/08
Step 3) Compare the name passed in with the name in the top node.
if ( listTop->name == delName ) {
Since the name passed in (“Mark”) is equivalent to the name in the top node (“Mark”), the if condition is
evaluated as TRUE.
Step 4) Execute the statements in the true part of the if statement.
First point the here pointer to the top node in the list.
here = listTop;
listTop
prev
delName
here
garbage
success
Mark
false
Mark
Alex
Patty
Cindy
27
28
29
30
NULL
Then move the move the listTop pointer down one node, to point to the second node in the list:
listTop = listTop->next;
listTop
prev
delName
here
garbage
Mark
success
false
Mark
Alex
Patty
Cindy
27
28
29
30
NULL
© 2008, Regis University
last mod 4/30/08
Finally, deallocate the node here points to, and set success to true.
delete here;
success = true;
listTop
prev
delName
here
garbage
Mark
garbage
success
true
Alex
Patty
Cindy
28
29
30
NULL
Step 5) Return to the calling function.
return;
Values back in the calling function:
deleted
topNode
true
Alex
Patty
Cindy
28
29
30
NULL
© 2008, Regis University
last mod 4/30/08
Case 2: When the Data is Not Found in the List
Begin with the list from the end of the previous case:
topNode
Alex
Patty
Cindy
28
29
30
NULL
Step 1) Call function deleteNode, passing in the pointer to the top of the list, the name in the node that should be deleted
from the list, and a boolean flag to pass back whether the deletion was successful.
string stuName = "Joe";
bool deleted;
deleteNode (topNode, stuName, deleted);
Step 2) Enter the function and initialize variables:
listTop
prev
garbage
here
delName
garbage
Joe
success
false
Alex
Patty
Cindy
28
29
30
NULL
Step 3) Compare the name passed in with the name in the top node.
if ( listTop->name == delName ) {
Since the name passed in (“Joe”) is NOT equivalent to the name in the top node (“Alex”), the if condition is
evaluated as FALSE.
© 2008, Regis University
last mod 4/30/08
Step 4) Execute the statements in the else clause. Begin by initializing the prev and here pointers.
else
listTop
{
prev = listTop;
here = listTop->next;
prev
here
delName
Joe
success
false
Alex
Patty
Cindy
28
29
30
NULL
Step 5) Evaluate the while loop condition.
while ( (here != NULL) && (here->name != delName) ) {
here is not currently NULL and the name in the node here points to (“Patty”) does not match the name passed in
(“Joe”), so the condition evaluates to TRUE and the while loop code is executed.
Step 6) Execute the while loop statements.
Both the prev and the here pointers are moved down one node.
prev = here;
here = here->next;
listTop
prev
here
delName
Joe
success
false
Alex
Patty
Cindy
28
29
30
NULL
© 2008, Regis University
last mod 4/30/08
Step 7) Loop back up and evaluate the while loop condition again.
while ( (here != NULL) && (here->name != delName) ) {
here is still not NULL and the name in the node here points to (“Cindy”) does not match the name passed in
(“Joe”), so the condition evaluates to TRUE and the while loop code is executed.
Step 8) Execute the while loop statements.
Both the prev and here pointers are moved down one node. Note that there is no further node for here to move
down to, so it becomes NULL.
prev = here;
here = here->next;
listTop
prev
here
delName
NULL
Joe
success
false
Alex
Patty
Cindy
28
29
30
NULL
Step 9) Loop back up and evaluate the while loop condition again.
while ( (here != NULL) && (here->name != delName) ) {
here is now NULL, so the condition evaluates to FALSE, and we fall out of the while loop.
Step 10) Evaluate the if condition:
if (here != NULL) {
here is NULL, so the condition evaluates to FALSE.
© 2008, Regis University
last mod 4/30/08
Step 11) Execute the statements in the else clause and print an error message.
else
cout << delName << " not found in list." << endl;
Display:
Joe not found in list.
Step 12) Return the calling function.
return;
The list is unchanged in the calling function.
The calling function can check the deleted flag to determine whether the requested node was deleted.
© 2008, Regis University
last mod 4/30/08
Case 3: Deleting a Node from the Middle of the List
Begin with the list from the end of the previous case:
topNode
Alex
Patty
Cindy
28
29
30
NULL
Step 1) Call function deleteNode, passing in the pointer to the top of the list, the name in the node that should be deleted
from the list, and a boolean flag to pass back whether the deletion was successful.
string stuName = "Patty";
bool deleted;
deleteNode (topNode, stuName, deleted);
Step 2) Enter the function and initialize variables:
listTop
prev
garbage
here
delName
garbage
Patty
success
false
Alex
Patty
Cindy
28
29
30
NULL
Step 3) Compare the name passed in with the name in the top node.
if ( listTop->name == delName ) {
Since the name passed in (“Joe”) is NOT equivalent to the name in the top node (“Alex”), the if condition is
evaluated as FALSE.
© 2008, Regis University
last mod 4/30/08
Step 4) Execute the statements in the else clause. Begin by initializing the prev and here pointers.
else
listTop
{
prev = listTop;
here = listTop->next;
prev
here
delName
Joe
success
false
Alex
Patty
Cindy
28
29
30
NULL
Step 5) Evaluate the while loop condition.
while ( (here != NULL) && (here->name != delName) ) {
here is not currently NULL, but the name in the node here points to (“Patty”) matches the name passed in
(“Patty”), so the condition evaluates to FALSE and the program skips the while loop code.
Step 6) Evaluate the if condition:
if (here != NULL) {
here is not NULL, so the condition evaluates to TRUE.
© 2008, Regis University
last mod 4/30/08
Step 7) Execute the statements in the true part of the if statement.
First re-route the pointer in the node prev points to.
prev->next = here->next;
listTop
prev
here
delName
Joe
success
false
Alex
Patty
Cindy
28
29
30
delete here;
success = true;
NULL
Then de-allocate the memory here points to and set success to true.
delete here;
success = true;
listTop
prev
here
delName
garbage
success
Joe
true
Alex
Cindy
28
30
NULL
Step 8) Pass back success so that the calling program knows that the deletion was successful, and the modified listTop.
return;
© 2008, Regis University
last mod 4/30/08
The result in the calling program is:
topNode
deleted
true
Alex
Cindy
28
30
NULL
© 2008, Regis University
last mod 4/30/08
© Copyright 2026 Paperzz