标准IO课题

1:编写一条学生链表,写一些能够像链表里边添加数据的函数 实现:将链表中的所有内容保存到文件中去 以及 读取文件中的所有内容,加载到链表里面

#include<stdio.h>
#include<stdlib.h>
typedef struct seq 
{
        union{
                int len;
        int score;
        };
        struct seq *next;

}seq,*seq_p;
seq_p create_seq_list()
{
        seq_p H=(seq_p)malloc(sizeof(seq));
        if(H==NULL)
        {
                printf("NULL");
                        return NULL;
        }
        H->next=NULL;
        H->len=0;
return H;
}
seq_p creat_list(int score)
{
        seq_p new=(seq_p)malloc(sizeof(seq));
        if(new==NULL)
        {
                printf("NULL");
                return NULL ;
        }
        new->score=score;
        return new;
}
void insert_head(seq_p H,int score)
{
        if(H==NULL)
        {
                printf("NULL");
                        return ;
        }
    seq_p new = creat_list(score);
        new->next=H->next;
        H->next=new;
        H->len++;
return;
}

void show_link(seq_p H)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    //输出
    //先保留第一个结点
    seq_p p = H->next;
    while(p!=NULL)
    {
        printf("%d->",p->score);
        p = p->next;
    }
    printf("NULL");
}
int main(int argc, char *argv[])
{
        seq_p H=(seq_p)malloc(sizeof(seq));
/*      insert_head(H,19);
        insert_head(H,18);
        insert_head(H,17);
        insert_head(H,16);
        insert_head(H,15);
        insert_head(H,14);
        insert_head(H,13);
        //show_link(H);
FILE *fp=fopen("test.txt","w");
if (fp==NULL)
{
        printf("文件打开失败");
                return 1;
}
//将链表的输出写入文件里面;
         seq_p p = H->next;
 while(p!=NULL)
 {
     fprintf(fp,"%d\n",p->score);
     p = p->next;
         
 }

fclose(fp);
*/

//将文件里面的数据输出到链表里面
FILE*rfp=fopen("test.txt","r");
if(rfp==NULL)
{
        printf("文件打开失败");
        return -1;
}

int a=0;
while(1)
{
    int res=fscanf(rfp,"%d",&a);
    
    if(res!=1)
    {break;}
    insert_head(H,a);

}
show_link(H);
fclose(rfp);
      return 0;
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/weixin_51648475/article/details/137124175

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值