your text
I was recently learning data structures through a singly linked list. When I created all the functions for my list, they all worked fine except for the delete function. flag
is the head of the list and stores the number of nodes. flag1
is a representative of flag
that doesn’t change flag
‘s data value. When I created this function and compared it with others’ work, I didn’t find much difference. Can someone tell me what’s wrong with my logic? I know there might be some other issues that within the code like the the position of return, but it shouldn’t be the major problem.
typedef struct Node{
int data;
struct Node* next;
}Node;
void delete(Node* flag,int data){
Node* flag1=flag;
while(flag1->next){
Node* preFlag1=flag1;
flag1=flag1->next;
if(flag1->data==data){
preFlag1->next=flag1->next;
free(flag1);
flag->data--;
return;
}
}
}
I tried to ask chatgpt, but the answer is not what I thought was crucial to this problem. I want to know what the problem in the logic aspect.I want them to be pointed out.
Yi Lin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.