C语言用bool型返回值判断回文序列,越写越乱


#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct LinkList)
struct LinkList
{
    int num;
    struct LinkList* next;
};
int main()
{
    bool IfPalind(struct LinkList* head);
    char c = '\0';
    struct LinkList* p1 = NULL, * p2 = NULL, * head = NULL;
    while (c != '\n')
    {
        p1 = (struct LinkList*)malloc(LEN);
        scanf_s("%d", &p1->num);
        c = getchar();
        if (head == NULL)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
    }
    p1->next = NULL;
    if (IfPalind(head))
        printf("true\n");
    else
        printf("false\n");
    return 0;
}
bool IfPalind(struct LinkList* head)
{
    struct LinkList* mid=NULL, * fast=NULL, * p=NULL, * q=NULL, * head_new=NULL, * cur=NULL;
    mid = fast = head;
    while (fast&&fast->next != NULL)
    {
        mid = mid->next;
        fast = fast->next->next;
    }
    head_new = p = mid->next;
    p->next = NULL;
    while (p)
    { 
        q = p->next;
        p = q;
        head_new = p;
        q = q->next;
    }
    mid->next = head_new;
    mid = mid->next;
    cur = head;
    while (mid)
    {
        if (cur->num == mid->num)
        {
            cur = cur->next;
            mid = mid->next;
        }
        else
            break;
    }
    if (!mid)
        return 1;
    else
        return 0;
}

c中使用bool需要#include <stdbool.h>,然后可以用 bool a=true;

我感觉写的还行,挺好的,继续加油!