rr_effort 2021-07-07 12:03 采纳率: 100%
浏览 58
已采纳

修正代码,实现需求,给我正确代码,晚上九点之前给回复

任务及要求:
(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”文件相同。
问题:
我写的代码运行时找不到节点信息,删除不了,后面的代码也没法运行,保存到文件全是乱码。
代码:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(stuInfo)
typedef struct stuInfo
{
char id[12]; //数据成员 char name[15]; float score; struct stuInfo *next;}stuInfo, *stu;
stu headStu;
void save(stu headStu);
stuInfo *create(stuInfo *headStu);void Lsort(stu headStu);stuInfo *create(stuInfo *headStu){ int N=7; headStu=(stuInfo *)malloc(LEN); stuInfo *p,*q; p=headStu; for(int i=0;i<N;i++) { q=(stuInfo *)malloc(LEN); p->next=q; p->next; q->next=NULL; p=q; } printf("创建结束!\n"); return headStu;}void ListStu(stu headStu){ int num; stuInfo *p; p=headStu->next; while(p!=NULL) { printf("%d:%s %s %f\n",++num,p->id,p->name,p->score); p=p->next; } printf("打印完毕\n");}stu Bubble_Sort(stu headStu){ stu p1,p2; p1=headStu->next; p2=headStu; while(p1!=NULL) { while(p1->next!=NULL) { if(p1->scorescore) { float temp=p1->score; p1->score=p1->next->score; p1->next->score=temp; } p1=p1->next; } p1=p2->next; p2=p1; }}stu Delete_stuInfo(stu headStu,char delId[12]){ stu lp,p; lp=headStu; p=lp->next; if(headStu==NULL) { printf("链表为空\n"); return; } while(strcmp(p->id,delId)!=0&&(p->next!=NULL)) { lp=p; p=p->next; } if((strcmp(p->id,delId)==0)) { lp->next=p->next; free(p); } else printf("没有找到该学生的信息!\n"); return headStu;}void Lsort(stu headStu){ stu head; int x=0; float t; stu end,p,next; if(head==NULL) return; for(p=headStu;p->next!=NULL;p=p->next) end=p; while(end!=headStu) { for(p=headStu;p!=end;p=p->next) { if(p->next->score>p->next->score) { t=p->score;p->score=p->next->score;p->next->score=t; } next=p; } end=next; }}void save(stu headStu){ int N=7; FILE *fp; stu p=headStu->next; if((fp=fopen("score.txt","w+"))==NULL) { printf("文件打开失败!\n"); exit(-1); } else { printf("打开save成功\n"); } for(int i=0;i<N;i++) { fprintf(fp,"%s %s %f",p->id,p->name,p->score); p=p->next; } printf("文件保存成功!\n"); fclose(fp);}void load(){ char id[12],name[15]; float score; int N=7; FILE *fp; fp=fopen("stuinfo.txt","rb+"); if(fp==NULL) { printf("文件打开失败!"); exit(-1); } else { printf("读取成功:\n"); } stuInfo *h,*p,*q; h=(stuInfo *)malloc(LEN); p=h; printf(" 学号 姓名 分数\n"); while(N>0) { q=(stuInfo *)malloc(LEN); fscanf(fp,"%s %s %f",&q->id,&q->name,&q->score); printf("%s %s %f\n",q->id,q->name,q->score); p->next=q; if(q->next=NULL) p=q; N--; } printf("读取结束!\n"); fclose(fp);}int main(){ stu head; char id[12]; head=create(head); load(); printf("请输入要删除学生的学号:\n"); scanf("%s",id); head=Delete_stuInfo(head,id); Lsort(head); save(head); ListStu(head); return 0;}

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-07-07 12:46
    关注

    https://blog.csdn.net/technologist_37/article/details/118500579
    一样的功能已经写好了,把main中注释掉的冒泡排序恢复出来,下面那个排序注释掉

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动