C语言代码:有两个单链C和D,判断D是否为C的子链

以下是一个生成判断链表 D 是否为链表 C 的子链的 C 语言代码示例:

```c
#include <stdio.h>
#include <stdbool.h>

// 定义链表节点
struct ListNode {
    int val;
    struct ListNode *next;
};

// 判断链表 D 是否为链表 C 的子链
bool isSubList(struct ListNode *C, struct ListNode *D) {
    if (D == NULL) {
        return true;  // 空链表是任何链表的子链
    }

    struct ListNode *currentC = C;
    struct ListNode *currentD = D;

    // 遍历链表 C
    while (currentC != NULL) {
        // 如果当前节点的值与链表 D 的头节点的值相等
        if (currentC->val == currentD->val) {
            // 检查链表 D 是否是链表 C 的子链
            struct ListNode *tempC = currentC;
            struct ListNode *tempD = currentD;

            while (tempC != NULL && tempD != NULL && tempC->val == tempD->val) {
                tempC = tempC->next;
                tempD = tempD->next;
            }

            if (tempD == NULL) {
                return true;  // 链表 D 是链表 C 的子链
            }
        }

        currentC = currentC->next;
    }

    return false;  // 链表 D 不是链表 C 的子链
}

// 创建链表节点
struct ListNode* createNode(int val) {
    struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

int main() {
    // 创建链表 C
    struct ListNode *C = createNode(1);
    C->next = createNode(2);
    C->next->next = createNode(3);
    C->next->next->next = createNode(4);
    C->next->next->next->next = createNode(5);

    // 创建链表 D
    struct ListNode *D = createNode(2);
    D->next = createNode(3);
    D->next->next = createNode(4);

    // 判断链表 D 是否是链表 C 的子链
    bool isSub = isSubList(C, D);

    if (isSub) {
        printf("链表 D 是链表 C 的子链\n");
    } else {
        printf("链表 D 不是链表 C 的子链\n");
    }

    // 释放链表 C 的内存
    struct ListNode *current = C;
    while (current != NULL) {
        struct ListNode *temp = current;
        current = current->next;
        free(temp);
    }

    // 释放链表 D 的内存
    current = D;
    while (current != NULL) {
        struct ListNode *temp = current;
        current = current->next;
        free(temp);
    }

    return 0;
}
```

这段代码定义了一个 `isSubList` 函数,它接受两个链表指针 `C` 和 `D`,并判断链表 `D` 是否为链表 `C` 的子链。函数通过遍历链表 `C`,在每个节点处检查链表 `D` 是否与 `C` 的子链匹配。

在 `main` 函数中,我们创建了链表 `C` 和链表 `D` 的示例,并调用 `isSubList` 函数来判断链表 `D` 是否为链表 `C` 的子链。根据结果打印相应的信息。

请注意,这只是一个示例代码,你可以根据自己的需求进行修改和扩展。另外,为了完整性和正确性,请确保在使用完链表后释放其内存以避免内存泄漏。

#include <stdio.h>
#include <stdbool.h>

// 定义链表节点
struct ListNode {
    int val;
    struct ListNode *next;
};

// 判断链表 D 是否为链表 C 的子链
bool isSubList(struct ListNode *C, struct ListNode *D) {
    if (D == NULL) {
        return true;  // 空链表是任何链表的子链
    }

    struct ListNode *currentC = C;
    struct ListNode *currentD = D;

    // 遍历链表 C
    while (currentC != NULL) {
        // 如果当前节点的值与链表 D 的头节点的值相等
        if (currentC->val == currentD->val) {
            // 检查链表 D 是否是链表 C 的子链
            struct ListNode *tempC = currentC;
            struct ListNode *tempD = currentD;

            while (tempC != NULL && tempD != NULL && tempC->val == tempD->val) {
                tempC = tempC->next;
                tempD = tempD->next;
            }

            if (tempD == NULL) {
                return true;  // 链表 D 是链表 C 的子链
            }
        }

        currentC = currentC->next;
    }

    return false;  // 链表 D 不是链表 C 的子链
}

// 创建链表节点
struct ListNode* createNode(int val) {
    struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

int main() {
    // 创建链表 C
    struct ListNode *C = createNode(1);
    C->next = createNode(2);
    C->next->next = createNode(3);
    C->next->next->next = createNode(4);
    C->next->next->next->next = createNode(5);

    // 创建链表 D
    struct ListNode *D = createNode(2);
    D->next = createNode(3);
    D->next->next = createNode(4);

    // 判断链表 D 是否是链表 C 的子链
    bool isSub = isSubList(C, D);

    if (isSub) {
        printf("链表 D 是链表 C 的子链\n");
    } else {
        printf("链表 D 不是链表 C 的子链\n");
    }

    // 释放链表 C 的内存
    struct ListNode *current = C;
    while (current != NULL) {
        struct ListNode *temp = current;
        current = current->next;
        free(temp);
    }

    // 释放链表 D 的内存
    current = D;
    while (current != NULL) {
        struct ListNode *temp = current;
        current = current->next;
        free(temp);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要什么私信我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值