implementation of lists through linked list .whenever i delete a node and call display function loop runs infinitely

#include<stdio.h>
#include<malloc.h>
struct node{
int data;
struct node *next;
};
void incert(int num,node *pointer)
{
while(pointer->next!=NULL)
pointer=pointer->next;
pointer->next=(node *)malloc(sizeof(node));
pointer=pointer->next;
pointer->data=num;
pointer->next=NULL;
}
void display(node *pointer)
{
while(pointer->next!=NULL)
{
printf("%d->",(pointer->next)->data);
pointer=pointer->next;
}
printf("\n");
}
void del(int num,node *pointer)
{
while(((pointer->next)->data!=num)&&(pointer->next!=NULL))
{
pointer=pointer->next;
}


if(pointer->next==NULL)

printf("element not found");

else
{

node *t;
t=(node *)malloc(sizeof(node));
t=pointer->next;
pointer=t->next;
free(t);
}

return;
}

main()
{ int num,choice;
struct node *start,*temp;
start=(node *)malloc(sizeof(node));
temp=start;
temp->next=NULL;
while(choice!=4)
{
printf("enter choice number\n");
printf("1)add an element\n2)delete an element\n3)display list\n4)to exit");
scanf("%d",&choice) ;

if(choice==1)
{ printf("enter element to be added");
scanf("%d",&num);
incert(num,temp);
printf("\n");
}
else if(choice==2)
{
printf("enter the data to be deleted");
scanf("%d",&num);
del(num,start);
}

else if(choice==3)
display(start);

else if(choice==4)
break;

else
printf("wrong choice");
}
}
Last edited on
Topic archived. No new replies allowed.