m0_62267751 2021-11-23 09:08 采纳率: 85.7%
浏览 32
已结题

急 C语言可能循环出现问题 本应该是输入数字 直到输入0就结束,但是最后会一直输出


#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct int_list)
struct int_list
{
    int value;
    struct int_list *next;
};
typedef struct int_list IntList;
int n;
struct int_list *creat()
{
    struct int_list *head;
    struct int_list *p1,*p2;
    n=0;
    p1=p2=(struct int_list *)malloc(LEN);
    printf("Input:");
    scanf("%d", &p1->value);
    head=NULL;
    while(p1->value!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next = p1;
        p2=p1;
        p1=(struct int_list*)malloc(LEN);
        printf("Input:");
        scanf("%d", &p1->value);
    }
    p2->next=NULL;
    return(head);
}

void print(struct int_list *head)
{
    struct int_list *p;
    printf("out");
    p=head;
    if(head!=NULL)
        do
        {
            printf("Output:");
            printf("%d",p->value);
        }while(p!=NULL);
}

int main ()
{
    struct int_list *head;
    head = creat();
    print(head);
    return 0;
}
  • 写回答

1条回答 默认 最新

  • bekote 2021-11-23 11:00
    关注

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 11月23日

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像