设head1和head2分别指向两个带表头结点的升序链表,将两个升序链表合并成一个升序链表并去重复值

#include<stdio.h>
#include<stdlib.h>
#define N 5
#include"toulianbiao.h"//与前文的“lianbiao.h”类似,此处为带表头结点的链表
void Delkeynode(ElemSN *head1,ElemSN *head2)
{
	ElemSN *p,*tail,*h1=NULL,*s;
	while(head1->next&&head2->next){//找出两者最小的 
		if(head1->next->data>head2->next->data){
			p=head2->next;
			head2->next=p->next;
		}
		else{
			p=head1->next;
			head1->next=p->next;
		}
		p->next=NULL;
		if(!h1){
			h1=tail=p;
		}
		else{
			if(p->data!=tail->data){//判断是否为重复值,并挂链 
				tail=tail->next=p; 
			}
			else{
				free(p);
			}
		}
	}
		if(head1->next){//head2先跑完 
			s=head1->next;
			free(head2); 
		}
		else{
			s=head2->next;
		}
		while(s){
			p=s;
			s=s->next;
			p->next=NULL;
			if(p->data!=tail->data){//排除重复值 
				tail=tail->next=p;
			}
			else{//删除重复值 
				free(p);
			}
		}
		head1->next=h1;
} 
int main()
{
	int a[N],i,key,b[N];
	printf("Please input the number of a[N]:");
	for(i=0;i<N;i++){
		scanf("%d",&a[i]);
	}
	printf("\n");
	printf("Please input the number of b[N]:");
	for(i=0;i<N;i++){
		scanf("%d",&b[i]);
	}
	ElemSN *head1=NULL,*head2=NULL;
	//调用createlink函数   创建带表头单向链表 
	head1=Createlink(a);
	head2=Createlink(b);
	//删除关键字值为key的结点 
	Delkeynode(head1,head2);
	free(head2);
	//调用prelink函数  输出 
	Printlink(head1);
	return 0; 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值