Subscribe Us

Responsive Advertisement

Advertisement

Create linklist from array using C

 


Create linklist from array using C




#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

};

struct node *create_linklist(int arr[],int size)

{

    struct node *head=NULL,*temp=NULL,*current=NULL;

    for(int i=0;i<size;i++)

    {

        temp=(struct node *)malloc(sizeof(struct node));

        temp->data=arr[i];

        temp->next=NULL;

        if(head==NULL)

        {

            head=temp;

            current=temp;

        }

        else

        {

            current->next=temp;

            current=temp;

        }

    }

    return head;

};

int main()

{


    int a[]={10,20,30,40,50,50};

    struct node *head=NULL;

       head=create_linklist(a,6);

       while(head!=NULL)

       {

           printf("%d ",head->data);

           head=head->next;

       }


    return 0;

}


Post a Comment

0 Comments