题目描述
已知线性表 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)
输入
有多组测试数据,每组测试数据占两行。第一行是集合A,第一个整数m(0<=m<=100)代表集合A起始有m个元素,后面有m个非递减排序的整数,代表A中的元素。第二行是集合B,第一个整数n(0<=n<=100)代表集合B起始有n个元素,后面有n个非递减排序的整数,代表B中的元素。每行中整数之间用一个空格隔开。
输出
每组测试数据只要求输出一行,这一行含有 m+n 个来自集合 A 和集合B 中的元素。结果依旧是非递减的。每个整数间用一个空格隔开。
#include <iostream>
using namespace std;
struct LinkNode {
int data;
LinkNode* link;
LinkNode(LinkNode* ptr = NULL) { link = ptr; }
LinkNode(int x) { data = x; }
LinkNode(LinkNode* ptr = NULL, int a = 0) { data = a; link = ptr; }
};
class List {
public:
LinkNode* first;
List(LinkNode* p = NULL, int x = NULL) { first = new LinkNode(p, x); }
~List() { makeempty(); }
void makeempty();
LinkNode* Locate(int i);
bool insert(int i, int& x); //第i个后插结点
void sort(List& a, List& b, int a1, int b1);
bool remove(int i); //删除第i个结点
void input(int& x);
void output();
};
void List::makeempty() {
LinkNode* q;
while (first->link != NULL) {
q = first->link;
first->link = q->link;
delete q;
}
}
LinkNode* List::Locate(int i){
if (i < 0) return NULL;
LinkNode* current = first;
int k = 0;
while (current != NULL && k < i) {
current = current->link; k++;
}
return current;
}
bool List::remove(int i) {
LinkNode* current = this->Locate(i - 1);
if (current == NULL || current->link == NULL)return false;
LinkNode* del = current->link;
current->link = del->link;delete del;
return true;
}
bool List::insert(int i, int& x) {
LinkNode* current = Locate(i);
if (current == NULL)return false;
LinkNode* newNode = new LinkNode(x);
if (newNode == NULL) { cerr << "存储分配错误!" << endl; exit(1); }
newNode->link = current->link;
current->link = newNode;
return true;
}
void List::input(int &x) { //x为建立表中结点个数
LinkNode* last = first;
for (int i = 0; i < x; i++) {
LinkNode* newNode = new LinkNode(NULL,NULL);
cin >> (newNode->data);
last->link = newNode;last = newNode;
}
last->link = NULL;
}
void List::output() {
LinkNode* p = first->link;
while (p != NULL) {
cout << p->data<<" ";
p = p->link;
}
}
void List::sort(List& a, List& b,int a1,int b1) { //有序插入
List c;
int mark = 0;
int sizea = a1; int sizeb = b1; char flag=' ';
int i = 0, x = 0, min = a.first->link->data, count = 0;
for (int j = 0,k = 0,l = 0; j < a1+b1; j++) {
for (; k < sizea; k++) {
if (min >= (a.Locate(k + 1)->data)) {
min = (a.Locate(k + 1)->data); flag = 'a'; mark = k + 1;
}
}
for (; l < sizeb; l++) {
if (min >= (b.Locate(l + 1)->data)) {
min = (b.Locate(l + 1)->data); flag = 'b'; mark = l + 1;
}
}
if (flag == 'a') { a.remove(mark); sizea--; }
else { b.remove(mark); sizeb--; }
count++; k = 0; l = 0;flag = ' ';
c.insert(count-1, min);
if (a.first->link == NULL && b.first->link == NULL)break;
else if (min = a.first->link == NULL)min = b.first->link->data;
else min = a.first->link->data;
} c.output();
};
int main()
{
List A, B, C;
int sizea, sizeb;
cin >> sizea;
if (sizea >= 0 && sizea <= 100)A.input(sizea);
cin >> sizeb;
if (sizeb >= 0 && sizeb <= 100 && sizea >= 0 && sizea <= 100) {
B.input(sizeb);
C.sort(A, B, sizea, sizeb); //A,B合入C中
}
return 0;
}