高分笔记考研oj——1324: 算法2-2:有序线性表的有序合并,zoj

题目描述:

已知线性表 LA 和 LB 中的数据元素按值非递减有序排列,现要求将 LA 和 LB 归并为一个新的线性表 LC, 且 LC 中的数据元素仍然按值非递减有序排列。例如,设LA=(3,5,8,11) ,LB=(2,6,8,9,11,15,20) 则
LC=(2,3,6,6,8,8,9,11,11,15,20)
算法描述如下:
从上述问题要求可知,LC中的数据元素或是LA中的数据元素,或是LB中的数据元素,则只要先设LC为空表,然后将LA或LB中的元素逐个插入到LC中即可。为使LC中元素按值非递减有序排列,可设两个指针 i 和 j 分别指向LA和LB中某个元素,若设 i 当前所指的元素为 a,j 所指的元素为 b,则当前应插入到 LC 中的元素 c 为 c = a < b ? a : b显然,指针 i 和 j 的初值均为1(实际写代码时往往是从 0 开始的),在所指元素插入 LC 之后,在 LA 或者 LB 中顺序后移。
输入:

有多组测试数据,每组测试数据占两行。第一行是集合A,第一个整数m0<=m<=100)代表集合A起始有m个元素,后面有m个非递减排序的整数,代表A中的元素。第二行是集合B,第一个整数n(0<=n<=100)代表集合B起始有n个元素,后面有n个非递减排序的整数,代表B中的元素。每行中整数之间用一个空格隔开。

输出:

每组测试数据只要求输出一行,这一行含有 m+n 个来自集合 A 和集合中的元素。结果依旧是非递减的。每个整数间用一个空格隔开。

样例输入:

4 3 5 8 11

7 2 6 8 9 11 15 20

样例输出:

2 3 5 6 8 8 9 11 11 15 20


 解题提示:1.当两个线性表都是空表的时候,仍然要输出一个空格

   2.可以用链表和顺序表两种方法来做

                   3.因为题中说明要测试多组数据,所以要一个循环

链表解法代码:

/*it is the answer to the question in my method*/

#include<iostream>
#include<string>
#include<sstream>
struct LNode{
	int data;
	LNode*next;
};
using namespace std;
void operation(LNode*const head, string s){//with head node
	stringstream in(s);
	LNode*p = head;
	int temp;
	while (in >> temp){
		p->next = new LNode;
		p = p->next;
		p->data = temp;
	}
	p->next = NULL;
}//function above without problem
LNode* merge(LNode*head1, LNode*head2){
	if (head1->data == 0){
		delete head1;
		return head2;
	}
	else if (head2->data == 0){
		delete head2;
		return head1;
	}
	LNode*p = head2, *head;
	head2 = head2->next;
	delete p;
	head = p = head1;
	head1 = head1->next;
	while (head1 != NULL&&head2 != NULL)
	if (head1->data < head2->data){
		p->next = head1;
		p = p->next;
		head1 = head1->next;
	}
	else{
		p->next = head2;
		p = p->next;
		head2 = head2->next;
	}
	if (head1 != NULL)p->next = head1;
	else p->next = head2;
	return head;
}
void print(LNode*head){
	if (head == NULL)return;
	else{
		printf(" %d", head->data);
		print(head->next);
		delete head;
	}
}//function above without problem
void inline pophead(LNode*&head){
	LNode*p = head;
	head = head->next;
	delete p;
}
int main(){
	string s;
	while (getline(cin, s)){
		LNode*head1 = new LNode;
		operation(head1, s);
		getline(cin, s);
		LNode*head2 = new LNode;
		operation(head2, s);
		pophead(head1);
		pophead(head2);//delete the empty head
		LNode*head = merge(head1, head2);
		pophead(head);//delete the head containing the length of one initial list
		if (head == NULL){ printf("\n"); continue; }
		printf("%d", head->data);
		pophead(head);
		print(head);
		printf("\n");
	}
	return 0;
}


顺序表解法代码:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
const int mz = 100;
void construct(int*arr, string s,int& len){
	stringstream in(s);
	in >> len;
	if (len == 0)return;
	else{
		int i = 0;
		while (i != len)
			in >> arr[i++];
	}
}
void print(int*arr1, int*arr2, int len1, int len2){
	if (len1 == 0 && len2 == 0){
		//printf("\n");
		return;
	}
	int i, j;
	i = j = 0;
	if (len2==0||len1!=0&&len2!=0&&arr1[i]<=arr2[j])printf("%d", arr1[i++]);
	else printf("%d", arr2[j++]);
	while (i != len1&&j != len2){
		if (arr1[i] <= arr2[j]) printf(" %d", arr1[i++]);
		else printf(" %d", arr2[j++]);
	}
	while (i != len1) printf(" %d", arr1[i++]);
	while (j != len2)printf(" %d", arr2[j++]);
	printf("\n");
}
int main(){
	string s;
	int arr1[mz], arr2[mz];
	int len1=0, len2=0;
	while (getline(cin, s)){
		construct(arr1, s, len1);
		getline(cin, s);
		construct(arr2, s, len2);
		print(arr1, arr2, len1, len2);
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值