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日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!