匿名用户
1级
2018-12-17 回答
#include
#include
#define LEN sizeof(struct Node)
struct Node
{
int num ;
struct Node *next;
};
int main()
{
struct Node *creat();
struct Node *del(struct Node *head);
void print(struct Node *);
struct Node *head;
head=creat();
print(head);
printf("删除相同的结点后:\n");
del(head);
print(head);
return 0;
}
//建立链表的的函数
struct Node *creat()
{
struct Node *head;
struct Node *p1,*p2;
p1=p2=(struct Node *) malloc(LEN);
head=NULL;
int n = 0;
p1->num = n;
while (p1->num < 19)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Node *)malloc(LEN);
p1->num = n;
}
p2->next=NULL;
return (head);
}
//删除相同结点的函数
struct Node *del(struct Node *head)
{
struct Node *p,*q,*f,*r;
p=head;
while(p!=NULL)
{
r=p;
f=r->next;
while(f!=NULL)
{
if(f->num % 2 != 0)
{
q=f;
r->next=f->next;
f=f->next;
free(q);
}
else
{
r=f;
f=f->next;
}
}
p=p->next;
}
return head;
}
//输出链表的函数
void print(struct Node *head)
{
struct Node *p;
p=head;
while (p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}
追问:
你这和我要求的不同吧?