m0_69473531 2022-09-21 17:40 采纳率: 81.8%
浏览 1074
已结题

C语言,建立由这三组数据结点组成的简单链表

提交与自己学号相邻的两位同学的学号与一门考试成绩,编程建立由这三组数据结点组成的简单链表
样例输入
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);
}
}

  • 写回答

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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 赵4老师 2022-09-21 17:58
    关注

    数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633

    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月22日
  • 已采纳回答 9月22日
  • 修改了问题 9月21日
  • 创建了问题 9月21日

悬赏问题

  • ¥15 Erasure Code纠删码表
  • ¥15 用vite创建的vue3项目,404重定向不起作用??
  • ¥15 关于#c语言#的问题:一个球从80米高度自由落下,每次落地后反弹的高度为原高度的一半计算6次小球反弹的高度.(反弹结果取整,使用走走for循环结构)
  • ¥15 SurfaceControl的screenshot问题
  • ¥15 基于51单片机的oled菜单代码,要C语言,模块化编程!
  • ¥15 JAVAswing,设计一个扑克牌什么的
  • ¥50 python ctypes调用dll实现分析
  • ¥40 用python解决数据统计问题
  • ¥100 是否有方案能通过抓包分析得到移动应用的名称和包名信息?
  • ¥15 opencv检测不到轮廓