Friday, September 2, 2016

Tags

PROGRAM TO ADD NODE AT ANY POSITION IN A LINKED LIST

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;

}*head;
void insert(int data,int loc)
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
if(loc==1)
{
temp->next=head;
head=temp;
return;
}int i;
ptr=head;
for(i=0;i<loc-2;i++)
{ ptr=ptr->next;

}
temp->next=ptr->next;
ptr->next=temp;

}
void print()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}

}
main()
{int a,b;
head=NULL;
insert(1,1);
insert(6,1);
insert(7,1);
insert(9,3);
insert(10,3);
insert(5,2);
printf("ELEMENTS ARE :");
     print();
}

This Is The Oldest Page