鱼C论坛

 找回密码
 立即注册
查看: 4241|回复: 2

这个那里错了??

[复制链接]
发表于 2012-8-19 15:07:00 | 显示全部楼层 |阅读模式

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

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

x
#include "stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define LEN sizeof(struct stud)
struct stud *creat();
struct stud
{
        long a;
        struct stud *next;
};
int n;
void main()
{
        struct stud *qq;
        qq=creat();
        do
        {
                printf("%c\n",qq->a);
                qq=qq->next;
        }while(qq!=0);
       

}
struct stud *creat()
{
        struct stud *head;struct stud *p1,*p2;
    p1=p2=(struct stud *)malloc(LEN);
        scanf("%c",&p1->a);
        n=0;head=0;
        while(p1->a!=0)
        {
                n++;
                if(n==1) head=p1;
                else p2->next=p1;
                p2=p1;
                p1=(struct stud *)malloc(LEN);
                scanf("%c",&p1->a);
        }
                p2->next=0;
                printf("%d\n",n);
                return head;

}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-20 16:16:26 | 显示全部楼层
  1. #include "stdio.h"  //标准库的头文件最好用<>
  2. #include"malloc.h"
  3. #include"stdlib.h"
  4. #define LEN sizeof(struct stud)
  5. struct stud *creat();
  6. struct stud
  7. {
  8.         long a;//定义的是long型变量
  9.         struct stud *next;
  10. };
  11. int n;
  12. void main()
  13. {
  14.         struct stud *qq;
  15.         qq=creat();
  16.         do
  17.         {
  18.                 printf("%ld\n",qq->a);//改为ld%
  19.                 qq=qq->next;
  20.         }while(qq!=0);
  21.        
  22.        
  23. }
  24. struct stud *creat()
  25. {
  26.         struct stud *head;struct stud *p1,*p2;
  27.         p1=p2=(struct stud *)malloc(LEN);
  28.         scanf("%ld",&p1->a);//改为ld%
  29.         n=0;head=p1;//这里先把p1给head,下面的循环不执行的话返回是空指针,会出错
  30.         while(p1->a!=0)
  31.         {
  32.                 n++;
  33.                 if(n==1) head=p1;
  34.                 else p2->next=p1;
  35.                 p2=p1;
  36.                 p1=(struct stud *)malloc(LEN);
  37.                 scanf("%ld",&p1->a);//改为ld%
  38.         }
  39.         p2->next=0;
  40.         printf("%d\n",n);
  41.         return head;
  42.        
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-20 17:05:22 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-14 17:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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