Suppose we have an int array and we want to create it into a singly linked list from it.

Our Node structure is,
struct node { Â Â Â int value; Â Â Â node * nextPtr;Â Â Â Â Â Â node(int val) Â Â Â { Â Â Â Â Â Â value = val; Â Â Â Â Â Â nextPtr = NULL;Â Â Â Â Â Â } };
We will use our new function like this,
   int arr[] = {1,2,3,4, 89,1,1,1,0};    node * ptr = createListFromArray(arr, sizeof(arr)/sizeof(int));
In this createListFromArray function we will pass array pointer and size of array.
Then we will iterate the array one by one and create a node for each element and append it in last node’s next pointer.
We also keep a track of last node created while iteration to update its next pointer. Besides this, we will also store the first node created,
that we will return in end.
[showads ad=inside_post]
Check out the Code,
Frequently Asked:
node * createListFromArray(int * ptr, int arraySize) { Â Â Â node * nodePtr = NULL; Â Â Â node * rootNodePtr = NULL; Â Â Â node * lastNodePtr = NULL; Â Â Â for(int i = 0 ; i < arraySize; i++) Â Â Â { Â Â Â Â Â Â if(!nodePtr) Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â nodePtr = new node(*(ptr+i)); Â Â Â Â Â Â Â Â Â if(!rootNodePtr) Â Â Â Â Â Â Â Â Â Â Â Â rootNodePtr = nodePtr;Â Â Â Â Â Â Â Â Â Â Â Â if(lastNodePtr)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lastNodePtr->nextPtr = nodePtr; Â Â Â Â Â Â } Â Â Â Â Â Â lastNodePtr = nodePtr; Â Â Â Â Â Â nodePtr = nodePtr->nextPtr; Â Â Â } Â Â Â return rootNodePtr; }
Full Code example is as follows,
[code language=”cpp”]
#include <iostream>
/*
 * Node will contain and int variable as value
 * and a node pointer to next Node.
 **/
struct node
{
   int value;
   node * nextPtr;  Â
   node(int val)
   {
      value = val;
      nextPtr = NULL;  Â
   }
};
/*
 * Iterate the passed array one by one and create a node
 * for each element and append it in last node’s next pointer.
 * Also keep a track of last node created while iteration to
 * update its next pointer in next iteration.
 * Also store the first node created,that we will returned as
 * root node in end.
**/
node * createListFromArray(int * ptr, int arraySize)
{
   node * nodePtr = NULL;
   node * rootNodePtr = NULL;
   node * lastNodePtr = NULL;
   for(int i = 0 ; i < arraySize; i++)
   {
      if(!nodePtr)
      {
         nodePtr = new node(*(ptr+i));
         if(!rootNodePtr)
            rootNodePtr = nodePtr;  Â
         if(lastNodePtr)  Â
            lastNodePtr->nextPtr = nodePtr;
      }
      lastNodePtr = nodePtr;
      nodePtr = nodePtr->nextPtr;
   }
   return rootNodePtr;
}
/*
 * Store the next pointer of passed node as temp variable.
 * Delete the current pointer and pass the earlier stored next pointer
 * to destroyList funtion.
 *
 * If ptr is null it means its the end of linked list, so just return
 * because complete linked list is deleted.
 **/
void destroyList(node * ptr)
{
   if(ptr)
   {
      node * pNext = ptr->nextPtr;
      delete ptr;
      destroyList(pNext);
   }
}
/*
 * Iterate through all nodes and display content
 * of each node untill end of linked list is reached.
 **/
void displayLinkedList(node *ptr)
{
   while(ptr != NULL)
   {
      std::cout<<ptr->value<<" ";
      ptr = ptr->nextPtr;
   }
   std::cout<<std::endl;
}
/*
 * Testing functions.
 **/
int main()
{
   int arr[] = {1,2,3,4, 89,1,1,1,0};
   node * ptr = createListFromArray(arr, sizeof(arr)/sizeof(int));
   displayLinkedList(ptr);
   destroyList(ptr);
   return 0;
}
[/code]