编写链表,链表里面随便搞点数据使用 fprintf 将链表中所有的数据,保存到文件中使用 fscanf 读取文件中的数据,写入链表中

#include "fun.h"

int main(int argc, const char *argv[])
{
    list_p H=creat();
    write_data(H,1,1);
    write_data(H,2,2);
    write_data(H,5,3);
    write_data(H,6,4);    
    write_data(H,7,5);
    //printf_list(H);
    //保存到文件中
    
    list_p p=H;
    FILE *fp=fopen("1.txt","w");
    if(fp==NULL){return 1;}
    while(1)
    {
        int ret=fprintf(fp,"%d ",p->next->data);
        p=p->next;
        if(ret==-1||p->next==NULL)
        {
            break;
        }
    }
        fprintf(fp,"%d ",99);
    fclose(fp);
    
    //写入链表
    
    list_p q=H;
    FILE *fp2=fopen("1.txt","r");
    if(fp2==NULL){return 1;}
    while(1)
    {
        int rr=fscanf(fp2,"%d",&(q->next->data));
        q=q->next;
        if(rr==-1||q->next==NULL)
        {
            break;
        }
    }
    printf_list(H);
    
    return 0;
}
#include "fun.h"
list_p creat()
{
    list_p H =(list_p)malloc(sizeof(list));
    if(H==NULL)
    {
        return NULL;
    }
    H->len=0;
    H->next =NULL;
    return H;
}
list_p creat_node(int data)
{
    list_p new=(list_p)malloc(sizeof(list));
    new->data=data;
    return new;
}
void write_data(list_p H,int data,int pos)
{
    list_p new=creat_node(data);
    list_p p = H;
    for(int i=0;i<pos-1;i++)
    {
        p=p->next;
    }
    new->next=p->next;
    p->next=new;
    H->len++;
}
void printf_list(list_p H)
{
    list_p p=H->next;
    while(p!=NULL)
    {
        printf("%d\n",p->data);
        p=p->next;
    }
}
#ifndef __FUN_H__
#define __FUN_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct list
{
    union
    {
        int len;
        int data;
    };
    struct list *next;
}list,*list_p;


list_p creat();
list_p creat_node(int data);
void write_data(list_p H,int data,int pos);
void printf_list(list_p H);


#endif
 

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一些思路和代码示例。 首先,我们需要定义一个链表节点和图节点的结构体,它们的定义与前面提到的一样。 ```c typedef struct ListNode { int val; // 节点值 struct ListNode *next; // 指向下一个节点的指针 } ListNode; typedef struct GraphNode { int val; // 节点值 struct GraphNode *next; // 指向下一个邻接节点的指针 } GraphNode; ``` 然后我们可以定义一些函数来操作这个链表和图,比如创建节点、添加边等等。 ```c // 创建一个新链表节点 ListNode *new_node(int val) { ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->val = val; node->next = NULL; return node; } // 创建一个新图节点 GraphNode *new_graph_node(int val) { GraphNode *node = (GraphNode*)malloc(sizeof(GraphNode)); node->val = val; node->next = NULL; return node; } // 将链表转换成图 GraphNode **list_to_graph(ListNode **list, int node_count) { GraphNode **graph = (GraphNode**)malloc(node_count * sizeof(GraphNode*)); for (int i = 0; i < node_count; i++) { graph[i] = NULL; } ListNode *node = *list; while (node) { ListNode *cur = node; node = node->next; free(cur); GraphNode *graph_node = new_graph_node(cur->val); graph_node->next = graph[cur->val]; graph[cur->val] = graph_node; } *list = NULL; return graph; } // 将图转换成链表 ListNode **graph_to_list(GraphNode **graph, int node_count) { ListNode **list = (ListNode**)malloc(sizeof(ListNode*)); *list = NULL; for (int i = 0; i < node_count; i++) { GraphNode *node = graph[i]; while (node) { ListNode *list_node = new_node(node->val); list_node->next = *list; *list = list_node; node = node->next; } } return list; } // 从文件读取图的信息 GraphNode **read_graph(char *filename, int *node_count, int *edge_count) { FILE *fp = fopen(filename, "r"); if (!fp) { printf("Failed to open file %s\n", filename); return NULL; } fscanf(fp, "%d %d", node_count, edge_count); GraphNode **graph = (GraphNode**)malloc(*node_count * sizeof(GraphNode*)); for (int i = 0; i < *node_count; i++) { graph[i] = NULL; } for (int i = 0; i < *edge_count; i++) { int u, v; fscanf(fp, "%d %d", &u, &v); GraphNode *node = new_graph_node(v); node->next = graph[u]; graph[u] = node; node = new_graph_node(u); node->next = graph[v]; graph[v] = node; } fclose(fp); return graph; } // 保存图的信息文件 void save_graph(GraphNode **graph, int node_count, int edge_count, char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) { printf("Failed to open file %s\n", filename); return; } fprintf(fp, "%d %d\n", node_count, edge_count); for (int i = 0; i < node_count; i++) { GraphNode *node = graph[i]; while (node) { if (node->val > i) { fprintf(fp, "%d %d\n", i, node->val); } node = node->next; } } fclose(fp); } ``` 最后,我们可以在主函数调用这些函数来创建链表、将链表转换成图、保存文件、从文件读取图的信息并将图转换成链表。 ```c int main() { // 创建一个新链表 ListNode *list = NULL; for (int i = 0; i < 5; i++) { ListNode *node = new_node(i); node->next = list; list = node; } // 将链表转换成图 GraphNode **graph = list_to_graph(&list, 5); // 保存文件 save_graph(graph, 5, 4, "graph.txt"); // 从文件读取图的信息 int node_count, edge_count; GraphNode **new_graph = read_graph("graph.txt", &node_count, &edge_count); // 将图转换成链表 ListNode **new_list = graph_to_list(new_graph, node_count); // 打印链表的值 ListNode *node = *new_list; while (node) { printf("%d ", node->val); node = node->next; } printf("\n"); return 0; } ``` 这样,我们就可以用 C 语言编写链表数据存入无向图的信息文件读取文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值