#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct node
{
int date;
node *next;
};
int main()
{
node *head,*L,*p;
int n;
cin>>n;
int x;
for(int i=1;i<=n;i++)
{
cin>>x;
p=(struct node *)malloc(sizeof(struct node));
p->date=x;
p->next=NULL;
if(i==1)
head=L=p;
else
L->next=p,L=p;
}
node *i,*j,*k;
L=head;///主链
while(L->next!=NULL)
{
j=L;///j是要删除的节点的上一个节点
k=L->next;///k 次链,查找相同元素
while(k!=NULL)///k=空时,就是到了主链链尾,stop
{
if(L->date==k->date)///判断
{
if(k->next==NULL)///如果k是链尾
{
free(k);///删除k
// k=NULL;
j->next=NULL;
goto endw;
}
j->next=k->next;
free(k);
k=j->next;
}
else
{
j=k;
k=k->next;
}
}
endw:;
if(L->next)
L=L->next;
}
k=head;
while(k!=NULL)
{
cout<<k->date<<" " ;
k=k->next;
}
return 0;
}
数据结构单链表删除相同元素(int)
最新推荐文章于 2022-11-03 22:12:37 发布