diff --git a/Intersection_Union/Linked_List/main.cpp b/Intersection_Union/Linked_List/main.cpp
index 11c6515..c089233 100644
--- a/Intersection_Union/Linked_List/main.cpp
+++ b/Intersection_Union/Linked_List/main.cpp
@@ -32,10 +32,7 @@ void Union(LNode
*& A, LNode*& B, int length_a, int length_b)
{
int temp;
GetElem_i(B, i, temp); // You should manually get the element instead of using B.elem[i]
- if (LocateElem_e(A, temp) == 0)
- {
- InsertElem_i(A, ++length_a, temp); // ++length_a to always insert at the end
- }
+ if (LocateElem_e(A, temp) == 0) InsertElem_i(A, ++length_a, temp); // ++length_a to always insert at the end
}
}
template
@@ -48,7 +45,8 @@ void Intersection(LNode*& A, LNode*& B, int length_a)
GetElem_i(A, i, temp);
if (LocateElem_e(B, temp) == 0)
{
- DeleElem_i(A, i);
+ DeleElem_i(A, i--);
+ length_a--;
}
}
}
@@ -72,10 +70,7 @@ int main()
Union(A, B, length_a, length_b); // You should manually pass the length of the list instead of using A.length
cout << "A∪B: ";
DispList(A);
- while (true)
- {
- if (!DeleElem_i(A, length_a + 1)) break; // Delete elements after the original list A
- }
+ while (true) if (!DeleElem_i(A, length_a + 1)) break; // Delete elements after the original list A
Intersection(A, B, length_a);
cout << "A∩B: ";
DispList(A);
diff --git a/Intersection_Union/Sequential_List/main.cpp b/Intersection_Union/Sequential_List/main.cpp
index 44d39c2..a1fbc2f 100644
--- a/Intersection_Union/Sequential_List/main.cpp
+++ b/Intersection_Union/Sequential_List/main.cpp
@@ -25,25 +25,13 @@ bool CreateListNoRepeat(SqList& L, int n)
template
void Union(SqList& A, const SqList& B)
{
- for (int i = 0; i < B.length; ++i)
- {
- if (LocateElem_e(A, B.elem[i]) == 0)
- {
- InsertElem_i(A, A.length + 1, B.elem[i]);
- }
- }
+ for (int i = 0; i < B.length; ++i) if (LocateElem_e(A, B.elem[i]) == 0) InsertElem_i(A, A.length + 1, B.elem[i]);
}
template
void Intersection(SqList& A, const SqList& B)
{
int k = 0;
- for (int i = 0; i < A.length; ++i)
- {
- if (LocateElem_e(B, A.elem[i]) > 0)
- {
- A.elem[k++] = A.elem[i];
- }
- }
+ for (int i = 0; i < A.length; ++i) if (LocateElem_e(B, A.elem[i]) > 0) A.elem[k++] = A.elem[i];
A.length = k;
}
int main()