用链表删除重复元素,以0为结尾,只会C语言,不会c++,求指点
#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;
}