两条带头结点的升序重复合并成一个无重复的升序链表-创新互联
#include
#include
typedef struct node{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n){ //建立带表头结点的链表
int i;
ElemSN * h, * p;
h=p=(ElemSN *)malloc(sizeof(ElemSN));
h->next=NULL;
for( i=0;i
p=p->next=(ElemSN *)malloc(sizeof(ElemSN));
p->data =a[i];
p->next=NULL;
}
return h;
}
void Fun(ElemSN*head1,ElemSN*head2){
ElemSN*h=NULL,*p,*t,*s;
while(head1->next&&head2->next) { //任意一条链表为空,循环结束。判断剩余链表的头结点与生成新链表的尾结点的值是否相同,
//相同则free(p),头结点后移
if(head1->next->data
p=head1->next;
head1->next=p->next;
}
else{
p=head2->next;
head2->next=p->next;
}
p->next=NULL;
if(!h)
h=t=p;
else{
if(t->data==p->data)
free(p);
else
t=t->next=p;
}
}
if(head1->next)
s=head1->next;
else
s=head2->next;
while(s){
p=s;
s=s->next;
p->next=NULL;
if(p->data==t->data)
free(p);
else
t=t->next=p;
}
head1->next=h;
}
void Printlink(ElemSN * h){
ElemSN * p;
for(p=h;p->next;p=p->next)
printf("%2d\n",p->next->data);
}
int main(void){
int a[9]={1,2,4,4,5,7,7,8,9};
int b[8]={2,3,3,4,10,12,12,13};
ElemSN * head1,*head2;
head1=Createlink(a,9);
head2=Createlink(b,8);
Fun(head1,head2);
free(head2);
Printlink(head1);
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章标题:两条带头结点的升序重复合并成一个无重复的升序链表-创新互联
当前链接:http://azwzsj.com/article/diedoj.html