DELETION IN LINK LIST








--------------------------------------------------------------------------------------------------------------------------

#include
#include
#include
#include
struct node{
int data;
struct node *next;
};
struct node * deleting(struct node *head ,int n);
void main()
{
struct node *head=NULL,*ptr=NULL,*temp=NULL,*ptr2,*ptr3;
int n,i;
clrscr();
printf("enter the size of link list \n");
scanf("%d",&n);
for(i=0;i
       {
temp=(struct node *)malloc(sizeof(struct node));
temp->next=NULL;
printf("\nenter the data in temp variable");
scanf("%d",&temp->data);

if(head==NULL)
{
    head=temp;

}
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}}
ptr3=head;
while(ptr3!=NULL)
{
printf("%d ",ptr3->data);
ptr3=ptr3->next;
}
     head=deleting(head,n);
     printf("after deletingn\n");
     ptr3=head;
while(ptr3!=NULL)
{
printf("%d ",ptr3->data);
ptr3=ptr3->next;
}
getch();
}
struct node * deleting(struct node *head ,int n)
{
struct node *temp2,*ptr,*temp3;
int i=1,loc;
printf("\nenter position where do you want to delete a node\n");
scanf("%d",&loc);
if(loc>n)
{
printf("you have entered wrong choice\n");
  }
else if(i==loc)
{
temp2=head;
head=head->next;
free(temp2);
   }
else
{
     ptr=head;

     for(i=1;i
     {
temp3=ptr;
ptr=ptr->next;
}
       temp3->next=ptr->next;
       ptr->next=NULL;
       free(ptr);
}
return head;
}