From 8933bff3c7edc6c5cf34ca1d4cd459d242d7ce6d Mon Sep 17 00:00:00 2001 From: Launchcore Date: Fri, 15 Mar 2024 08:34:30 +0800 Subject: [PATCH] 2024-3-15 Small changes --- Intersection_Union/Linked_List/main.cpp | 13 ++++--------- Intersection_Union/Sequential_List/main.cpp | 16 ++-------------- 2 files changed, 6 insertions(+), 23 deletions(-) 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()