INSERT AT THE Nth POSITION OF LINK LIST

#include<stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* link;
};

struct node* head = NULL;

void insert(int number ,int pos)
{
int i=0;
struct node* temp1 = (struct node*)malloc(sizeof(struct node));
temp1->data = number;
temp1->link = NULL;
if(pos == 1)
{
temp1->link = head ;
head = temp1;
return;
}

struct node* temp2 = head;
for(i=0;i<pos-2;i++)
{
temp2 = temp2->link;
}
temp1->link = temp2->link;
temp2->link = temp1;
}

void print()
{
struct node* temp = head ;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp = temp->link;
}
}
void main()
{
insert(5,1);
insert(3,2);
insert(7,1);
insert(9,2);
print();
}

Comments