Wednesday, September 7, 2016

PROGRAM TO DELETE A NODE FROM 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 deplete(int data)
 {
  struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *save=(struct node*)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if(save->data==data)
{
head=head->next;
return;
}
else{
while(ptr!=NULL)
{
if(ptr->data==data)
{
save->next=ptr->next;
return;
}
else
{
save=ptr;
ptr=ptr->next;
}}
printf("ELEMENT NOT FOUND");
 
}
 }
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();
     deplete(9);
     printf("AFTER DELETION");
     print();
}