P
peo_leo
I have a simple implementation of double linked list
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
int i,n,c,x,m,flag=0;
struct node{
struct node* next;
int data;
struct node* prev;
}*h,*t,*start,*last,*z,*p,*q;
printf("enter the number of nodes");
scanf_s("%d",&n);
if(n<=0)
{
printf("no of nodes cannot be %d\n",n);
exit(0);
}
start=(node*)malloc(sizeof(node));
start->data=NULL;
start->prev=NULL;
start->next=NULL;
last=(node*)malloc(sizeof(node));
last->data=NULL;
last->next=NULL;
last->prev=NULL;
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
start->next=h=z;
z->prev=NULL;
i=1;
while(i<n)
{
q=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&q->data);
z->next=q;
q->prev=z;
z=q;
++i;
}
z->next=NULL;
last->prev=t=z;
for(;
{
printf("\n");
printf("1.Insertion\n2.Deletion\n3.Display \n4.exit\n");
printf("\tenter your choice");
scanf_s("%d",&c);
switch(c)
{
case 1:
printf("enter the element after which node has to be inserted:\t");
scanf_s("%d",&x);
p=h;
if(x==-999)
{
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=p;
p->prev=z;
z->prev=NULL;
start->next=h=z;
}
else
{
while((p->data!=x)&&(p!=NULL))
{
p=p->next;
if(p==NULL)
{
printf("node cannot be inserted\n");
//exit(0);
}
}
if (p->next==NULL)
{
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
p->next=z;
z->next=NULL;
z->prev=p;
last->prev=t=z;
}
else
{
q=p->next;
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=q;
q->prev=z;
p->next=z;
z->prev=p;
}
}
break;
case 2:
printf("enter the node to be deleted");
scanf_s("%d",&m);
p=h;
if(p->data==m)
{
start->next=h=p->next;
free(p);
}
else
{
//p=h;
q=p->next;
while((q->data!=m)&&(q!=NULL))
{
p=q;
q=q->next;
}
if(q==NULL)
{
printf("node cannot be deleted\n");
//exit(0);
}
else
{
p->next=q->next;
free(q);
}
}
break;
case 3:
p=start->next;
printf("data are");
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
break;
case 4:
exit(0);
default:
printf("\t\tWrong Choice");
}
}
}
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
int i,n,c,x,m,flag=0;
struct node{
struct node* next;
int data;
struct node* prev;
}*h,*t,*start,*last,*z,*p,*q;
printf("enter the number of nodes");
scanf_s("%d",&n);
if(n<=0)
{
printf("no of nodes cannot be %d\n",n);
exit(0);
}
start=(node*)malloc(sizeof(node));
start->data=NULL;
start->prev=NULL;
start->next=NULL;
last=(node*)malloc(sizeof(node));
last->data=NULL;
last->next=NULL;
last->prev=NULL;
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
start->next=h=z;
z->prev=NULL;
i=1;
while(i<n)
{
q=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&q->data);
z->next=q;
q->prev=z;
z=q;
++i;
}
z->next=NULL;
last->prev=t=z;
for(;
{
printf("\n");
printf("1.Insertion\n2.Deletion\n3.Display \n4.exit\n");
printf("\tenter your choice");
scanf_s("%d",&c);
switch(c)
{
case 1:
printf("enter the element after which node has to be inserted:\t");
scanf_s("%d",&x);
p=h;
if(x==-999)
{
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=p;
p->prev=z;
z->prev=NULL;
start->next=h=z;
}
else
{
while((p->data!=x)&&(p!=NULL))
{
p=p->next;
if(p==NULL)
{
printf("node cannot be inserted\n");
//exit(0);
}
}
if (p->next==NULL)
{
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
p->next=z;
z->next=NULL;
z->prev=p;
last->prev=t=z;
}
else
{
q=p->next;
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=q;
q->prev=z;
p->next=z;
z->prev=p;
}
}
break;
case 2:
printf("enter the node to be deleted");
scanf_s("%d",&m);
p=h;
if(p->data==m)
{
start->next=h=p->next;
free(p);
}
else
{
//p=h;
q=p->next;
while((q->data!=m)&&(q!=NULL))
{
p=q;
q=q->next;
}
if(q==NULL)
{
printf("node cannot be deleted\n");
//exit(0);
}
else
{
p->next=q->next;
free(q);
}
}
break;
case 3:
p=start->next;
printf("data are");
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
break;
case 4:
exit(0);
default:
printf("\t\tWrong Choice");
}
}
}