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
Posted: Sat Feb 19, 2022 3:21 pm
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++
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++