📌  相关文章
📜  C程序检查字符串的链接列表是否形成回文(1)

📅  最后修改于: 2023-12-03 15:00:13.260000             🧑  作者: Mango

C程序检查字符串的链接列表是否形成回文

在计算机科学中,“回文”是指一个字符串从前往后和从后往前读都一样,比如“racecar”和“level”。这里我们要编写一个C程序来检查一个链接列表中的字符串是否形成回文。

链接列表

一个链接列表是由多个节点(node)所构成的数据结构,每个节点都包含一个值(value)和一个指向下一个节点的指针(next)。下面是一个例子:

struct ListNode {
    char* value;
    struct ListNode* next;
};

struct ListNode* head = NULL;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->value = "r";
head->next = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next->value = "a";
head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next->next->value = "c";
head->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next->next->next->value = "e";
head->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next->next->next->next->value = "c";
head->next->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next->next->next->next->next->value = "a";
head->next->next->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next->next->next->next->next->next->value = "r";
head->next->next->next->next->next->next->next = NULL;

这个链表中包含字符串“racecar”,它是一个回文。

程序实现

首先我们需要从链表中提取出所有的字符串,并存储到一个数组中。下面是实现代码:

int length = 0;
struct ListNode* current = head;
while (current != NULL) {
    length++;
    current = current->next;
}

char* strings[length];
current = head;
for (int i = 0; i < length; i++) {
    strings[i] = current->value;
    current = current->next;
}

然后我们需要检查这些字符串是否构成回文。我们可以分别从字符串的两端开始比较,依次向中间靠拢。下面是实现代码:

int isPalindrome = 1;
for (int i = 0; i < length / 2; i++) {
    int j = length - i - 1;
    if (strcmp(strings[i], strings[j]) != 0) {
        isPalindrome = 0;
        break;
    }
}

最后,我们可以根据检查结果输出一些提示信息。下面是实现代码:

if (isPalindrome) {
    printf("The linked list forms a palindrome.");
} else {
    printf("The linked list does not form a palindrome.");
}

完整程序如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct ListNode {
    char* value;
    struct ListNode* next;
};

int main() {
    struct ListNode* head = NULL;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->value = "r";
    head->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next->value = "a";
    head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next->next->value = "c";
    head->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next->next->next->value = "e";
    head->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next->next->next->next->value = "c";
    head->next->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next->next->next->next->next->value = "a";
    head->next->next->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next->next->next->next->next->next->value = "r";
    head->next->next->next->next->next->next->next = NULL;

    int length = 0;
    struct ListNode* current = head;
    while (current != NULL) {
        length++;
        current = current->next;
    }

    char* strings[length];
    current = head;
    for (int i = 0; i < length; i++) {
        strings[i] = current->value;
        current = current->next;
    }

    int isPalindrome = 1;
    for (int i = 0; i < length / 2; i++) {
        int j = length - i - 1;
        if (strcmp(strings[i], strings[j]) != 0) {
            isPalindrome = 0;
            break;
        }
    }

    if (isPalindrome) {
        printf("The linked list forms a palindrome.");
    } else {
        printf("The linked list does not form a palindrome.");
    }

    return 0;
}

输出结果为:The linked list forms a palindrome.