#include
int main()
{
struct node{
int data;
struct node *link;
}*head;
struct node *temp,*temp1;
head=NULL;
int num,i,item;
scanf("%d",&num);
for(i=0;i
{
scanf("%d",&item);
if(head==NULL)
{
head=malloc(sizeof(struct node));
head->data=item;
temp=head;
}
else
{
temp->link=malloc(sizeof(struct node));
temp->link->data=item;
temp=temp->link;
}
}
temp->link=NULL;
temp=head;
struct node *prev,*temp2;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
temp2=temp->link; //without these two lines
free(temp2); // deletion at end can be done
prev->link=NULL;
printf("after deletion at end\n");
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
return 0;
}
deletion at any node -----
#include
#include
int main()
{
struct node{
int data;
struct node *link;
}*head;
struct node *temp,*temp1;
head=NULL;
int num,i,item;
scanf("%d",&num);
for(i=0;i
{
scanf("%d",&item);
if(head==NULL)
{
head=malloc(sizeof(struct node));
head->data=item;
temp=head;
}
else
{
temp->link=malloc(sizeof(struct node));
temp->link->data=item;
temp=temp->link;
}
}
temp->link=NULL;
temp=head;
struct node *prev;
int pos;
printf("enter position from where is to be deleted");
scanf("%d",&pos);
for(i=0;i
{
temp1=temp;
temp=temp->link;
}
temp1->link=temp->link;
free(temp);
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
return 0;
}
No comments:
Post a Comment