1.27 保存和加载链表内容

发布于:2025-02-10 ⋅ 阅读:(108) ⋅ 点赞:(0)

请使用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;
}

运行效果:

网站公告

今日签到

点亮在社区的每一天
去签到