Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have this function, but when I run my test cases it stops after the first one. It doesn't crash the program, it just stops
I have this function, but when I run my test cases it stops after the first one. It doesn't crash the program, it just stops displaying any tests after test which leads me to believe I have an infinite loop of some sort. But I can't find itAny ideas? template
void SinglyLinkedList::swapFifthAndSeventhNodes
if thiscount
return; There are fewer than elements in the list, so no need to perform the swap
Node current thishead;
Node previous nullptr;
Traverse to the fifth node
for int i ; i ; i Corrected loop condition
previous current;
current currentlink;
Save pointers to the fifth and seventh nodes
Node fifthNode current;
Node seventhNode fifthNodelinklinklink;
Save pointers to the nodes next to the fifth and seventh nodes
Node fifthNext fifthNodelink;
Node seventhNext seventhNodelink;
Rearrange the pointers
if previous
previouslink seventhNode;
else
thishead seventhNode;
fifthNodelink seventhNext;
seventhNodelink fifthNext;
Update tail pointer if necessary
if seventhNode thistail
thistail fifthNode;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started