#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct no
{
int data;
struct no *next;
} node;
node *creat(int k)
{
int i,m;
node *p,*head;
head=(node *)malloc(sizeof(node));
head->next=NULL;
for(i=1;i<=k;i++)
{
scanf("%d",&m);
p=(node *)malloc(sizeof(node));
p->data=m;
p->next=NULL;
p->next=head->next;
head->next=p;
}
printf("%d\n",k);
return head;
}
node *del(node *head,int k)
{
int f=0;
node *p,*q,*t;
t=head->next;
while(t)
{
p=t;
q=t->next;
while(q)
{
if(t->data==q->data)
{
f++;
p->next=q->next;
free(q);
q=p->next;
}
else
{
p=q;
q=q->next;
}
}
t=t->next;
}
printf("%d\n",k-f);
return head;
}
node print(node *head)
{
node *t;
t=head->next;
while(t)
{
if(t->next==NULL)
{
printf("%d\n",t->data);
}
else
{
printf("%d ",t->data);
}
t=t->next;
}
}
int main()
{
int n;
node *p;
scanf("%d",&n);
p=creat(n);
print(p);
del(p,n);
print(p);
return 0;
}
数据结构实验之链表七:单链表中重复元素的删除
最新推荐文章于 2020-05-18 12:48:04 发布