zoukankan      html  css  js  c++  java
  • 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;
    }  
     
    }
  • 相关阅读:
    HttpContext.GetOwinContext().Authentication 报错 解决办法
    owin Claims-based认证登录实现
    angularjs初识ng-app、ng-model、ng-repeat指令
    SpringBoot配置slf4j logback-spring.xml日志
    idea时间注释模版
    oracel截取字符串
    win10官网下载地址
    使用HttpWebRequest实现basic身份认证
    mybatis常用jdbcType数据类型与mysql的类型对照
    修改IntelliJ IDEA 默认配置路径
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/4781652.html
Copyright © 2011-2022 走看看