文章

84

粉丝

408

获赞

34

访问

878.0k

头像
链表合并(c++)
P1025 贵州大学2019机试
发布于2020年3月23日 10:18
阅读数 12.0k

#include<iostream>
using namespace std;
typedef struct Node{
	int number;
	struct Node* next;
}LNode;
int main()
{
	int s1, s2, x;
	LNode *head1 = new LNode, *head2 = new LNode, *head3 = new LNode, *p, *q, *r;
	//建表 
	p = head1;
	cin >> s1;
	for (int i = 0; i < s1; i++) {
		cin >> x;
		p->next = new LNode;
		p = p->next;
		p->number = x;
		p->next = NULL;
	}
	p = head2;
	cin >> s2;
	for (int i = 0; i < s2; i++) {
		cin >> x;
		p->next = new LNode;
		p = p->next;
		p->number = x;
		p->next = NULL;
	}
	//合并 
	p = head3, q = head1->next, r = head2->next;
	while (q != NULL && r != NULL) {
		if (q->number < r->number) {
			p->next = q;
			q = q->next;
		}
		else {
			p->next = r;
			r = r->next;
		}
		p = p->next;
	}
	if (q) {
		p->next = q;
	}
	if (r) {
		p->next = r;
	}
	//输出 
	bool flag = false;//控制输出 
	p = head3->next;
	while ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发