Saturday, September 3, 2016

PROGRAM TO INSERT NODE AT ANY POSITION AND AT END 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 insert(int data)
{struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *temp=(struct node*)malloc(sizeof(struct node));
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;

}
temp->data=data;
temp->next=NULL;
ptr->next=temp;
return;




}
void print()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}
}
main()
{
head=NULL;
insert(1,1);
insert(6,1);
insert(7,1);
insert(9,3);
insert(10,3);
insert(5,2);
insert(11);
insert(12);
printf("ELEMENTS ARE:\n");
     print();
}