| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 471 人关注过本帖
标题:[求助]一个小程序的错误,问错在哪里
只看楼主 加入收藏
CHEN5354520
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-4-18
收藏
 问题点数:0 回复次数:2 
[求助]一个小程序的错误,问错在哪里

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define NULL 0

struct student{
char name[20];
int number;
int math;
struct student * next;
};


struct student * creat()
{
struct student *head,*p,*q;
int n;
printf("输入一个1继续输入n=");
scanf("%d",&n);
printf("\nname:\tnumber:\t\tmath:\t\t\n");

while(!n){
p=(struct student*)malloc (sizeof(struct student));
head=p;
q=p;
p=p->next;
q->next=NULL;
scanf("%s",&p->name);
scanf("%d",&p->number);
scanf("%d",&p->math);

}
return(head);
}

void print(struct student *)
{
struct student *head,*p;
head=p;
while(!p->next)
{
printf("name:\tnumber:\tmath\t\n",p->name,p->number,p->math);
p=p->next;
}
}

void main()
{
struct student *head;
head=creat();
print(head);
}

2007-05-17 09:05
chenxinfu
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-4-14
收藏
得分:0 

#include "stdio.h"
#define NULL 0

struct student{
char name[20];
int number;
int math;
struct student * next;
};


struct student * creat()
{
struct student *head,*p,*q;
int n;
head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
q=head;
printf("输入一个1继续输入n=");
scanf("%d",&n);
if(!n)exit(0);

while(n){
printf("\nname:\tnumber:\t\tmath:\t\t\n");
p=(struct student*)malloc (sizeof(struct student));
scanf("%s",p->name);
scanf("%d",&p->number);
scanf("%d",&p->math);
q->next=p;
q=p;
printf("输入一个1继续输入n=");
scanf("%d",&n);


}
q->next=NULL;
return(head);
}

void print(struct student *head)
{
struct student *p;
p=head->next;
while(p)
{
printf("name:%s number:%d math:%d\n",p->name,p->number,p->math);
p=p->next;
}
}

void main()
{
struct student *head;
head=creat();
print(head);
getch();
}





我也是刚学这个不久的。还是先看看版主贴的程序段吧
不过这个我也不知道怎么名字老是打印不出来的。还请版主指教执教




[此贴子已经被作者于2007-5-17 19:04:58编辑过]


2007-05-17 12:19
chenxinfu
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-4-14
收藏
得分:0 
以下是引用CHEN5354520在2007-5-17 9:05:21的发言:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define NULL 0

struct student{
char name[20];
int number;
int math;
struct student * next;
};


struct student * creat()
{
struct student *head,*p,*q;
int n;
printf("输入一个1继续输入n=");
scanf("%d",&n);
printf("\nname:\tnumber:\t\tmath:\t\t\n");

while(!n){ //这个你上面输入后判断n=1是根本执行不了,n=0时执行没有结束标志
p=(struct student*)malloc (sizeof(struct student));
head=p;
q=p;
p=p->next;
q->next=NULL;
scanf("%s",&p->name);//取地址后又取地址
scanf("%d",&p->number);
scanf("%d",&p->math);

}
return(head);
}

void print(struct student *)
{
struct student *head,*p;
head=p;
while(!p->next)//这部分也有错误,不过我也不知道怎么和你解释啊。呵呵

{
printf("name:\tnumber:\tmath\t\n",p->name,p->number,p->math);//打印地址
p=p->next;
}
}

void main()
{
struct student *head;
head=creat();
print(head);
}


2007-05-17 12:25
快速回复:[求助]一个小程序的错误,问错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011070 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved