Static linked list implementation (A-B) U (B-A)

Source: Internet
Author: User

The yellow part of the figure is the practical meaning of (A-B) U (B-A), and the expression is implemented by using a structure array as a static linked list.

The approximate process is to create a linked list, and then iterate through the list of B elements entered in a chain. If it is not found, it is added to the end of the a linked list labeled Endpointer. If found, delete the node (recycled into the alternate list).

It is important to note that:

    • Every time I walk through a linked list with the elements of B, we stop at the Endpointer. Because Endpointer is followed by the elements in B, excluding the possibility of having the same element in B, only with the element in a (that is, the comparison before Endpointer)
    • In the code, the insertion of elements in B is inserted in the form of a flashback insert after Endpointer, so the first insert, instead, runs to the back of the sequence.
/*the algorithm iterates through each of the elements of B in a. Delete if found, otherwise, flashbacks are added after the endpointer location. S is the result chain. Time Complexity of O (m*n)*/#defineMAXSIZE 1000;typedefstruct{ElementType data; intcur;} Component,slinklist[maxsize];/*Initializes an array of space structures as an alternate list, 0 points to 2 ...*/voidInitspace (Slinklist &space) {     for(inti =0; i < MAXSIZE; i++) {Space[i].cur= i +1; } space[maxsize-1].cur =0;}/*Assign a node from an alternate list*/intMallocfromspace (Slinklist &space) {    inti = space[0].cur;//Space[0] is always the head node of the alternate list    if(space[0].cur)//determine if the alternate list is emptyspace[0].cur =space[i].cur; returni;}/*Recycle the Idle node labeled K, always add the node after the head node*/voidFreetospace (Slinklist &space,intk) {space[k].cur= space[0].cur; space[0].cur =K;}/*(A-B) U (B-A)*/voidDifference (slinklist &space,int&S) {intEndpointer,tmp,apre,anow;    Initspace (space); S= Mallocfromspace (space);//This is the head node of a set, and it's not loaded.Endpointer =S; scanf (m,n);//M is the number of elements of a set, and N is the number of elements in the B collection    /*start entering a collection*/     for(inti =0; I < m; i++) {tmp=Mallocfromspace (space); Space[endpointer].cur=tmp;        scanf (Space[tmp].data); Endpointer=tmp; } space[endpointer].cur=0;  for(intj =0; J < N; J + +) {scanf (b); Apre=S; Anow=space[s].cur;  while(Space[anow].data! = b && Anow! = space[endpointer].cur) {//The comparison to Endpointer is stopped, because unless there are duplicate elements in B, there is no need to compare the elements in B to BApre =Anow; Anow=space[anow].cur; }        if(Space[anow].data = = b) {//have aSpace[apre].cur =space[anow].cur;            Freetospace (Space,anow); if(Anow = =endpointer) Endpointer= Apre;//this endpointer always points to the last element of a.}Else{//No            intTMP =Mallocfromspace (space); //Insert the elements of B that are not in a in a flashbackSpace[i].cur =space[endpointer].cur; Space[endpointer].cur=i; }    }}
View Code

Static linked list implementation (A-B) U (B-A)

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.