"Project-Find set merge Set"
Suppose that two sets a and B are represented by two linear table LA and LB, that is, the data element in the linear table is a member of the collection. Design algorithm, using function unionlist (list LA, list LB, List &LC) function to implement the algorithm, to find a new set of C=a∪b, will be two sets of the set in a linear table LC.
Tips:
(1) In addition to implementing the Unnionlist function, you also need to design the code in the main function, call unionlist for testing and demonstration;
(2) can make full use of the built-in algorithm library [click ...], directly on the head of the program #include<list.h>
(the most common method of the project, recommended adoption);
(3) It is also possible to implement a function corresponding to the basic operation of the linear table required in the algorithm, in the same file as all the programs designed by them.
[Reference Solution]
#include "list.h"#include <stdio.h>voidUnionlist (sqlist *la, SqList *lb, SqList *&lc) {intLena,i; Elemtype e; Initlist (LC); for(i=1; I<=listlength (LA); i++)//Insert all elements of LA into the LC{Getelem (la,i,e); Listinsert (lc,i,e); } lena=listlength (LA);//Find the length of the linear table La for(i=1; I<=listlength (LB); i++) {Getelem (lb,i,e);//Take the first data element of LB to e if(! Locateelem (La,e))The //la does not exist in the same person as E, inserted into the LCListinsert (lc,++lena,e); }}//write test code in mainintMain () {sqlist *sq_a, *sq_b, *sq_c; Elemtype a[6]= {5,8,7,2,4,9}; CreateList (Sq_a, A,6);printf("LA:"); Displist (sq_a); Elemtype b[6]= {2,3,8,6,0}; CreateList (Sq_b, B,5);printf("LB:"); Displist (Sq_b); Unionlist (Sq_a, Sq_b, Sq_c);printf("LC:"); Displist (Sq_c);return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure Practice-sequence table: intersection of two sets