两个循环链合并

思路:

1、写循环链结构;

2、写两个循环链合并操作。

#include <iostream>
using namespace std;
#include <stdlib.h>

typedef struct cyclink {
	int data;
	struct cyclink *next;
	//struct cyclink *head;
}cyclink,*link;

void create_cyclink(link &h,int num) {
	int i;
	link p,s;

	//p = l;
	h = (link)malloc(sizeof(cyclink));
	p = h;
	h ->data = num;
	//l ->next = l;

	for(i = 0;i < num;i++) {
		
		s = (link)malloc(sizeof(cyclink));
	
		cin >> s ->data;
		p ->next = s;
		p = s;
	}
	//h = h ->next;
	p ->next = h;
//	h = h ->next;

/*	l ->next = l;

	for(i = num;i > 0; --i) {
		p = (link)malloc(sizeof(cyclink));
		cin>> p ->data;
		p ->next = l ->next;
		l ->next = p;
	}
*/
}


void cat_link(link &lc,link la,link lb) {
	link ha,hb,hc;
	int length;
	ha = la;
	hb = lb;
	length = (ha ->data) + (hb ->data);

	while(lb ->next != hb) lb = lb ->next;
	//lb = lb ->next;
	lb ->next = ha;

	while(la ->next != ha) la = la ->next;
	hb = hb ->next;
	la ->next = hb;

	lc = (link)malloc(sizeof(cyclink));
	hc = lc;
	lc ->data = length;
	while(la ->next != ha) la = la ->next;
	hc = la ->next;
	lc = hc;

}

void print_cyclink(link l) {
	link h;
	h = l;
	while(l ->next != h ) {
		l = l ->next;
		cout << l ->data<<", ";
	}
	
}

int main() {
	int i;
	link la,lb,lc;

	cout<<"num1 = ";
    cin>>i;

	create_cyclink(la,i);
	print_cyclink(la);
	cout<<endl;

	cout<<"num2 = ";
    cin>>i;

	create_cyclink(lb,i);
	print_cyclink(lb);
	cout<<endl;

	cat_link(lc,la,lb);
	print_cyclink(lc);

	free(lc);
	free(lb);
	free(la);
	while(1);
	return 0;
}


执行结果图:

 

总结:

此程序还有一些问题存在,

就是到最后打印合并后链的时候,

程序执行有问题,但是结果不影响。

这个问题要想想是不是搜索过头!~

 

修改:

两个链并联函数,冗余有一个链。

删减一个

 

#include <iostream>
using namespace std;
#include <stdlib.h>

typedef struct cyclink {
	int data;
	struct cyclink *next;
}cyclink,*link;

void create_cyclink(link &h,int num) {
	int i;
	link p,s;

	h = (link)malloc(sizeof(cyclink));
	p = h;

	for(i = 0;i < num;i++) {
		
		s = (link)malloc(sizeof(cyclink));
	
		cin >> s ->data;
		p ->next = s;
		p = s;
	}
	p ->next = h;

}


void cat_link(link &la,link &lb) {
	link ha,hb;
	int length;
	ha = la;
	hb = lb;

	while(lb ->next != hb) lb = lb ->next;
	lb ->next = ha;

	while(la ->next != ha) la = la ->next;
	hb = hb ->next;
	la ->next = hb;

	while(la ->next != ha) la = la ->next;
	la = la ->next;

}

void print_cyclink(link l) {
	link h;
	h = l;
	while(l ->next != h ) {
		l = l ->next;
		cout << l ->data<<", ";
	}
	
}

int main() {
	int i;
	link la,lb;

	cout<<"num1 = ";
    cin>>i;

	create_cyclink(la,i);
	print_cyclink(la);
	cout<<endl;

	cout<<"num2 = ";
    cin>>i;

	create_cyclink(lb,i);
	print_cyclink(lb);
	cout<<endl;

	cat_link(la,lb);
	print_cyclink(la);


	free(lb);
	free(la);
	while(1);
	return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值