Thursday, September 8, 2016

PROGRAM TO DELETE A NODE FROM A LINKED LIST USING LOCATION :)


#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 deplete(int loc)
{
struct node *ptr=(struct node *)malloc(sizeof(struct node));
struct node *save=(struct node *)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if (loc==1)
{
head=head->next;
return;
}
else
{
for(int i=0;i<loc-2;i++)
{ save=ptr;
ptr=ptr->next;
}
save->next=ptr->next;

}
}
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();
     deplete(4);
     printf("AFTER DELETION\n");
     print();
}
Also see program to illustrate the concept of inline function