鱼C论坛

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

[已解决]循环出了点问题 不能循环 找了好久没发现哪里有问题求大佬帮忙

[复制链接]
发表于 2021-3-14 18:51:43 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
typedef struct node
{
        DataType data;
        struct node *next;
}ListNode,*LinkList;
LinkList InitList ();
void HeadAddList(LinkList L);
void PrintList(LinkList L);
int main()
{
        LinkList L=InitList();
        HeadAddList(L);
        PrintList(L);
        printf("软件2005 李广森\n学号:20200220117");
        return 0;
}
LinkList InitList ()
{
        LinkList head=(LinkList)malloc(sizeof(ListNode));
        if(head==NULL)
        {
                printf("空间申请失败!\n");
                exit(-1);
        }
        head->next=NULL;
        return head;
}
void HeadAddList(LinkList L)
{
        DataType x;
        char ch;
        do
        {
                LinkList p=(LinkList)malloc(sizeof(ListNode));
                if(p==NULL)
                {
                        printf("空间申请失败!\n");
                        return;
                }
                printf("请输入数据的值\n");
                scanf("%d",&x);
                p->data=x;
                p->next=L->next;
                L->next=p;
                printf("是否要继续输入数据元素的值(Y/N)");
                scanf("%c",&ch);
        }while(ch=='Y'||ch=='y');
        return;
}
void PrintList(LinkList L)
{
        LinkList p=L->next;
        int i=0;
        while(p)
        {
                printf("(%d)%d\t",++i,p->data);
                p=p->next;       
        }
        printf("\n");
        return;
}
最佳答案
2021-3-14 19:56:37
在scanf("%d", &x);后加一个getchar();来缓冲输入的回车符
  1. void HeadAddList(LinkList L)
  2. {
  3.         DataType x;
  4.         char ch;
  5.         do
  6.         {
  7.                 LinkList p=(LinkList)malloc(sizeof(ListNode));
  8.                 if(p==NULL)
  9.                 {
  10.                         printf("空间申请失败!\n");
  11.                         return;
  12.                 }
  13.                 printf("请输入数据的值\n");
  14.                 scanf("%d",&x);
  15.                 getchar();
  16.                 p->data=x;
  17.                 p->next=L->next;
  18.                 L->next=p;
  19.                 printf("是否要继续输入数据元素的值(Y/N)");
  20.                 scanf("%c",&ch);
  21.                
  22.         }while(ch=='Y'||ch=='y');
  23.         return;
  24. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-14 19:56:37 | 显示全部楼层    本楼为最佳答案   
在scanf("%d", &x);后加一个getchar();来缓冲输入的回车符
  1. void HeadAddList(LinkList L)
  2. {
  3.         DataType x;
  4.         char ch;
  5.         do
  6.         {
  7.                 LinkList p=(LinkList)malloc(sizeof(ListNode));
  8.                 if(p==NULL)
  9.                 {
  10.                         printf("空间申请失败!\n");
  11.                         return;
  12.                 }
  13.                 printf("请输入数据的值\n");
  14.                 scanf("%d",&x);
  15.                 getchar();
  16.                 p->data=x;
  17.                 p->next=L->next;
  18.                 L->next=p;
  19.                 printf("是否要继续输入数据元素的值(Y/N)");
  20.                 scanf("%c",&ch);
  21.                
  22.         }while(ch=='Y'||ch=='y');
  23.         return;
  24. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 03:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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