迷宫5 2022-06-09 20:43 采纳率: 0%
浏览 24

真的不会这个,有谁能解一下吗

文件操作+线性链表+冒泡排序实验
现有结构体定义如下:
struct stuInfo
{
char id[12]; //数据成员
char name[15];
float score;

struct stuInfo *next;   //指针成员

};
任务及要求:
(1)首先完成线性链表的定义和创建,确保链表的第一个节点为哨兵节点,不存放有效的数据;
(2)利用文件的相关操作,动态开辟节点存储空间,将存放在磁盘某个路径中的文件“stuinfo.txt”信息逐条读入,并依次存入到线性链表的每个节点中,信息如下;
00001 张三 100
00002 李四 99
00003 王五 100
00004 王六 89
00005 江涛 98
00006 陈琪 88
00007 祁廷 91
(3)接下来,将学号为‘00004’的节点删除;
(4)再接下来,利用冒泡排序,对链表继续升序排序;
(5)将完整的线性链表信息从头到尾依次存储到磁盘某个路径下的“score.txt”文件中,存储格式和“stuinfo.txt”文件相同。文件操作+线性链表+冒泡排序实验
现有结构体定义如下:
struct stuInfo
{
char id[12]; //数据成员
char name[15];
float score;

struct stuInfo *next;   //指针成员

};
任务及要求:
(1)首先完成线性链表的定义和创建,确保链表的第一个节点为哨兵节点,不存放有效的数据;
(2)利用文件的相关操作,动态开辟节点存储空间,将存放在磁盘某个路径中的文件“stuinfo.txt”信息逐条读入,并依次存入到线性链表的每个节点中,信息如下;
00001 张三 100
00002 李四 99
00003 王五 100
00004 王六 89
00005 江涛 98
00006 陈琪 88
00007 祁廷 91
(3)接下来,将学号为‘00004’的节点删除;
(4)再接下来,利用冒泡排序,对链表继续升序排序;
(5)将完整的线性链表信息从头到尾依次存储到磁盘某个路径下的“score.txt”文件中,存储格式和“stuinfo.txt”文件相同。

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-06-10 12:27
    关注

    供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct stuInfo{
        char id[12]; //数据成员
        char name[15];
        float score;
        struct stuInfo* next;//指针成员
    };
    void createList(struct stuInfo** L)//创建链表
    {
        (*L) = (struct stuInfo*)malloc(sizeof(struct stuInfo));
        (*L)->next = NULL;
    }
    
    void readfile(struct stuInfo *L) //读文件到链表
    {
        FILE* fp;
        struct stuInfo* pL = L, * pnew = NULL;
        fp = fopen("stuinfo.txt", "r");
        if (fp == NULL) {
            printf("文件打开失败!\n");
            return;
        }
        while (pL->next) pL = pL->next;
        while (1) {
            pnew = (struct stuInfo*)malloc(sizeof(struct stuInfo));
            pnew->next = NULL;
            if (fscanf(fp, "%12s%15s%f", pnew->id, pnew->name, &pnew->score) != 3) {
                free(pnew);
                break;
            }
            else {
                pL->next = pnew;
                pL = pnew;
            }
        }
        fclose(fp);
    }
    
    void deletestuid(struct stuInfo* L) //删除学号为xxxxx的结点
    {
        int flg = 0;
        char id[12] = { 0 };
        struct stuInfo* pL = L, * pT = NULL;
        printf("请输入要删除的学号:");
        scanf("%s", id);
        while (pL->next) {
            if (strcmp(pL->next->id, id) == 0) {
                pT = pL->next;
                pL->next = pT->next;
                free(pT);
                flg = 1;
            }
            else
                pL = pL->next;
        }
        if (!flg)
            printf("未找到学号:%s 的记录。\n", id);
        else
            printf("删除成功!\n");
    }
    
    void bbsortList(struct stuInfo* L)//按成绩冒泡排序函数
    {
        struct stuInfo* pL, * tail = NULL, * pNext;
        while ((L->next->next) != tail) {
            pL = L;
            pNext = L->next;
            while (pNext->next != tail) 
            {
                if (pNext->score > pNext->next->score) 
                {
                    pL->next = pNext->next;
                    pNext->next = pNext->next->next;
                    pL->next->next = pNext;
                    pNext = pL->next;
                }
                pNext = pNext->next;
                pL = pL->next;
            }
            tail = pNext;
        }
    }
    void writefile(struct stuInfo* L) //写文件,将链表内容保存到文件
    {
        FILE* fp;
        struct stuInfo* pL = L;
        fp = fopen("score.txt", "w");
        if (fp == NULL) {
            printf("文件打开失败!\n");
            return;
        }
        while (pL->next) {
            fprintf(fp, "%s %s %f\n", pL->next->id, pL->next->name, pL->next->score);
            pL = pL->next;
        }
        fclose(fp);
    }
    
    void printList(struct stuInfo* L)
    {
        struct stuInfo* p = L;
        while (p->next) {
            printf("%s %s %7.2f\n", p->next->id,p->next->name, p->next->score);
            p = p->next;
        }
    }
    int main()
    {
        struct stuInfo* L = NULL;//(1)线性链表的定义
        createList(&L); //(1) 线性表创建
        readfile(L);    //(2) 将存放在磁盘的文件“stuinfo.txt”信息逐条读入存入到线性链表中
        deletestuid(L); //(3) 将学号为‘xxxxx’的节点删除 
        bbsortList(L);  //(4) 冒泡排序,对链表升序排序
        writefile(L);   //(5) 将线性链表信息存储到磁盘“score.txt”文件中
        printList(L);
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 6月9日

悬赏问题

  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口