Delete duplicate elements with a list, end with 0, only know C language, do not know C ++, ask for instructions
#include<stdio.h>
#include<malloc.h>
struct cell{
int x;
struct cell*next;};
struct cell*build (void)
{
struct cell*head,*p,*tmp;
head=p=tmp=NULL;
int n;
scanf("%d",&n);
if(n==0){return head;}
p=(struct cell*)malloc(sizeof(struct cell));
p->x=n;
p->next=NULL;
head=p;
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);
}
return head;
};
void cut(struct cell*head)
{
struct cell*p,*q,*p0,*q0;
p=head;
while(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;free(q0);}else{q0->next=NULL;free(q);q=q0->next;}}
else q=q->next;
}
p=p->next;
}
}
void rint(struct cell*head)
{
if(head==NULL){printf("NULL");return ;}
struct cell*p,*p0;
p=head;
while(p!=NULL)
{
printf("%d",p->x);
p=p->next;
}
}
void release(struct cell*head)
{
struct cell*p,*p0;
p=head;
while(p!=0)
{
p0=p;p=p->next;free(p0);
}
}
int main()
{
struct cell*head;
head=build();
cut(head);
rint(head);
release(head);
return 0;
}
0 Answer
after input output was over nothing
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;
p=head;
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)
{
if(head == NULL) {
printf("NULL");
return;
}
struct cell*p;
p = head;
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.
void cut(struct cell*head) function modified as follows, please see the comments for reference:
#include
#include
struct cell{
int x;
struct cell*next;
};
struct cell*build (void)
{
struct cell*head,*p,*tmp;
head=p=tmp=NULL;
int n;
scanf("%d",&n);
if(n==0){return head;}
p=(struct cell*)malloc(sizeof(struct cell));
p->x=n;
p->next=NULL;
head=p;
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);
}
return head;
};
void cut(struct cell*head)
{
struct cell*p,*q,*p0,*q0;
p=head;
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;
}
}
void rint(struct cell*head)
{
if(head==NULL){printf("NULL");return ;}
struct cell*p,*p0;
p=head;
while(p!=NULL)
{
printf("%d",p->x);
p=p->next;
}
}
void release(struct cell*head)
{
struct cell*p,*p0;
p=head;
while(p!=0)
{
p0=p;p=p->next;free(p0);
}
}
int main()
{
struct cell*head;
head=build();
cut(head);
rint(head);
release(head);
return 0;
}
What error phenomenon?
Line 11 entered n, line 17 does not need to be entered again
这家伙很懒,什么都没留下...