题目如下
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *head, *tail;
void input();
int main()
{
struct stud_node *p;
head = tail = NULL;
input();
for ( p = head; p != NULL; p = p->next )
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}
/* 你的代码将被嵌在这里 */
代码如下
void input()
{
//为链表分配一个头结点head,并初始化,将next指针设为NULL,此时,链表为空
head = (struct stud_node *)malloc(sizeof(struct stud_node));
head->next = NULL;
tail = head;//设置一个尾节点指向头结点(当前尾部)
int number;//输入学号
scanf("%d",&number);
while(number!=0)
{
//为新学生分配内存并设置next指针为NULL,为链表中的新数据创建跳过原本的空头结点
//每次读取一个学生信息,都会创建一个新结点
struct stud_node *tmp = (struct stud_node *)malloc(sizeof(struct stud_node));
tmp->next = NULL;
tmp->num = number;
scanf("%s",tmp->name);
scanf("%d",&tmp->score);
tail->next = tmp;//将新结点添加到链表的末尾
tail = tmp;//确保其始终指向最后一个结点
scanf("%d",&number);
}
head = head->next;
}
今日刷题小结:
对于一个还没有学数据结构的人来说,力扣上的链表题着实有些难了,转战场地,开始刷pta上的链表题,加油!!!