m0_58131791 2021-12-21 22:41 采纳率: 66.7%
浏览 17
已结题

链表 c语言 数据结构问题

请问:这段代码该怎么解释呢?(完整的代码在最下面)


    while (pre && p)
    {
        if(p->id < t->id)
        {
            pre = p;
            p = p->next;
        }else
            break;
    }
    if(p)
    {
        pre->next = t;
        t->next = p;
    }else
        pre->next = t;
    return head;
}
--------------------------
--------------------------
完整如下:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
    int id;
    char name[20];
    int score;
    struct Student* next;
};
//创建链表,按学号排序插入
struct Student* createlist()
{
    int i,n;
    struct Student* head,*pre,*p,*t;
    head = (struct Student*)malloc(sizeof(struct Student));
    head->next = NULL;
    printf("请输入学生的个数:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        t = (struct Student*)malloc(sizeof(struct Student));
        t->next = NULL;
        printf("请输入学生%d的学号:",i+1);
        scanf("%d",&t->id);
        printf("请输入学生%d的姓名:",i+1);
        scanf("%s",t->name);
        printf("请输入学生%d的成绩:",i+1);
        scanf("%d",&t->score);
        pre = head;
        p = pre->next;
        while (pre && p)
        {
            if(p->id < t->id)
            {
                pre = p;
                p = p->next;
            }else
                break;
        }
        if(p)
        {
            pre->next = t;
            t->next = p;
        }else
            pre->next = t;
    }
    return head;
}
//遍历显示
void show(struct Student* head)
{
    struct Student* p;
    if(head == 0) 
    {
        printf("链表为空\n");
        return;
    }
    p=head->next;
    while(p)
    {
        printf("学号:%d 姓名:%s 成绩:%d\n",p->id,p->name,p->score);
        p = p->next;
    }
}
//插入
struct Student* insert(struct Student* head)
{
    struct Student *pre,*p,*t;
    t = (struct Student*)malloc(sizeof(struct Student));
    t->next = NULL;
    printf("请输入学生的学号:");
    scanf("%d",&t->id);
    printf("请输入学生的姓名:");
    scanf("%s",t->name);
    printf("请输入学生的成绩:");
    scanf("%d",&t->score);
    pre = head;
    p = pre->next;
    while (pre && p)
    {
        if(p->id < t->id)
        {
            pre = p;
            p = p->next;
        }else
            break;
    }
    if(p)
    {
        pre->next = t;
        t->next = p;
    }else
        pre->next = t;
    return head;
}
//修改
struct Student* mod(struct Student* head)
{
    struct Student* p = head->next;
    int id;
    printf("请输入要修改信息的学生学号:");
    scanf("%d",&id);
    while(p)
    {
        if(p->id == id)
            break;
        else
            p = p->next;
    }
    printf("请输入学生的姓名:");
    scanf("%s",p->name);
    printf("请输入学生的成绩:");
    scanf("%d",&p->score);
    return head;
}
//删除
struct Student* del(struct Student* head)
{
    struct Student* p,*pre;
    int id;
    pre = head;
    p = head->next;
    printf("请输入要修改信息的学生学号:");
    scanf("%d",&id);
    while(p)
    {
        if(p->id == id)
            break;
        else
        {
            pre = p;
            p = p->next;
        }
    }
    if(p)
    {
        pre->next = p->next;
        free(p); p=0;
        printf("删除成功\n");
    }else
        printf("未找到该学号的学生\n");
    return head;
}
//释放内存
void release(struct Student* head)
{
    struct Student *p;
    while(head)
    {
        p = head->next;
        free(head);
        head = p;
    }
}
int main()
{
    struct Student* head =NULL;
    head = createlist();
    printf("当前链表数据为:\n");
    show(head);
    printf("插入数据:");
    head = insert(head);
    printf("插入数据后:\n");
    show(head);
    printf("修改数据:");
    head = mod(head);
    printf("修改数据后:\n");
    show(head);
    printf("删除数据\n");
    head = del(head);
    printf("删除数据后:\n");
    show(head);
    //释放内存
    release(head);
    return 0;
}
 

  • 写回答

2条回答 默认 最新

  • fuill 2022-01-01 02:33
    关注

    写注解里了,供参考

    
        while (pre && p)//找出比t所指向的id小的id,把pre指针指向最小id的地址
        {
            if(p->id < t->id)
            {
                pre = p;
                p = p->next;
            }else
                break;
        }
        if(p)//pre指向原t指针的位置,插入pre指向的数据
        {
            pre->next = t;
            t->next = p;
        }else
            pre->next = t;
        return head;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 创建了问题 12月21日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格