0% found this document useful (0 votes)
4 views2 pages

Create and Insert in Linked List

The document contains C code for creating and manipulating a linked list. It includes functions to create a linked list from an array, display the list, and insert new nodes at specified indices. The main function demonstrates inserting nodes into the list and displaying the final list.

Uploaded by

Anuj Rane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Create and Insert in Linked List

The document contains C code for creating and manipulating a linked list. It includes functions to create a linked list from an array, display the list, and insert new nodes at specified indices. The main function demonstrates inserting nodes into the list and displaying the final list.

Uploaded by

Anuj Rane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Insert and create a Linked List

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
}*first=NULL;

void create(int A[],int n)


{
int i;
struct Node *t,*last;
first=(struct Node *)malloc(sizeof(struct Node));
first->data=A[0];
first->next=NULL;
last=first;

for(i=1;i<n;i++)
{
t=(struct Node*)malloc(sizeof(struct Node));
t->data=A[i];
t->next=NULL;
last->next=t;
last=t;
}
}

void Display(struct Node *p)


{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}

void Insert(struct Node *p,int index,int x)


{
struct Node *t;
int i;

if(index < 0 || index > count(p))


return;
t=(struct Node *)malloc(sizeof(struct Node));
t->data=x;

if(index == 0)
{
t->next=first;
first=t;
}
else
{
for(i=0;i<index-1;i++)
p=p->next;
t->next=p->next;
p->next=t;

}
}
int main()
{

Insert(first,0,15);
Insert(first,0,8);
Insert(first,0,9);
Insert(first,1,10);

Display(first);

return 0;
}

You might also like