#include <stdio.h>
#include <stdlib.h>
struct st
{
int data;//定义数据域
struct st *next;//定义指针域
};
struct st *cr1(int n) //建逆序表
{ int i;
struct st*head,*p;
head=(struct st*)malloc(sizeof(struct st));
head->next=NULL;
for(i=1;i<=n;i++)
{
p=(struct st *)malloc(sizeof(struct st));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
return head;
};
int del(struct st*h,int n) //删除n个结点中的重复结点。
{
struct st*p,*q,*z;
p=h->next; //p指向第一个点
while(p)
{
z=p;
q=z->next;
while(q)
{if(p->data==q->data)
{
z->next=q->next;
q=z->next;
n--;
}
else {z=z->next;q=q->next;}
}
p=p->next;
}
return n;
}
void pr(struct st *h)
{
struct st*p;
int n=0;
p=h->next;
while(p)
{n++;
if(n==1)
printf("%d",p->data);
else printf(" %d",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
int n,s;
struct st *h;
scanf("%d",&n);
h=cr1(n);
printf("%d\n",n);
pr(h);
s=del(h,n);
printf("%d\n",s);
pr(h);
return 0;
}
链表删除
最新推荐文章于 2022-11-01 21:46:37 发布