当前位置: 首页 > article >正文

1.27 保存和加载链表内容

请使用read 和 write 实现链表保存到文件,以及从文件加载数据到链表中的功能。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

// 定义链表节点结构
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 创建新节点
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        perror("Memory allocation failed");
        exit(EXIT_FAILURE);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 插入节点到链表尾部
void insertNode(Node** head, int data) {
    Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}

// 打印链表
void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// 将链表保存到文件
void saveListToFile(Node* head, const char* filename) {
    int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd == -1) {
        perror("Failed to open file for writing");
        return;
    }
    Node* temp = head;
    while (temp != NULL) {
        if (write(fd, &(temp->data), sizeof(int)) != sizeof(int)) {
            perror("Write failed");
            close(fd);
            return;
        }
        temp = temp->next;
    }
    close(fd);
}

// 从文件加载数据到链表
Node* loadListFromFile(const char* filename) {
    int fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("Failed to open file for reading");
        return NULL;
    }
    Node* head = NULL;
    int data;
    while (read(fd, &data, sizeof(int)) == sizeof(int)) {
        insertNode(&head, data);
    }
    close(fd);
    return head;
}

// 释放链表内存
void freeList(Node* head) {
    Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}
int main(int argc, const char *argv[])
{
    // 创建链表
    Node* head = NULL;
    insertNode(&head, 1);
    insertNode(&head, 2);
    insertNode(&head, 3);

    // 打印原始链表
    printf("原始链表内容: ");
    printList(head);

    // 保存链表到文件
    const char* filename = "list_data.txt";
    saveListToFile(head, filename);

    // 释放原始链表内存
    freeList(head);

    // 从文件加载数据到新链表
    head = loadListFromFile(filename);

    // 打印加载后的链表
    printf("加载后的链表内容: ");
    printList(head);

    // 释放加载后的链表内存
    freeList(head);

	return 0;
}

运行效果:

http://www.kler.cn/a/520466.html

相关文章:

  • 笔试-二维数组2
  • 深入探讨数据库索引类型:B-tree、Hash、GIN与GiST的对比与应用
  • 在AlarmLinux系统中安装KeyDB
  • 01绪论 + 递归+分治+搜索+回溯+原码反码补码+进制+位运算+位图(D2_刷题练习)
  • JVM深入学习(二)
  • Effective C++ 规则50:了解 new 和 delete 的合理替换时机
  • convnext 网络结构简介
  • 想品客老师的第六天:函数
  • 论文阅读(四):混合贝叶斯和混合回归方法推断基因网络的比较
  • Docker快速部署高效照片管理系统LibrePhotos搭建私有云相册
  • HarmonyOS:创建应用静态快捷方式
  • k8s支持自定义field-selector spec.hostNetwork过滤
  • [MoeCTF 2022]ezhtml
  • 2014年蓝桥杯第五届CC++大学B组真题及代码
  • 【Postman接口测试】接口用例设计实战—以聚合数据的新闻头条接口为例
  • pytorch卷积的入门操作
  • 深入理解Pytest中的Setup和Teardown
  • LLM大模型推理中的常见数字
  • Windows上通过Git Bash激活Anaconda
  • 【算法】图解面试笔试热点二叉树相关算法题汇总