提交与自己学号相邻的两位同学的学号与一门考试成绩,编程建立由这三组数据结点组成的简单链表
样例输入
201 98 202 94 203 89
样例输出
[num=201,score=98]
[num=202,score=94]
[num=203,score=89]
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int num;
int score;
struct Node *next;
}Node,*Linklist;
void initLinklist(Linklist &l)
{
l=(Node *)malloc(sizeof(Node));
l->next=NULL;
}
void add(Linklist &l,int i,int num,int score)
{
Node *p;
int j=0;
p=l;
for(;j<i-1;j++)
{
p=p->next;
}
Node *s;
s->num=num;
s->score=score;
s->next=p->next;
p->next=s;
printf("%d %d",s->num,s->score);
}
int main(){
Linklist L;
initLinklist(L);
int num,score;
int i;
for(i=1;i<=3;i++)
{
scanf("%d,%d",&num,&score);
add(L,i,num,score);
}
}
![](https://profile-avatar.csdnimg.cn/df1b49f930cc4db0be2111fc0c36bee5_m0_69473531.jpg!4)
C语言,建立由这三组数据结点组成的简单链表
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- _GX_ 2022-09-21 17:58关注
#include <stdio.h> #include <stdlib.h> typedef struct Node { int num; int score; struct Node *next; } Node, *Linklist; void initLinklist(Linklist &l) { l = (Node *)malloc(sizeof(Node)); l->next = NULL; } void add(Linklist &l, int i, int num, int score) { Node *p; int j = 0; p = l; for (; j < i - 1; j++) { p = p->next; } Node *s = (Node *)malloc(sizeof(Node)); s->num = num; s->score = score; s->next = p->next; p->next = s; } void printLinklist(Linklist &l) { Node *p = l->next; while (p) { printf("[num=%d,score=%d]\n", p->num, p->score); p = p->next; } } void destroyLinklist(Linklist &l) { Node *p = l; while (p) { Node *q = p; p = p->next; free(q); } l = NULL; } int main() { Linklist L; initLinklist(L); int num, score; int i; for (i = 1; i <= 3; i++) { scanf("%d%d", &num, &score); add(L, i, num, score); } printLinklist(L); destroyLinklist(L); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 8无用 8
悬赏问题
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵