Q4. Suppose we have a doubly linked list having forward and back pointers: 3, 1, 8, 5, 4, 2, 9, 6, 7, 0. Write a routine

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Q4. Suppose we have a doubly linked list having forward and back pointers: 3, 1, 8, 5, 4, 2, 9, 6, 7, 0. Write a routine

Post by answerhappygod »

Q4. Suppose we have a doubly linked list having forward and back
pointers: 3, 1, 8, 5, 4, 2,
9, 6, 7, 0. Write a routine that swaps any two nodes in the list,
resetting their forward and
back pointers as needed. You should of course check that values a
and b are in the list
(however you like) and then exchange them in the list if both are
present. Notice that the head
pointer may change as a result of this operation. And remember to
pay attention to the special
cases, like:
a and b are separated by one node in the list,
a and b are adjacent to each other in the list,
a or b is the head or the tail of the list.
void swap(const node*& head_ptr, Data a, Data b )
{
// up to you
}
do in c++
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply