鱼C论坛

 找回密码
 立即注册
查看: 1162|回复: 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 。

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

以下是修改后的代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct List
  4. {
  5.         int i;
  6.         struct List *next;
  7. };

  8. void input(struct List a,struct List b,int i)
  9. {
  10.         struct List *xin_a;
  11.         struct List *temp1;
  12.         struct List *xin_b;
  13.         struct List *temp2;
  14.       
  15.         xin_a=(struct List *)malloc(sizeof(struct List));
  16.         xin_b=(struct List *)malloc(sizeof(struct List));
  17.       
  18.         if(xin_a==NULL&&xin_b==NULL)
  19.         {
  20.                 printf("内存分配失败:\n");
  21.                 exit(1);
  22.         }
  23.         xin_a->i =i;
  24.         xin_b->i =i;      
  25.       
  26.         if(*a==NULL)
  27.         {
  28.                 *a=xin_a;
  29.                 xin_a->next =NULL;
  30.         }
  31.         else
  32.         {
  33.                 temp1=*a;
  34.                 while(temp1->next !=NULL)
  35.                 {
  36.                         temp1=temp1->next ;
  37.                 }
  38.                 temp1->next=xin_a;
  39.                 xin_a->next =NULL;
  40.         }
  41.       
  42.       
  43.         if(*b==NULL)
  44.         {
  45.                 *b=xin_b;
  46.                 xin_b->next =NULL;
  47.         }
  48.         else
  49.         {
  50.                 temp2=*b;
  51.                 *b=xin_b;
  52.                 xin_b->next =temp2;
  53.         }
  54. }

  55. int determine(struct List *a,struct List *b)
  56. {
  57.         while(a!=NULL&&b!=NULL)
  58.         {
  59.                 if(a->i !=b->i)
  60.                 {
  61.                         return 0;
  62.                         break;
  63.                 }
  64.                 a = a->next;
  65.                 b = b->next;
  66.         }
  67.         if(a!=NULL || b!=NULL)
  68.             return 0;
  69.         return 1;
  70. }

  71. void print(struct List *a)
  72. {
  73.         struct List *cur;
  74.         cur=a;
  75.       
  76.         while(cur!=NULL)
  77.         {
  78.                 printf("%d ",cur->i );
  79.                 cur=cur->next ;
  80.         }
  81. }

  82. int main(void)
  83. {
  84.         struct List *a=NULL;
  85.         struct List *temp1=NULL;
  86.         struct List *b=NULL;
  87.         struct List *temp2=NULL;
  88.       
  89.         int i;
  90.         while(1)
  91.         {
  92.                 printf("请输入一个整数(输入-1表示结束):");
  93.                 scanf("%d",&i);
  94.                 if(i==-1)
  95.                 {
  96.                         break;
  97.                 }
  98.                 else
  99.                 {
  100.                         input(&a,&b,i);
  101.                 }
  102.         }
  103.       
  104.         printf("你输入的单链表是: ");
  105.         print(a);
  106.         putchar('\n');
  107.         if(determine(a,b))
  108.         {
  109.                 printf("录入的单链表是回文数列!\n");
  110.         }
  111.         else
  112.         {
  113.                 printf("录入的单链表不是回文数列!\n");
  114.         }
  115.         while(a!=NULL)
  116.         {
  117.                 temp1=a->next ;
  118.                 free(a);
  119.                 a=temp1;
  120.         }
  121.         while(b!=NULL)
  122.         {
  123.                 temp2=b->next ;
  124.                 free(b);
  125.                 b=temp2;
  126.         }
  127.         return 0;
  128. }
复制代码


注意我做了以下修改:

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 是否同时到达结尾的检查,这是为了防止两个链表长度不一样但是都可以是回文的情况。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

以下是修改后的代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct List
  4. {
  5.         int i;
  6.         struct List *next;
  7. };

  8. void input(struct List a,struct List b,int i)
  9. {
  10.         struct List *xin_a;
  11.         struct List *temp1;
  12.         struct List *xin_b;
  13.         struct List *temp2;
  14.       
  15.         xin_a=(struct List *)malloc(sizeof(struct List));
  16.         xin_b=(struct List *)malloc(sizeof(struct List));
  17.       
  18.         if(xin_a==NULL&&xin_b==NULL)
  19.         {
  20.                 printf("内存分配失败:\n");
  21.                 exit(1);
  22.         }
  23.         xin_a->i =i;
  24.         xin_b->i =i;      
  25.       
  26.         if(*a==NULL)
  27.         {
  28.                 *a=xin_a;
  29.                 xin_a->next =NULL;
  30.         }
  31.         else
  32.         {
  33.                 temp1=*a;
  34.                 while(temp1->next !=NULL)
  35.                 {
  36.                         temp1=temp1->next ;
  37.                 }
  38.                 temp1->next=xin_a;
  39.                 xin_a->next =NULL;
  40.         }
  41.       
  42.       
  43.         if(*b==NULL)
  44.         {
  45.                 *b=xin_b;
  46.                 xin_b->next =NULL;
  47.         }
  48.         else
  49.         {
  50.                 temp2=*b;
  51.                 *b=xin_b;
  52.                 xin_b->next =temp2;
  53.         }
  54. }

  55. int determine(struct List *a,struct List *b)
  56. {
  57.         while(a!=NULL&&b!=NULL)
  58.         {
  59.                 if(a->i !=b->i)
  60.                 {
  61.                         return 0;
  62.                         break;
  63.                 }
  64.                 a = a->next;
  65.                 b = b->next;
  66.         }
  67.         if(a!=NULL || b!=NULL)
  68.             return 0;
  69.         return 1;
  70. }

  71. void print(struct List *a)
  72. {
  73.         struct List *cur;
  74.         cur=a;
  75.       
  76.         while(cur!=NULL)
  77.         {
  78.                 printf("%d ",cur->i );
  79.                 cur=cur->next ;
  80.         }
  81. }

  82. int main(void)
  83. {
  84.         struct List *a=NULL;
  85.         struct List *temp1=NULL;
  86.         struct List *b=NULL;
  87.         struct List *temp2=NULL;
  88.       
  89.         int i;
  90.         while(1)
  91.         {
  92.                 printf("请输入一个整数(输入-1表示结束):");
  93.                 scanf("%d",&i);
  94.                 if(i==-1)
  95.                 {
  96.                         break;
  97.                 }
  98.                 else
  99.                 {
  100.                         input(&a,&b,i);
  101.                 }
  102.         }
  103.       
  104.         printf("你输入的单链表是: ");
  105.         print(a);
  106.         putchar('\n');
  107.         if(determine(a,b))
  108.         {
  109.                 printf("录入的单链表是回文数列!\n");
  110.         }
  111.         else
  112.         {
  113.                 printf("录入的单链表不是回文数列!\n");
  114.         }
  115.         while(a!=NULL)
  116.         {
  117.                 temp1=a->next ;
  118.                 free(a);
  119.                 a=temp1;
  120.         }
  121.         while(b!=NULL)
  122.         {
  123.                 temp2=b->next ;
  124.                 free(b);
  125.                 b=temp2;
  126.         }
  127.         return 0;
  128. }
复制代码


注意我做了以下修改:

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 是否同时到达结尾的检查,这是为了防止两个链表长度不一样但是都可以是回文的情况。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 13:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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