C语言:创建链表,提交与自己学号相邻的两位同学的学号与一门考试成绩,编程建立由这三组数据结点组成的简单链表。输入形式:三组同学的学号后三位,成绩。输出形式:链表各结点 的数据。样例输入:201,98,202,94,203,89 样例输出 [num=201,score=98] [num=202,score=94]
时间: 2023-05-24 12:05:21 浏览: 268
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
int score;
struct node *next;
} NODE;
NODE *new_node(int num, int score) {
NODE *node = (NODE *) malloc(sizeof(NODE));
node->num = num;
node->score = score;
node->next = NULL;
return node;
}
NODE *add_node(NODE *head, int num, int score) {
NODE *node = new_node(num, score);
if (head == NULL) {
head = node;
} else {
NODE *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
return head;
}
void print_list(NODE *head) {
NODE *p = head;
while (p != NULL) {
printf("[num=%d, score=%d] ", p->num, p->score);
p = p->next;
}
}
int main() {
int num1, num2, num3, score1, score2, score3;
printf("请输入三组同学的学号后三位和成绩:\n");
scanf("%d%d%d%d%d%d", &num1, &score1, &num2, &score2, &num3, &score3);
NODE *head = NULL;
head = add_node(head, num1, score1);
head = add_node(head, num2, score2);
head = add_node(head, num3, score3);
printf("链表各节点的数据为:");
print_list(head);
printf("\n");
return 0;
}
```
阅读全文