E

I don't understand what your problem is.So I'm gonna propose a code that allows the list to be filled with elements of the body. I hope that if it's not what you need, you will inform me in your commentary to the answer.Here's the demonstration program.#include <iostream>
template <typename T>
struct Node
{
T data;
Node *next;
};
template <typename T>
Node<T> * arrayToList( const T a[], size_t n )
{
Node<T> *head = nullptr;
Node<T> **current = &head;
for ( size_t i = 0; i < n; i++, current = &( *current )->next )
{
*current = new Node<T> { a[i], nullptr };
}
return head;
}
template <typename T>
void displayList( Node<T> *head )
{
for ( ; head; head = head->next ) std::cout << head->data << ' ';
}
int main()
{
int a[] = { 2, 1, 4, 3, 6, 5, 7, 8 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
Node<int> *list = arrayToList( a, N );
displayList( list );
std::cout << std::endl;
}
Its conclusion to the console:2 1 4 3 6 5 7 8
2 1 4 3 6 5 7 8
If your compiler doesn't support the initialization list for the operator. newthe proposal*current = new Node<T> { a[i], nullptr };
you can replace the following proposals*current = new Node<T>;
( *current )->data = a[i];
( *current )->next = nullptr;
Either you can write for class. Node The designer, so that we can rewrite everything in one line like, for example,*current = new Node<T>( a[i], nullptr );