OneCompiler

union two linear list. ( A = AUB)

260

Question

The two linear tables LA and LB respectively represent two sets A and B (the data elements in the linear table are members of the sets), which are combined into one set.

Cases

input:
  A = (1,2,3,4);
  B = (1,4,5,6);
output:
  A = (1,2,3,4,5,6);

Click to jump to the full runnable code

Pseudocode

Status Compare(ElemType a, ElemType b) {
  if (a == b){
    return TRUE;
  }
  return FALSE
}

Status Union(SqList* A, SqList B) {
  // traverse B, get the element bi of B.
  for (int i = 1; i <= B.length; i++) {
    GetElem(B, i, bi)
    // find bi that does not exist in A
    if (LocateElem(*A, bi, Compare) == 0) {
      // insert it at the end of A
      ListInsert(A, (*A).length + 1, bi);
    }
  }
}