PTA 第8章结构体与其他结构类型作业 6-6 末尾添加新的结点 (20分)

本题要求实现,将输入的学生信息组成单向链表;并在末尾追加一学生信息。
函数接口定义:
struct stud_node *create();
struct stud_node *insertdata( struct stud_node *head, int n,char name[],int s );

函数create利用scanf从输入中获取一系列正整数,当读到0时表示输入结束。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct stud_node)

struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};

struct stud_node *create();
struct stud_node *insertdata( struct stud_node *head, int n,char name[],int s );

int main()
{
int num,score;
char name[20];
struct stud_node *p, *head = NULL;
head = create();
scanf("%d%s%d",&num,name,&score);
head=insertdata(head,num,name,score);
for ( p = head; p != NULL; p = p->next )
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:
10001 zhang 96
10002 wang 63
0
10003 liu 100

输出样例1:
10001 zhang 96
10002 wang 63
10003 liu 100

struct stud_node *create()
{
 struct stud_node *p2,*p1,*head;
 head=NULL;
 int n=0;
 p1=p2=(struct stud_node*)malloc(sizeof(struct stud_node));
 scanf("%d",&p1->num);
 while(p1->num!=0)
 {
  scanf("%s %d",p1->name,&p1->score);
  n=n+1;
  if(n==1)
  head=p1;
  else
  p2->next=p1;
  p2=p1;
  p1=(struct stud_node*)malloc(sizeof(struct stud_node));
  scanf("%d",&p1->num);   
 }
 p2->next=NULL;
 return(head);
}
struct stud_node *insertdata( struct stud_node *head, int n,char name[],int s )
{
 struct stud_node *p=head,*pr=head,*q=head;
 p=(struct stud_node*)malloc(sizeof(struct stud_node));
 p->next=NULL;
 p->num=n;
 strcpy(p->name,name);
 p->score=s;
 while(pr->num<n&&pr->next!=NULL)
 {
  q=pr;
  pr=pr->next;
 }
 pr->next=p;
 return(head);
}

完整代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct stud_node)
struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};
struct stud_node *create()
{
 struct stud_node *p2,*p1,*head;
 head=NULL;
 int n=0;
 p1=p2=(struct stud_node*)malloc(sizeof(struct stud_node));
 scanf("%d",&p1->num);
 while(p1->num!=0)
 {
  scanf("%s %d",p1->name,&p1->score);
  n=n+1;
  if(n==1)
  head=p1;
  else
  p2->next=p1;
  p2=p1;
  p1=(struct stud_node*)malloc(sizeof(struct stud_node));
  scanf("%d",&p1->num);   
 }
 p2->next=NULL;
 return(head);
}
struct stud_node *insertdata( struct stud_node *head, int n,char name[],int s )
{
 struct stud_node *p=head,*pr=head,*q=head;
 p=(struct stud_node*)malloc(sizeof(struct stud_node));
 p->next=NULL;
 p->num=n;
 strcpy(p->name,name);
 p->score=s;
 while(pr->num<n&&pr->next!=NULL)
 {
  q=pr;
  pr=pr->next;
 }
 pr->next=p;
 return(head);
}
int main()
{
    int  num,score;
    char name[20];
     struct stud_node *p, *head = NULL;
     head = create();
     scanf("%d%s%d",&num,name,&score);
      head=insertdata(head,num,name,score);
     for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值