
c语言改错题,求大神指点!!! 5
下列给定程序中,函数Creat的功能是:创建一个学生链表,并返回链表头指针,Put函数输出链表结点。#include<stdio.h>#include<stdlib.h>...
下列给定程序中, 函数Creat的功能是:创建一个学生链表,并返回链表头指针,Put函数输出链表结点。
#include <stdio.h>
#include <stdlib.h>
#define LENGTH sizeof(struct stulink)
struct stulink
{
long num; //学号
char name[20]; //姓名
int score; //成绩
struct stulink *next; //指向结构体的指针变量
};
int main()
{
struct stulink *Creat(struct stulink *);
void Put(struct stulink *);
struct stulink *head=NULL;
head=Creat(head);
Put(head);
return 0;
}
struct stulink *Creat(struct stulink *head)
{
int i,n;
struct stulink *p,*pr;
printf("input the number of student's ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(struct stulink *)malloc(LENGTH);
if(p==NULL)
{
printf("No enough memory to allocate!\n");
exit(0);
}
if(head==NULL)
head=p;
else
pr->next=p;
printf("input the N0 %d student num name score:\n",i);
scanf("%ld%s%d",&p->num,&p->name,&p->score);
p->next=NULL;
pr=p;
}
return(head);
}
void Put(struct stulink *head)
{
struct stulink *p=head;
printf("num name score\n");
while(p!=NULL)
{
printf("%ld\t%-10s %-3d\n",p->num,p->name,p->score);
p=p->next;
}
} 展开
#include <stdio.h>
#include <stdlib.h>
#define LENGTH sizeof(struct stulink)
struct stulink
{
long num; //学号
char name[20]; //姓名
int score; //成绩
struct stulink *next; //指向结构体的指针变量
};
int main()
{
struct stulink *Creat(struct stulink *);
void Put(struct stulink *);
struct stulink *head=NULL;
head=Creat(head);
Put(head);
return 0;
}
struct stulink *Creat(struct stulink *head)
{
int i,n;
struct stulink *p,*pr;
printf("input the number of student's ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(struct stulink *)malloc(LENGTH);
if(p==NULL)
{
printf("No enough memory to allocate!\n");
exit(0);
}
if(head==NULL)
head=p;
else
pr->next=p;
printf("input the N0 %d student num name score:\n",i);
scanf("%ld%s%d",&p->num,&p->name,&p->score);
p->next=NULL;
pr=p;
}
return(head);
}
void Put(struct stulink *head)
{
struct stulink *p=head;
printf("num name score\n");
while(p!=NULL)
{
printf("%ld\t%-10s %-3d\n",p->num,p->name,p->score);
p=p->next;
}
} 展开
2个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
#define LENGTH sizeof(struct stulink)
struct stulink {
long num; //学号
char name[20]; //姓名
int score; //成绩
struct stulink *next; //指向结构体的指针变量
};
struct stulink *Creat() {
int T,n;
struct stulink *p,*head;
p = head = (struct stulink *)malloc(LENGTH);
printf("input the number of student's :");
scanf("%d",&n);
T = n;
while(T--) {
p->next = (struct stulink *)malloc(LENGTH);
if(p->next == NULL) {
printf("No enough memory to allocate!\n");
exit(0);
}
printf("输入学号 姓名 成绩 : ");
scanf("%ld%s%d",&p->next->num,p->next->name,&p->next->score);
p = p->next;
}
p->next = NULL;
return head;
}
void Put(struct stulink *head) {
struct stulink *p = head->next;
printf("num\tname\tscore\n");
while(p) {
printf("%ld\t%-10s %-3d\n",p->num,p->name,p->score);
p = p->next;
}
}
int main() {
struct stulink *head = Creat();
Put(head);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |