Linked list implementation using array:
#include<stdio.h>
struct node
{
int info;
struct node* next;
};
/*
Function to create Linked List from Array elements.
*/
struct node* createLL(int* nAR, int n)
{
static int i=0;
struct node* temp = NULL;
if(n==0)
return NULL;
// Create New Node
temp = (struct node*)malloc(sizeof(struct node*));
temp->info = nAR[i++];
temp->next = createLL(nAR, --n);
return temp;
}
void displayLL(struct node *temp)
{
while(temp)
{
printf("%d ", temp->info);
temp=temp->next;
}
}
int main()
{
int n=0, i=0, AR[100]={0};
struct node *t = NULL;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &AR[i]);
printf("\nCreate linked list from array");
t =createLL(AR, n);
printf("\nDisplay Linked List : \n");
if(t)
displayLL(t);
}
OUTPUT|
Enter the number of elements: 5
Create linked list from array
5 6 7 8 9
Display Linked List:
5 6 7 8 9