|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 guhusf 于 2021-1-15 16:45 编辑
麻烦各位鱼油帮我看看 这个程序哪里出问题了,谢谢 (输入的时候num不能输入)
程序如下:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student) //结构的大小
struct student *creat(); //创造链表
void printf(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量
main()
{
struct student *stu;
stu=creat();
printf(stu);
printf("\n");
}
struct student *creat()
{
struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(LEN);//LEN是student结构的大小
printf("please enter the num:");
scanf("%d",p1->num);
printf("plese enter the score:");
scanf("%.1f",p1->score);
head=NULL;
n=0;
while(p1->num)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("please enter the num:");
scanf("%d",p1->num);
printf("\n");
printf("plese enter the score:");
scanf("%.1f",p1->score);
}
p2->next=NULL;
return head;
}
void printf(struct student *head)
{
struct student *p;
printf("There are %d records\n",n);
p=head;
if(head)
{
do
{
printf("学号为%d的成绩是:%.1f\n",p->num,p->score);
p=p->next;
}while(p);
}
}
本帖最后由 jackz007 于 2021-1-15 17:37 编辑
- #include<stdio.h>
- #include<stdlib.h>
- struct student
- {
- int num ;
- float score ;
- struct student * next ;
- } ;
- int n ;
- struct student * creat(void)
- {
- struct student * head , * p1 , * p2 ;
- int num ;
- float score ;
- for(n = 0 , head = NULL ;;) {
- printf("please enter the num : ") ;
- scanf("%d" , & num) ;
- if(num > 0) {
- printf("plese enter the score : ") ;
- scanf("%f" , & score) ;
- if((p1 = (struct student *) malloc(sizeof(struct student)))) {
- p1 -> num = num ;
- p1 -> score = score ;
- p1 -> next = NULL ;
- if(! head) head = p1 ;
- else p2 -> next = p1 ;
- p2 = p1 ;
- n ++ ;
- } else {
- for(p1 = head ; p1 ; p1 = p2) {
- p2 = p1 -> next ;
- free(p1) ;
- }
- head = NULL ;
- fprintf(stderr , "\n") ;
- fprintf(stderr , "failed to allocate memory !\n") ;
- fprintf(stderr , "\n") ;
- break ;
- }
- } else {
- break ;
- }
- }
- return head ;
- }
- void print(student * head)
- {
- student * p ;
- printf("\n") ;
- printf("There are %d records\n" , n) ;
- for(p = head ; p ; p = p -> next) printf("学号为 %d 的成绩是 : %.1f\n" , p -> num , p -> score) ;
- printf("\n") ;
- }
- int main(void)
- {
- struct student * stu = NULL ;
- stu = creat() ;
- print(stu) ;
- printf("\n") ;
- }
复制代码
|
|