0
Follow
0
View

# Why does this list of deleted duplicate nodes keep getting errors

cszhangjinbo 注册会员
2023-02-28 06:29
I don't know if your problem has been solved, if not:
• Take a look at this blog post, perhaps you will understand, link:

If you have solved the problem, I hope you can share the solution, write a blog about it, and post a link in the comments section to help more people. ^-^ /div>
< div class = "md_content_show e397 data - v - 3967" = "" >

after input output was over nothing

deathyeah 注册会员
2023-02-28 06:29

Based on Monster group and GPT:
1. In the cut function, when you delete a list node, you should use a temporary pointer variable to hold the node before the node to be deleted, rather than directly using the current pointer variable. Because the current pointer variable already points to the memory that has been freed after executing the free() function, revisiting the pointer variable results in undefined behavior. Therefore, you can modify the cut function as follows:

``````void cut(struct cell*head)
{
struct cell*p,*q,*p0,*q0;
while(p != NULL)
{
q0 = p;
q = p->next;
while(q != NULL)
{
if(p->x == q->x) {
q0->next = q->next;
free(q);
q = q0->next;
} else {
q0 = q;
q = q->next;
}
}
p = p->next;
}
}

``````

2. In the rint function, you should output the value of each node with a space or newline character, otherwise the output will be crowded together and difficult to read. Therefore, you can modify the rint function as follows:

``````void rint(struct cell*head)
{
printf("NULL");
return;
}
struct cell*p;
while(p != NULL) {
printf("%d ", p->x);
p = p->next;
}
printf("\n");
}

``````

The modified code above should remove duplicate nodes correctly and print the result.

patvice 注册会员
2023-02-28 06:29

``````#include
#include
struct cell{
int x;
struct cell*next;
};
struct cell*build (void)
{
int n;
scanf("%d",&n);
p=(struct cell*)malloc(sizeof(struct cell));
p->x=n;
p->next=NULL;
scanf("%d",&n);
while(n!=0)
{
tmp=(struct cell*)malloc(sizeof(struct cell));
tmp->x=n;
tmp->next=NULL;
p->next=tmp;
p=p->next;
scanf("%d",&n);
}
};
{
struct cell*p,*q,*p0,*q0;
while(p != NULL) //(p->next!=NULL) 修改
{
q0=p;
q=p->next;
while(q!=NULL)
{
if(p->x==q->x){
if(q->next!=NULL) //{  修改
//q0=q;      修改
//q=q->next; 修改
q0->next=q->next;
//free(q0);  修改
//q0=q;      修改
else      // } {       修改
q0->next=NULL;
free(q);             //修改
q=q0->next;          //修改
//}          修改
}
else{         //{          修改
q0=q;                //修改
q=q->next;
}
}
p=p->next;
}
}
{
struct cell*p,*p0;
while(p!=NULL)
{
printf("%d",p->x);
p=p->next;
}

}
{
struct cell*p,*p0;
while(p!=0)
{
p0=p;p=p->next;free(p0);
}
}
int main()
{

return 0;
}

``````
fengjing1215 注册会员
2023-02-28 06:29

What error phenomenon?
Line 11 entered n, line 17 does not need to be entered again

Publish Time
2023-02-28 06:29
Update Time
2023-02-28 06:29