C語言合並兩個集合(L,L1) 將L1中不在L中的元素插入到L線性表中
void main()
{
Sqlist L,L1;
InitList(&L);
InitList(&L1);
ListInsert(&L, 1, 2);
ListInsert(&L, 2, 3);
ListInsert(&L, 1, 1);
ListInsert(&L1,1,1);
ListInsert(&L1,2,2);
ListInsert(&L1,3,4);
for (int i = 0; i <L1.length; i++)
{
int flag = 0;
for (int j = 0; j < L.length; j++)
{
if (L1.elem[i] == L.elem[j]) {
flag = 1;
break;
}
}
if (!flag) {
ListInsert(&L, (L.length + 1), L1.elem[i]);
}
}
//輸出順序鏈表中的所有值
for (int i = 0; i < L.length; i++)
{
printf("元素的第%d值為%d\n", i + 1, L.elem[i]);
}
//int del_e = 0;
//ListDelete(&L, 3, del_e);
//printf("刪除的元素為%d\n", del_e);
//printf("輸出刪除後的線性表\n");
//輸出順序鏈表中的所有值
/*for (int i = 0; i < L.length; i++)
{
printf("元素的第%d值為%d\n", i + 1, L.elem[i]);
}*/
}
該算法的時間復雜度f(n2)
C語言合並兩個集合(L,L1) 將L1中不在L中的元素插入到L線性表中