diff --git a/homework240306/SqList.h b/homework240306/SqList.h new file mode 100644 index 0000000..e55349c --- /dev/null +++ b/homework240306/SqList.h @@ -0,0 +1,175 @@ +template +struct SqList // 顺序表 +{ + DT* elem; // 表首址 + int length; // 表长 + int size; // 表容量 +}; + +//算法2.1 +template +bool PriorElem_e(SqList
L, DT e, DT& pre_e) // 求值为e的元素前驱 +{ + int k; + k = LocateElem_e(L, e); // + if (k > 1) + { + GetElem_i(L, k - 1, pre_e); + return false; + } + else + return true; +} + +//【算法2.2】 初始化 +template +bool InitList(SqList
& L, int m) +{ + L.elem = new DT[m]; // 申请表空间 + if (L.elem == NULL) + { + cout << "未创建成功!"; // 申请不成功,退出 + exit(1); + } + L.length = 0; // 申请成功,属性赋值。空表,表长为0 + L.size = m; // 表容量为m + return true; +} + +//【算法2.3】 创建表元素 +template +bool CreateList(SqList
& L, int n) +{ + int i; + if (n > L.size) // 1.元素个数大于表容量,不能创建。 + { + cout << "元素个数大于表长,不能创建!" << endl; + return false; + } + cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值 + for (i = 1; i <= n; i++) + cin >> L.elem[i - 1]; + L.length = n; // 3.表长为创建的元素个数 + return true; +} + +//【算法2.4】 销毁顺序表 +template +void DestroyList(SqList
& L) +{ + delete[] L.elem; // 1.释放表空间 + L.length = 0; // 2.属性赋值 + L.size = 0; +} + +//【算法2.5】 获取第i个元素值 +template +bool GetElem_i(SqList
L, int i, DT& e) +{ + if (i<1 || i>L.length) // 1.位序不合理,返回false + { + cout << "该元素不存在!" << endl; + return false; + } + e = L.elem[i - 1]; // 2. 否则,获取第i个元素值 + return true; // 返回true +} + +//【算法2.6】 按值查找 +template +int LocateElem_e(SqList
L, DT e) +{ + for (int i = 1; i <= L.length; i++) // 顺序查找 + if (L.elem[i - 1] == e) // 1.找到 + return i; // 返回元素位序 + return 0; // 2.未找到,返回0 +} + +//【算法2.7】 +template +bool InsertElem_i(SqList
& L, int i, DT e) +{ + if (L.length >= L.size) // 1.表满,不能插入 + return false; + if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入 + return false; + for (int j = L.length; j >= i; j--) // 3. an~ai依次后移 + L.elem[j] = L.elem[j - 1]; + L.elem[i - 1] = e; + L.length++; + return true; // 插入成功,返回true +} + +//【算法2.8】 删除第i个元素 +template +bool DeleElem_i(SqList
& L, int i) +{ + if (L.length == 0) // 1.表空,不能删除 + return false; + if (i<1 || i>L.length) // 2.删除位置不合理,不能插入 + return false; + for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移 + L.elem[j - 1] = L.elem[j]; + L.length--; + return true; // 删除成功,返回true +} + + +//【算法2.9】 +template +bool PutElem(SqList
& L, int i, DT e) // 修改第i个元素的值 +{ + if (i<1 || i>L.length) // 1.位序不合理,不能修改, + return false; // 返回false + L.elem[i - 1] = e; // 2.重置第i个元素值 + return true; // 3.修改成功,返回true +} + +// 清空顺序表 +template +void ClearList(SqList
& L) +{ + L.length = 0; // 空表,表长为0 +} + + +// 测表长 +template +int ListLength(SqList
L) +{ + return L.length; +} + + +template +bool ListEmpty(SqList
L) // 测表空 +{ + if (L.length == 0) // 空表,返回true + return true; + else + return false; // 非空表,返回false +} + +template +bool ListFull(SqList
L) +{ + if (L.length == L.size) // 表满,返回true + return true; + else + return false; // 表不满,返回false +} + +//【算法2.10】 遍历输出 +template +void DispList(SqList
L) +{ + int i; + for (i = 1; i <= L.length; i++) // 依位序输出元素值 + { + cout << L.elem[i - 1] << "\t"; + + } + cout << endl; +} + + diff --git a/homework240306/homework240306.sln b/homework240306/homework240306.sln new file mode 100644 index 0000000..686bd06 --- /dev/null +++ b/homework240306/homework240306.sln @@ -0,0 +1,31 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34322.80 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "homework240306", "homework240306.vcxproj", "{9DF092C9-0251-46F4-A747-83AD359959A0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x64.ActiveCfg = Debug|x64 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x64.Build.0 = Debug|x64 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x86.ActiveCfg = Debug|Win32 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x86.Build.0 = Debug|Win32 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x64.ActiveCfg = Release|x64 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x64.Build.0 = Release|x64 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x86.ActiveCfg = Release|Win32 + {9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D125820C-4FDB-4C08-9519-7885288F1B5B} + EndGlobalSection +EndGlobal diff --git a/homework240306/homework240306.vcxproj b/homework240306/homework240306.vcxproj new file mode 100644 index 0000000..4c7b1c5 --- /dev/null +++ b/homework240306/homework240306.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {9df092c9-0251-46f4-a747-83ad359959a0} + homework240306 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/homework240306/homework240306.vcxproj.filters b/homework240306/homework240306.vcxproj.filters new file mode 100644 index 0000000..819b9d3 --- /dev/null +++ b/homework240306/homework240306.vcxproj.filters @@ -0,0 +1,27 @@ +锘 + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 澶存枃浠 + + + + + 婧愭枃浠 + + + \ No newline at end of file diff --git a/homework240306/main.cpp b/homework240306/main.cpp new file mode 100644 index 0000000..883feed --- /dev/null +++ b/homework240306/main.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; +#include "SqList.h" +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]; + } + } + A.length = k; +} +int main() +{ + SqList A; + SqList B; + InitList(A, 32); + InitList(B, 32); + int length_a, length_b; + cout << "请输入集合A的元素个数: "; + cin >> length_a; + CreateList(A, length_a); + cout << "请输入集合B的元素个数: "; + cin >> length_b; + CreateList(B, length_b); + cout << "集合A: "; + DispList(A); + cout << "集合B: "; + DispList(B); + Intersection(A, B); + cout << "A∩B: "; + DispList(A); + DestroyList(A); + DestroyList(B); + return 0; +} \ No newline at end of file