c++ replace elements in linked list

Jun 27, 2019 at 5:37am
Does someone know how to write a function that replaces the first one with the last element in a dynamic, one-way linked list.
Jun 27, 2019 at 6:29am
1
2
3
4
5
6
7
8
9
10
template <typename Item>
void replace_first_by_last( std::forward_list<Item> & my_forward_list )
{
    auto last = my_forward_list.begin();
    for( auto itr = my_forward_list.begin();  itr != my_forward_list.end();  ++itr)
    {
        last = itr;
    }
    *my_forward_list.begin() = *last;
}
Last edited on Jun 27, 2019 at 6:48am
Topic archived. No new replies allowed.