37)Write a C program to delect all the node in a singly linked list which has value N.


#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start=NULL;
void create(int);
void disp();
void del(int);
void main()
{
int ch,i,n,m=0,k;
clrscr();
do
{ printf(“\n1.CREATE\n”);
printf(“\n2.DISPLAY\n”);
printf(“\n3.DELETE SPECIFIED NODE\n”);
printf(“\n4.EXIT\n”);
printf(“\nENTER UR CHOICE\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“\nHOW MANY NODES”);
scanf(“%d”,&n);
while(n>0)
{
printf(“\nENTER THE DATA”);
scanf(“%d”,&m);
create(m);
n–;
}
break;
case 2:
disp();
break;
case 4:
exit(0);
case 3:
printf(“\nENTER THE VALUE OF NODE FOR DELETION”);
scanf(“%d”,&m);
del(m);
break;}
}
while(ch!=4);
getch();
}
void create(int m)
{
struct list *tmp,*q;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
if(start==NULL)
{
printf(“\nLIST IS EMPTY”);
}
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void del(int m)
{
struct list *tmp,*q;
q=start;
while(q->link->link!=NULL)
{
if(q->link->data==m)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
}
else
{
q=q->link;
}
}
if(q->link->data==m)
{
tmp=q->link;
q->link=NULL;
free(tmp);
}
if(start->data==m)
{
tmp=start;
start=start->link;
free(tmp);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s