数据结构线性表顺序表示 (三)

基于”线性表的顺序表示”应用篇---求集合A和B的并 A’ = A U B:

头文件:seqlistu.h

#include "seqlist.h"

template <class T>
void Union(SeqList<T> &LA, SeqList<T> LB) 
{
	T x;
	for (int i = 0; i < LB.Length(); i++) {
		LB.Find(i,x);
		if(LA.Search(x) == -1) {
			LA.Insert(LA.Length() - 1, x);
		}
	}
}

 

主函数:test.cpp

 

#include "seqlistu.h"

const int SIZE = 100;

int main() 
{
	SeqList<int> LA(SIZE);
	SeqList<int> LB(SIZE);

	for( int i = 0; i < 5; i++ ) {
		LA.Insert(i-1,i);
	}
	// LA = {0,1,2,3,4};
	for( i = 5; i < 10; i++) {
		LB.Insert(i-6,i);
	}
	// LB = {5,6,7,8,9};

	LB.Insert(-1,0);
	LB.Insert(3,2);
	LB.Insert(LB.Length() - 1, 4);
	// LB = {0,5,6,7,2,8,9,4};

	LA.Output(cout);
	LB.Output(cout);

	Union(LA,LB);
	LA.Output(cout);
	// 输出: 0 1 2 3 4 5 6 7 8 9 

	return 0;
}
原文地址:https://www.cnblogs.com/matrix77/p/2189509.html