C语言中链表任意位置怎么插入数据?然后写入文件中?
2024-08-28 13:54:59
链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便。
插入某个学号后面图:
定义的结构体:
struct student { ]; //学生学号 ]; //学生姓名 struct student *next; //next 指针 指向 struct student 类型的变量 }stu;
插入到某个学号后面,但不能插入到第一个节点的前面!
/*************** 函数功能: 插入学生 返回:指向链表表头的指针 /***************/ void insert_message(struct student* head) { FILE* fp; //定义文件指针 struct student* pointer,*q,*temp; // p指针指向新节点 q指向插入节点的地方 fp=fopen("student.txt","wb+"); pointer=head->next;//跳过头结点 指向下一个节点 InputBox(stu.ID,,"请输入要插入哪个学号后面"); while(pointer!=NULL) { ) //假设要插入到1后面,输入1 { fwrite(pointer,,fp); //先把1节点写入文件 q = (struct student *)malloc(sizeof(struct student)); //开辟新节点内存 InputBox(stu.ID,,"请输入学生学号"); strcpy(q->ID,stu.ID); InputBox(stu.name,,"请输入学生姓名"); strcpy(q->name,stu.name); temp= pointer->next; //将原来的 1后面的数据 2 赋值给临时temp结构体变量 pointer->next = q; //将q节点 赋值给 原来2的位置 pointer=pointer->next; //将q节点数据(pointer->next 等于q) 赋值给p 好让p节点写入文件 fwrite(pointer,,fp);//写入输入的q节点数据 pointer->next=temp; //将原来2位置的数据赋值到 p的下个节点(由于上个代码p=p->next)p被赋值p->next pointer=pointer->next; //p总是指向新的节点 while(pointer!=NULL) {fwrite(pointer,,fp); //将其他各节点遍历写入文件 pointer=pointer->next; } fclose(fp); outtext("插入学生成功!"); } fwrite(pointer,,fp); //事先开始遍历节点写入文件 pointer=pointer->next; } }
任意位置插入 图:
代码这么一改,任意位置的插入:
/*************** 函数功能: 插入出勤学生 返回:指向链表表头的指针 /***************/ void insert_message(struct student* head) { FILE* fp; //定义文件指针 struct student* pointer,*q,*temp; // p指针指向新节点 q指向插入节点的地方 fp=fopen("student.txt","wb+"); pointer=head->next;//跳过头结点 指向下一个节点 InputBox(stu.ID,,"请输入要插入哪个学号位置?"); while(pointer!=NULL) { ) //输入要插入到哪个位置? { q = (struct student *)malloc(sizeof(struct student)); //开辟新节点内存 InputBox(stu.ID,,"请输入学生学号"); strcpy(q->ID,stu.ID); InputBox(stu.name,,"请输入学生姓名"); strcpy(q->name,stu.name); temp=pointer; //将原来的 数据 赋值给临时temp结构体变量 pointer = q;//将q节点 赋值给 原来2的位置 fwrite(pointer,,fp);//写入输入的q节点数据 pointer->next=temp; //将原来2位置的数据赋值到 p的下个节点 pointer=pointer->next; //p总是指向新的节点 while(pointer!=NULL) {fwrite(pointer,,fp); //将其他各节点遍历写入文件 pointer=pointer->next; } fclose(fp); outtext("插入学生成功!"); } fwrite(pointer,,fp); //事先开始遍历节点写入文件 pointer=pointer->next; } }
最新文章
- 容器配置jndi Tomcat为例
- cpu缓存与多线程
- splObjectStroge的作用,实例化一个数组
- [Tommas] dateadd() 函数用法
- 16道嵌入式C语言面试题
- C++虚函数(09)
- 手把手教你用Jenkins自动发布dotnet core程序
- Golang:List
- 使用js主函数的原因是等文档加载完了才给里面的元素添加东西 如果不使用主函数则文档加载时候无法找到元素则不能成功给元素添加事件
- L2-005. 集合相似度(STL)*
- 【GMT43液晶显示模块】发布原理图、出厂代码
- English trip V1 - 24. Accommodations Teacher:Maple Key: make suggestions 提出建议
- C#实现windows服务安装,服务名可配置时出问题(无法创建 ProjectInstaller 安装程序类型的实例)
- 题解 P1345 【[USACO5.4]奶牛的电信Telecowmunication】
- 移动端meta声明
- groovy的三个强劲属性(一)Gpath
- bzoj4501 旅行
- 由于没有发现潜在的递归导致MySQL链接数溢出:MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: ";Too many connec
- Github:在Github上创建自己的代码仓库
- log4j教程 3、架构
热门文章
- windows不能显示此连接属性。windows management instrumentation (WMI) 信息可能损坏
- [Cordova] 无法显示Alert视窗
- [ASP.NET MVC] 使用CLK.AspNet.Identity提供依权限显示选单项目的功能
- Snabbt.js – 极简的 JavaScript 动画库
- iOS常用第三方库
- 通过GP加载卫星云图-雷达图-降雨预报图
- Source Insight基本使用和快捷键
- dispatch
- pod install 和 pod update的区别
- Android 下拉列表框、文本框、菜单