以下是一个生成判断链表 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;
}