Если я правильно понял назначение, вам нужно что-то вроде следующего, показанного в демонстрационной программе ниже.
Я использовал структуру шаблона, но если вы хотите, вы можете легко удалить любые упоминания спецификаций шаблона.
Я назвал функцию append_n
.
Вот, пожалуйста.
#include <iostream>
#include <iterator>
template <typename T>
struct node
{
T data;
node *next;
};
template <typename T, typename InputIterator>
void assign( node<T> * &head, InputIterator first, InputIterator last )
{
while ( head != nullptr )
{
node<T> *current = head;
head = head->next;
delete current;
}
for ( node<T> **current = &head; first != last; ++first )
{
*current = new node<T>{ *first, nullptr };
current = &( *current )->next;
}
}
template <typename T>
void append_n( node<T> * &head, size_t n )
{
if ( n != 0 )
{
node<T> **last = &head;
while ( *last != nullptr && n-- )
{
last = &( *last )->next;
}
if ( *last != nullptr )
{
node<T> **first = &head;
while ( *last != nullptr )
{
first = &( *first )->next;
last = &( *last )->next;
}
*last = head;
head = *first;
*first = nullptr;
}
}
}
template <typename T>
std::ostream & operator <<( std::ostream &os, node<T> * &head )
{
for ( const node<T> *current = head; current != nullptr; current = current->next )
{
os << current->data << " -> ";
}
return os << "null";
}
int main()
{
node<int> *head = nullptr;
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
assign( head, std::begin( a ), std::end( a ) );
std::cout << head << '\n';
for ( size_t i = 0, n = 1; i < N; i += n )
{
append_n( head, n );
std::cout << head << '\n';
}
std::cout << '\n';
for ( size_t i = 0, n = 2; i < N; i += n )
{
append_n( head, n );
std::cout << head << '\n';
}
std::cout << '\n';
for ( size_t i = 0, n = 5; i < N; i += n )
{
append_n( head, n );
std::cout << head << '\n';
}
std::cout << '\n';
return 0;
}
Вывод программы:
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> null
3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> null
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
Если использовать вашу функцию объявление, когда функция возвращает указатель на головной узел, тогда ее определение может выглядеть так, как показано в следующей демонстрационной программе.
#include <iostream>
#include <iterator>
template <typename T>
struct node
{
T data;
node *next;
};
template <typename T, typename InputIterator>
void assign( node<T> * &head, InputIterator first, InputIterator last )
{
while ( head != nullptr )
{
node<T> *current = head;
head = head->next;
delete current;
}
for ( node<T> **current = &head; first != last; ++first )
{
*current = new node<T>{ *first, nullptr };
current = &( *current )->next;
}
}
template <typename T>
node<T> * append_n( node<T> * head, size_t n )
{
if ( n != 0 )
{
node<T> *last = head;
while ( last != nullptr && n-- )
{
last = last->next;
}
if ( last != nullptr )
{
node<T> *first = head;
while ( last->next != nullptr )
{
first = first->next;
last = last->next;
}
last->next = head;
head = first->next;
first->next = nullptr;
}
}
return head;
}
template <typename T>
std::ostream & operator <<( std::ostream &os, node<T> * &head )
{
for ( const node<T> *current = head; current != nullptr; current = current->next )
{
os << current->data << " -> ";
}
return os << "null";
}
int main()
{
node<int> *head = nullptr;
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
assign( head, std::begin( a ), std::end( a ) );
std::cout << head << '\n';
for ( size_t i = 0, n = 1; i < N; i += n )
{
head = append_n( head, n );
std::cout << head << '\n';
}
std::cout << '\n';
for ( size_t i = 0, n = 2; i < N; i += n )
{
head = append_n( head, n );
std::cout << head << '\n';
}
std::cout << '\n';
for ( size_t i = 0, n = 5; i < N; i += n )
{
head = append_n( head, n );
std::cout << head << '\n';
}
std::cout << '\n';
return 0;
}
Вывод программы такой же, как показано выше.
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> null
3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> null
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null