鱼C论坛

 找回密码
 立即注册
查看: 1321|回复: 4

[已解决]请问是什么地方错了导致无法停止输出????

[复制链接]
发表于 2020-5-3 13:41:00 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<malloc.h>
typedef int datatype;
typedef struct node
{
        datatype data;
        struct node*next;
}listnode, *linklist;
linklist kong();
void headadd(linklist L);
int main()
{
        linklist L=kong();
        headadd(L);
        return 0;
}
linklist kong()
{
        linklist head = (linklist)malloc(sizeof(listnode));
        if (head == NULL)
        {
                printf("error\n");
                exit(-1);
        }
        else
        {
                head->next = NULL;
                return head;
        }
}
void headadd(linklist L)
{
        linklist s=(linklist)malloc(sizeof(listnode));
        int x;
        char ch;
        if (s == NULL)
        {
                printf("error");
                exit(-1);
        }
        else
        {
                 do
                        {
                                printf("输入数据:\n");
                                        scanf("%d", &x);
                                        s->data = x;
                                        s->next = L->next;
                                        L->next = s;
                                        printf("是否继续添加(y|n):\n");
                                        scanf(" %c", &ch);
                        }while (ch == 'y' || 'Y');
                        return;
               
        }
       
       
}
void printflist(linklist L)//将数据输出;
{
        linklist l = L->next;
        if (L == NULL)
        {
                printf("error");
                exit(-1);
        }
        else
        {
                while (L->next != NULL)
                {
                        printf("%d", l->data);
                }

        }
}
最佳答案
2020-5-4 13:21:51
while (ch == 'y' || 'Y');
这里改成while(ch=='y'||ch=='Y');
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-3 13:53:46 | 显示全部楼层
ch

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-4 00:44:41 | 显示全部楼层
while (L->next != NULL)
                {
                        printf("%d", l->data);
                        L = L->next;  //少这一句
                }
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-4 13:21:51 | 显示全部楼层    本楼为最佳答案   
while (ch == 'y' || 'Y');
这里改成while(ch=='y'||ch=='Y');
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-4 18:53:31 | 显示全部楼层
根据你的想法我写了一个类似的:
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef int datatype;
  4. typedef struct node
  5. {
  6.         datatype data;
  7.         struct node* next;
  8. }listnode,*linklist;
  9. void headadd(linklist *L);
  10. void printflist(linklist L);
  11. int main()
  12. {
  13.         int ch='y';
  14.         linklist L=NULL;
  15.         while(1)
  16.         {
  17.                 printf("是否录入信息:");
  18.                 do
  19.                 {
  20.                         ch=getchar();
  21.                 }while(ch!='y'&&ch!='n');
  22.                 if(ch=='y')
  23.                         headadd(&L);

  24.                 else
  25.                         break;
  26.         }
  27.         printflist(L);
  28.         return 0;
  29. }


  30. void headadd(linklist *L)
  31. {
  32.         linklist s=(linklist)malloc(sizeof(listnode));
  33.         linklist temp;
  34.         if(s==NULL)
  35.         {
  36.                 printf("申请空间失败!\n");
  37.                 exit(1);
  38.         }
  39.         printf("输入数据:\n");
  40.         scanf("%d",&s->data);
  41.         if(*L!=NULL)
  42.         {
  43.                 temp=*L;
  44.                 *L=s;
  45.                 s->next=temp;
  46.         }
  47.         else
  48.         {
  49.                 *L=s;
  50.                 s->next=NULL;
  51.         }
  52. }

  53. void printflist(linklist L)
  54. {
  55.         linklist s;
  56.         s=L;
  57.         while(s!=NULL)
  58.         {
  59.                 printf("%d\t",s->data);
  60.                 s=s->next;
  61.         }
  62.         putchar('\n');
  63. }
复制代码


你的void headadd(linklist L)中只申请了一个动态内存,将数据循环放进去是不合理的,然后在这个函数传值时,要修改的时L这个指针的值(也就是下一个节点的地址),这个时候应该传入L的地址改为void headadd(linklist *L),对应的主函数中写为headadd(&L)。还有while (ch == 'y' || 'Y');
这里应改成while(ch=='y'||ch=='Y');在void printflist(linklist L)函数中改为linklist l = L;以及上面大佬说的
while (L->next != NULL)
                {
                        printf("%d", l->data);
                        L = L->next;  //少这一句
                }

说的不对的地方,请多多指正
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 21:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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