diff --git "a/2209040026/chapter2/exp-6\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2209040026/chapter2/exp-6\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..cbcdd2c67204b4ed4a92650ff839950ad3d1afab --- /dev/null +++ "b/2209040026/chapter2/exp-6\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" @@ -0,0 +1,40 @@ +#include +#include"linklist.cpp" + +void Split(LinkNode *&L) +{ + LinkNode *pre,*p; + if(L->next == NULL || L->next->next == NULL) + return; + int x = L->next->data; // 将首节点赋给x + pre = L->next; + p = pre->next; + while(p!=NULL) + { + if(p->data < x) + { + pre->next = p->next; + p->next = L->next; // 将节点p插到表头 + L->next = p; + p = pre->next; + } + else + { + pre = p; // p,pre同步后移 + p= pre->next; + } + } +} +int main() +{ + LinkNode *L; + ElemType a[] = {23,41,2,15,12,90,66,9}; + int n = sizeof(a)/sizeof(int); // 求整型数组的长度 + CreateListR(L,a,n); // 创建链表 + printf("L: "); DispList(L); + printf("以首节点进行划分\n"); + Split(L); + printf("L: "); DispList(L); + DestroyList(L); // 销毁链表 + return 0; + } \ No newline at end of file diff --git "a/2209040026/chapter2/lc-5\345\222\214\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" "b/2209040026/chapter2/lc-5\345\222\214\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ab9f6cb392a9e81c5c79c1c03b613fa4bd6a663f --- /dev/null +++ "b/2209040026/chapter2/lc-5\345\222\214\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" @@ -0,0 +1,58 @@ +#include +#include +using namespace std; + +void sort_array(vector& nums1, int m, vector& nums2, int n) { + int len1 = m - 1, len2 = n - 1, len = m + n - 1; + + while (len1 >= 0 && len2 >= 0) { + + if (nums1[len1] > nums2[len2]) { + nums1[len--] = nums1[len1--]; + } + else { + nums1[len--] = nums2[len2--]; + } + } + while (len2 >= 0) { + nums1[len--] = nums2[len2--]; + } +} + + +void print_array(vector& nums1) { + + for (int i = 0; i < nums1.size(); ++i){ + cout << nums1[i] << " "; + } +} + +int main() { + int m, n, number; + vector nums1, nums2; + + cout << "nums1 = "; + while (cin >> number) { + nums1.push_back(number); + if (cin.get() == '\n'){ + break; + } + } + + cout << "nums2 = "; + while (cin >> number) { + nums2.push_back(number); + if (cin.get() == '\n') { + break; + } + } + + n = nums2.size(); + m = nums1.size() - n; + + sort_array(nums1, m, nums2, n); + cout << "Merged nums1: "; + print_array(nums1); + + return 0; +} \ No newline at end of file