Data structure Practice-sequence table: intersection of two sets

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.