Create a Linked List from an Array
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *Next;
};
int main()
{
int arr[]= {1,2,3,4,5};
struct Node *temp,*Head=NULL,*current=NULL;
//insert data from array
for(int i=0; i<5; i++)
{
temp= (struct Node *)malloc(sizeof(struct Node));
temp->data=arr[i];
temp->Next=NULL;
if(Head==NULL)
{
Head=temp;
current=temp;
}
else
{
//current=temp;
current->Next=temp;
current=current->Next;
}
}
//print link list
while(Head!=NULL)
{
cout<<Head->data<<endl;
Head=Head->Next;
}
}
Comments
Post a Comment