鱼C论坛

 找回密码
 立即注册
查看: 554|回复: 1

[已解决]看是不是回文单链表(一直打印不了,是不是malloc申请的空间不连续啊)

[复制链接]
发表于 2023-6-13 15:46:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

void input(struct List1 *a,struct List2 *b,int i);

struct List1
{
        int i;
        struct List1 *next;
};
struct List2
{
        struct List2 *next;
        int i;
};

void input(struct List1 *a,struct List2 *b,int i)
{
        struct List1 *xin_a;
        struct List1 *temp1;
        struct List2 *xin_b;
        struct List2 *temp2;
       
        xin_a=(struct List1 *)malloc(sizeof(struct List1));
        xin_b=(struct List2 *)malloc(sizeof(struct List2));
       
        if(xin_a==NULL&&xin_b==NULL)
        {
                printf("内存分配失败:\n");
                exit(1);
        }
        xin_a->i =i;
        xin_b->i =i;       
       
        if(a==NULL)
        {
                a=xin_a;
                xin_a->next =NULL;
        }
        else
        {
                temp1=a;
                while(temp1->next !=NULL)
                {
                        temp1=temp1->next ;
                }
                temp1->next=xin_a;
                xin_a->next =NULL;
        }
       
       
        if(b==NULL)
        {
                b=xin_b;
                xin_b->next =NULL;
        }
        else
        {
                temp2=b;
                b=xin_b;
                xin_b->next =temp2;
        }
       
}

int determine(struct List1 *a,struct List2 *b)
{
        while(a!=NULL&&b!=NULL)
        {
                if(a->i !=b->i)
                {
                        return 0;
                        break;
                }
        }
        return 1;
}

void print(struct List1 *a)
{
        struct List1 *cur;
        cur=a;
       
        while(cur!=NULL)
        {
                printf("%d ",cur->i );
                cur=cur->next ;
        }
}

int main(void)
{
        struct List1 *a=NULL;
        struct List1 *temp1=NULL;
        struct List2 *b=NULL;
        struct List2 *temp2=NULL;
       
        int i;
        while(1)
        {
                printf("请输入一个整数(输入-1表示结束):");
                scanf("%d",&i);
                if(i==-1)
                {
                        break;
                }
                else
                {
                        input(a,b,i);
                }
        }
       
        printf("你输入的单链表是: ");
        print(a);
        putchar('\n');
        if(determine(a,b))
        {
                printf("录入的单链表是回文数列!\n");
        }
        else
        {
                printf("录入的单链表不是回文数列!\n");
        }
        while(a!=NULL)
        {
                temp1=a->next ;
                free(a);
                a=temp1;
        }
        while(b!=NULL)
        {
                temp2=b->next ;
                free(b);
                b=temp2;
        }
        return 0;
}
最佳答案
2023-6-13 15:56:03
你的问题出现在你传递指针到函数的方式上。在C语言中,所有的函数参数都是按值传递的。所以当你在函数中修改一个指针,你其实只是修改了这个指针的副本,而不是原来的指针。因此,你的 input 函数中的 a 和 b 的更改不会影响到 main 函数中的 a 和 b 。

你应该将指针的地址传递给函数,而不是指针本身。你可以通过将指针的指针传递给函数来实现这一点。这就是使用双指针的情况。

以下是修改后的代码:
#include<stdio.h>
#include<stdlib.h>

struct List
{
        int i;
        struct List *next;
};

void input(struct List a,struct List b,int i)
{
        struct List *xin_a;
        struct List *temp1;
        struct List *xin_b;
        struct List *temp2;
       
        xin_a=(struct List *)malloc(sizeof(struct List));
        xin_b=(struct List *)malloc(sizeof(struct List));
       
        if(xin_a==NULL&&xin_b==NULL)
        {
                printf("内存分配失败:\n");
                exit(1);
        }
        xin_a->i =i;
        xin_b->i =i;       
       
        if(*a==NULL)
        {
                *a=xin_a;
                xin_a->next =NULL;
        }
        else
        {
                temp1=*a;
                while(temp1->next !=NULL)
                {
                        temp1=temp1->next ;
                }
                temp1->next=xin_a;
                xin_a->next =NULL;
        }
       
       
        if(*b==NULL)
        {
                *b=xin_b;
                xin_b->next =NULL;
        }
        else
        {
                temp2=*b;
                *b=xin_b;
                xin_b->next =temp2;
        }
}

int determine(struct List *a,struct List *b)
{
        while(a!=NULL&&b!=NULL)
        {
                if(a->i !=b->i)
                {
                        return 0;
                        break;
                }
                a = a->next;
                b = b->next;
        }
        if(a!=NULL || b!=NULL)
            return 0;
        return 1;
}

void print(struct List *a)
{
        struct List *cur;
        cur=a;
       
        while(cur!=NULL)
        {
                printf("%d ",cur->i );
                cur=cur->next ;
        }
}

int main(void)
{
        struct List *a=NULL;
        struct List *temp1=NULL;
        struct List *b=NULL;
        struct List *temp2=NULL;
       
        int i;
        while(1)
        {
                printf("请输入一个整数(输入-1表示结束):");
                scanf("%d",&i);
                if(i==-1)
                {
                        break;
                }
                else
                {
                        input(&a,&b,i);
                }
        }
       
        printf("你输入的单链表是: ");
        print(a);
        putchar('\n');
        if(determine(a,b))
        {
                printf("录入的单链表是回文数列!\n");
        }
        else
        {
                printf("录入的单链表不是回文数列!\n");
        }
        while(a!=NULL)
        {
                temp1=a->next ;
                free(a);
                a=temp1;
        }
        while(b!=NULL)
        {
                temp2=b->next ;
                free(b);
                b=temp2;
        }
        return 0;
}

注意我做了以下修改:

1. 将 List1 和 List2 合并为一个 List 结构体,因为他们是相同的。
2. 在 input 函数中,我使用 struct List a 和 struct List b 代替 struct List *a 和 struct List *b ,这样我们就可以在函数内部修改这些指针的值。
3. 在 main 函数中,我使用 &a 和 &b 调用 input 函数,以传递指针的地址。
4. 在 determine 函数中,我添加了递增的步骤并且增加了对 a 和 b 是否同时到达结尾的检查,这是为了防止两个链表长度不一样但是都可以是回文的情况。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-13 15:56:03 | 显示全部楼层    本楼为最佳答案   
你的问题出现在你传递指针到函数的方式上。在C语言中,所有的函数参数都是按值传递的。所以当你在函数中修改一个指针,你其实只是修改了这个指针的副本,而不是原来的指针。因此,你的 input 函数中的 a 和 b 的更改不会影响到 main 函数中的 a 和 b 。

你应该将指针的地址传递给函数,而不是指针本身。你可以通过将指针的指针传递给函数来实现这一点。这就是使用双指针的情况。

以下是修改后的代码:
#include<stdio.h>
#include<stdlib.h>

struct List
{
        int i;
        struct List *next;
};

void input(struct List a,struct List b,int i)
{
        struct List *xin_a;
        struct List *temp1;
        struct List *xin_b;
        struct List *temp2;
       
        xin_a=(struct List *)malloc(sizeof(struct List));
        xin_b=(struct List *)malloc(sizeof(struct List));
       
        if(xin_a==NULL&&xin_b==NULL)
        {
                printf("内存分配失败:\n");
                exit(1);
        }
        xin_a->i =i;
        xin_b->i =i;       
       
        if(*a==NULL)
        {
                *a=xin_a;
                xin_a->next =NULL;
        }
        else
        {
                temp1=*a;
                while(temp1->next !=NULL)
                {
                        temp1=temp1->next ;
                }
                temp1->next=xin_a;
                xin_a->next =NULL;
        }
       
       
        if(*b==NULL)
        {
                *b=xin_b;
                xin_b->next =NULL;
        }
        else
        {
                temp2=*b;
                *b=xin_b;
                xin_b->next =temp2;
        }
}

int determine(struct List *a,struct List *b)
{
        while(a!=NULL&&b!=NULL)
        {
                if(a->i !=b->i)
                {
                        return 0;
                        break;
                }
                a = a->next;
                b = b->next;
        }
        if(a!=NULL || b!=NULL)
            return 0;
        return 1;
}

void print(struct List *a)
{
        struct List *cur;
        cur=a;
       
        while(cur!=NULL)
        {
                printf("%d ",cur->i );
                cur=cur->next ;
        }
}

int main(void)
{
        struct List *a=NULL;
        struct List *temp1=NULL;
        struct List *b=NULL;
        struct List *temp2=NULL;
       
        int i;
        while(1)
        {
                printf("请输入一个整数(输入-1表示结束):");
                scanf("%d",&i);
                if(i==-1)
                {
                        break;
                }
                else
                {
                        input(&a,&b,i);
                }
        }
       
        printf("你输入的单链表是: ");
        print(a);
        putchar('\n');
        if(determine(a,b))
        {
                printf("录入的单链表是回文数列!\n");
        }
        else
        {
                printf("录入的单链表不是回文数列!\n");
        }
        while(a!=NULL)
        {
                temp1=a->next ;
                free(a);
                a=temp1;
        }
        while(b!=NULL)
        {
                temp2=b->next ;
                free(b);
                b=temp2;
        }
        return 0;
}

注意我做了以下修改:

1. 将 List1 和 List2 合并为一个 List 结构体,因为他们是相同的。
2. 在 input 函数中,我使用 struct List a 和 struct List b 代替 struct List *a 和 struct List *b ,这样我们就可以在函数内部修改这些指针的值。
3. 在 main 函数中,我使用 &a 和 &b 调用 input 函数,以传递指针的地址。
4. 在 determine 函数中,我添加了递增的步骤并且增加了对 a 和 b 是否同时到达结尾的检查,这是为了防止两个链表长度不一样但是都可以是回文的情况。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 01:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表