#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *h,*p,*t;
int main()
{
int n;
h=(struct node *)malloc(sizeof(struct node));
h->next=NULL;
t=h;
scanf("%d",&n);
int began=n;
while(n--)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
t->next=p;
t=p;
}
struct node *q;
p=h->next;
q=p->next;
h->next=NULL;
while(p)
{
p->next=h->next;
h->next=p;
p=q;
if(q)
q=q->next;
}
p=h->next;
printf("%d\n",began);
while(p->next)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
p=h->next;
while(p)
{
q=p->next;
t=p;
while(q)
{
if(q->data==p->data)
{
t->next=q->next;//t要一直跟在q的后面确保与p相同的元素出现之后能够将其删除
q=t->next;//q用来循环寻找p后与p值相同的元素
began--;
}//找到了就将其删除
else
{
q=q->next;
t=t->next;
}//找不到就将q的值后移一位,t紧跟在q的后面
}
p=p->next;//后移p
}
printf("%d\n",began);
p=h->next;
while(p->next)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
return 0;
}
另外一种用函数的代码