鱼生富贵 2022-03-20 22:06 采纳率: 97.4%
浏览 53
已结题

返回一个布尔值判断是否为回文序列

不知道哪里不对,输入“1 2 2 1”输出却是“false”,而且检查了,逆置函数没错误的


#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<stdbool.h>
#define LEN sizeof(struct LinkList)
struct LinkList
{
    int num;
    struct LinkList* next;
};
int main()
{
    bool IfPalind(struct LinkList* A);
    void Creat(struct LinkList* L);
    struct LinkList* L, * check;
    L = (struct LinkList*)malloc(LEN); L->next = NULL;
    Creat(L);
    if (IfPalind(L))
        printf("true\n");
    else
        printf("false\n");
    check = L->next;
    while (check)
    {
        printf("%d", check->num);
        check = check->next;
    }
    return 0;
}
void Creat(struct LinkList *L)
{
    char c='\0'; int x;
    struct LinkList* p, * rear;
    rear = L;
    while (c != '\n')
    {
        scanf_s("%d", &x);
        c = getchar();
        p = (struct LinkList*)malloc(LEN);
        p->num = x;
        p->next = NULL;
        rear->next = p;
        rear = p;
    }
    rear->next = NULL;
}
bool IfPalind(struct LinkList* A)
{
    struct LinkList* mid, * fast, * p, * q, * head_new, * cur;
    mid = fast = A->next;
    while (fast&&fast->next != NULL&&fast->next->next!=NULL)
    {
        mid = mid->next;
        fast = fast->next->next;
    }
    head_new = mid; p = mid->next; q = p->next; head_new->next = NULL;
    while (q)
    { 
        p->next = head_new->next;
        head_new->next = p;
        p = q;
        q = q->next;
    }
    p->next = head_new->next;
    head_new->next = p;
    cur = A; mid = mid->next;
    for(;mid;)
    {
        if (cur->num == mid->num)
        {
            cur = cur->next;
            mid = mid->next;
        }
        else
            break;
    }
    if (mid)
        return false;
    else
        return true;
}
  • 写回答

1条回答 默认 最新

  • 魔法自动机 2022-03-21 13:00
    关注

    我对IfPalind函数进行了修改,你可以对照一下。

    bool IfPalind(struct LinkList* A)
    {
        struct LinkList* mid, *fast, *p, *q, *head_new, *cur;
        mid = fast = A->next;
        while (fast&&fast->next != NULL && fast->next->next != NULL)
        {
            mid = mid->next;
            fast = fast->next->next;
        }
    
        //这里开始和你写的不一样
        //以head_new头结点对mid后面子串进行原地逆置
        head_new = (struct LinkList*)malloc(LEN);
        head_new->next = NULL;
        p = mid->next;
        while (p)
        {
            q = p->next;
            p->next = head_new->next;
            head_new->next = p;
            p = q;
        }
        //判断head_new后半段逆置部分和A前半段部分是否相同
        p = A->next; q = head_new->next;
        while (p&&q) {
            if (p->num != q->num) {
                return false;
            }
            p = p->next;
            q = q->next;
        }
        return true;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 创建了问题 3月20日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器