高分筆記考研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;
}



發佈了39 篇原創文章 · 獲贊 18 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章