From f00d71aee6a7807f977deda695e5f23e5b97ea42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BA=9A=E8=8E=8E?= <2980083632@qq.com> Date: Fri, 27 Oct 2023 07:23:41 +0000 Subject: [PATCH] =?UTF-8?q?add=202224020123/chapter2/=E5=B0=86=E5=8D=95?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E6=8C=89=E5=9F=BA=E5=87=86=E5=88=92=E5=88=86?= =?UTF-8?q?.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张亚莎 <2980083632@qq.com> --- ...2\345\207\206\345\210\222\345\210\206.cpp" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 "2224020123/chapter2/\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" diff --git "a/2224020123/chapter2/\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/2224020123/chapter2/\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 00000000..f070a951 --- /dev/null +++ "b/2224020123/chapter2/\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,102 @@ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +typedef char ElemType; + +typedef struct Node { + ElemType data; + struct Node *next; +}LinkNode; + +void CreateListR(LinkNode *&L, ElemType a[], int n) +{ + LinkNode *node = NULL; + int i = 0; + L = (LinkNode *)malloc(sizeof(LinkNode)); + L->next = NULL;//创建头结点,其next域设置为NULL + + LinkNode *p = L; + for(i=0; idata = a[i]; + node->next = NULL; + p->next = node; + p = node; + } +} + +void DispList(LinkNode *L) +{ + LinkNode *p = L->next; + while(p) + { + printf("%c ",p->data); + p = p->next; + } + printf("\n"); +} + +void Split(LinkNode *L, ElemType x) +{ + //两个链表的头指针,用两个小链表储存小于和大于部分 + LinkNode* pLeftHead = (LinkNode *)malloc(sizeof(LinkNode));//小的 + LinkNode* pRightHead = (LinkNode *)malloc(sizeof(LinkNode));//大的 + //两个链表的当前最后一个元素 + LinkNode* left = pLeftHead; + LinkNode* right = pRightHead; + LinkNode* p = L->next; + while(p != NULL) + { + if (p->data < x) + { + left->next = p; + left = p; + } + else + { + right->next = p; + right = p; + } + p = p->next; + } + + //将right连接到left尾部,将两个链表合成一个 分割好的 + left->next = pRightHead->next; + right->next = NULL; + + L->next = pLeftHead->next; + + free(pLeftHead); + free(pRightHead); +} + +void DestroyList(LinkNode* L) +{ + LinkNode* node = L->next; + while(L != NULL) + { + node = L->next; + free(L); + L = node; + } +} + +int main() +{ + LinkNode *L = NULL; + ElemType a[] = "fgbedhca"; + int n = strlen(a); + CreateListR(L, a, n); + printf("划分前 L: "); + DispList(L); + ElemType x = 'd'; + printf("以%c进行划分\n", x); + Split(L, x); + printf("划分后 L: "); + DispList(L); + DestroyList(L); + + return 0; +} \ No newline at end of file -- Gitee