• C语言中链表任意位置怎么插入数据?然后写入文件中?


    链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便。

    插入某个学号后面图:

     

    定义的结构体:

    struct student
    {  
    char ID[11]; //学生学号
    char name[20];  //学生姓名
     
    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,11,"请输入要插入哪个学号后面");
           
           while(pointer!=NULL)
           { 
               if(strcmp(pointer->ID,stu.ID)==0) //假设要插入到1后面,输入1
               {  
                   fwrite(pointer,sizeof(struct student),1,fp); //先把1节点写入文件
                          
                   q = (struct student *)malloc(sizeof(struct student)); //开辟新节点内存
                   
                   InputBox(stu.ID,11,"请输入学生学号");     
                   strcpy(q->ID,stu.ID);
                   
                   InputBox(stu.name,20,"请输入学生姓名");
                   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,sizeof(struct student),1,fp);//写入输入的q节点数据
                 
                   pointer->next=temp; //将原来2位置的数据赋值到 p的下个节点(由于上个代码p=p->next)p被赋值p->next
    
                   pointer=pointer->next; //p总是指向新的节点
                   
                   while(pointer!=NULL)
                   {fwrite(pointer,sizeof(struct student),1,fp);  //将其他各节点遍历写入文件
                   pointer=pointer->next;
                   }
                   fclose(fp);
                   outtext("插入学生成功!");
                   
               }
               fwrite(pointer,sizeof(struct student),1,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,11,"请输入要插入哪个学号位置?");
           
           while(pointer!=NULL)
           { 
               if(strcmp(pointer->ID,stu.ID)==0) //输入要插入到哪个位置?
               {  
                          
                   q = (struct student *)malloc(sizeof(struct student)); //开辟新节点内存
                   
                   InputBox(stu.ID,11,"请输入学生学号");     
                   strcpy(q->ID,stu.ID);
                   
                   InputBox(stu.name,20,"请输入学生姓名");
                   strcpy(q->name,stu.name);
    
                   temp=pointer; //将原来的  数据  赋值给临时temp结构体变量
                
                   pointer = q;//将q节点 赋值给 原来2的位置
    
                   fwrite(pointer,sizeof(struct student),1,fp);//写入输入的q节点数据
                 
                   pointer->next=temp; //将原来2位置的数据赋值到 p的下个节点
    
                   pointer=pointer->next; //p总是指向新的节点
                   
                   while(pointer!=NULL)
                   {fwrite(pointer,sizeof(struct student),1,fp);  //将其他各节点遍历写入文件
                   pointer=pointer->next;
                   }
                   fclose(fp);
                   outtext("插入学生成功!");
                  
               }
               fwrite(pointer,sizeof(struct student),1,fp);  //事先开始遍历节点写入文件
                pointer=pointer->next;
    }  
     
    }
  • 相关阅读:
    entity framework 查看自动生成的sql
    如何从只会 C++ 语法的水平到达完成项目编写软件的水平?
    C/C++程序员必须熟练应用的开源项目
    VS2013创建Windows服务
    VS2013中使用Git建立源代码管理
    PowerDesigner导出表到word
    SQLSERVER的逆向工程,将数据库导入到PowerDesigner中
    Asp.Net MVC+EF+三层架构的完整搭建过程
    QT开发(一)Vs2013集成 QT5.3.1
    VS2013 好用的插件
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/4781652.html
Copyright © 2020-2023  润新知