Data Structure practice-sequence table: intersection of two sets and intersection of data structures
[Project-calculate the union of sets]
Assume that two sets A and B are represented by two linear tables LA AND LB respectively, that is, the data element in the linear table is A member in the set. Design the algorithm. Use the unionList (List LA, List LB, List & LC) function to implement the algorithm and obtain A new set C = A ∪ B, place the union of the two sets in the linear table LC.
Tip:
(1) In addition to implementing the unnionList function, you also need to design code in the main function and call unionList for testing and demonstration;
(2) Make full use of the previously created Algorithm Library [Click…], Add directly in the program Header#include<list.h>(The most common method in the project, recommended );
(3) You can also store the functions corresponding to the basic operations of linear tables required by the algorithm in the same file as all programs you have designed.
[Reference]
# Include "list. h "# include <stdio. h> void unionList (SqList * LA, SqList * LB, SqList * & LC) {int lena, I; ElemType e; InitList (LC); for (I = 1; I <= ListLength (LA); I ++) // insert all the elements of LA into Lc {GetElem (LA, I, e); ListInsert (LC, I, e);} lena = ListLength (LA); // obtain the length of the linear LA table for (I = 1; I <= ListLength (LB); I ++) {GetElem (LB, I, e); // obtain the I-th Data Element in LB and assign it to e if (! LocateElem (LA, e) // The LA does not have the same person as e, and is inserted into the LC ListInsert (LC, ++ lena, e );}} // use main to write the test code int main () {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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.