调试:链表链接问题

亚历克斯

我有一个链表实现如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CAPACITY 128

typedef struct blob blob;
struct blob {
    char text[CAPACITY];
    struct blob* prev;
    struct blob* next;
};

static blob* create_blob() {
    blob* newBlob = malloc(sizeof(blob));
    if (!newBlob) {
        fprintf(stderr, "%s\n", "Malloc failed");
        return 0;
    }
    newBlob->prev = 0;
    newBlob->next = 0;
    memset(newBlob->text, 0, CAPACITY);
    return newBlob;
}


int insertAtTail(blob* list[static 1]) {
    blob* newBlob = create_blob();
    if (!newBlob) {
        fprintf(stderr, "%s\n", "Malloc failed");
        return 0;
    }
    if (!*list) {
        *list = newBlob;
        return 1;
    }
    blob* lastElement = *list;
    while (lastElement->next) {
        // Check if linkage until last element is okay
        if (lastElement != (lastElement->next)->prev) {
            fprintf(stderr, "%s\n", "The next_element->prev does not point to the current element");
        } else {
            fprintf(stderr, "%s\n", "Okay");
        }
        lastElement = lastElement->next; // Find last Blob
    }
    lastElement->next = newBlob;
    newBlob->prev = lastElement;
    return 1;
}

int reversePrintBlobs(blob const list[static 1]) {
    if (!list) {
        fprintf(stderr, "%s\n", "list is empty");
        return 1;
    }
    const blob* tmp = list;
    size_t blobCounter = 1;
    while (tmp->next) {
        blobCounter++;
        tmp = tmp->next;
    }
    while (tmp && tmp != list) {
        if ((tmp->prev)->next != tmp) {
            fprintf(stderr, "%s\n", "error with the linkage");
            exit(1);
        }
        printf("#number = %zu: %s\n", blobCounter--, tmp->text);
        tmp = tmp->prev;
    }
    return 0;
}

当我使用以下 main() 函数添加一些 blob 时,结构prev成员似乎没有指向前一个元素。即联动有问题。谁能指出为什么?

int main() {
    FILE* pFile = fopen("example.txt", "r");
    if (!pFile) {
        perror("fopen failed\n");
        return EXIT_FAILURE ;
    }
    fseek(pFile, 0, SEEK_SET);
    blob* list = create_blob();
    blob* tmp = list;
    while (!feof(pFile)) {
        size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
                                   pFile);
        if (noBytesRead != CAPACITY && !feof(pFile)) {
            fprintf(stderr, "%s\n", "File read failed");
            exit(EXIT_FAILURE);
        }
        // Append a null terminating character in the end
        tmp->text[noBytesRead] = '\0';
        if (!feof(pFile) && insertAtTail(&list)) {
            tmp = tmp->next;
        }
    }
}

另一方面,如果我使用以下 main() 一切正常。

int main() {
    blob* list = create_blob();
    insertAtTail(&list);
    insertAtTail(&list);
    insertAtTail(&list);
    insertAtTail(&list);
    insertAtTail(&list);
    /* code */
    return 0;
}
亚历克斯

该错误已修复,新的主要功能如下:


int main() {
    FILE* pFile = fopen("example.txt", "r");
    if (!pFile) {
        perror("fopen failed\n");
        return EXIT_FAILURE ;
    }
    fseek(pFile, 0, SEEK_SET);
    blob* list = create_blob();
    blob* tmp = list;
    int blobCounter = 1;
    while (!feof(pFile)) {
        size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
                                   pFile);
        if (noBytesRead != CAPACITY && !feof(pFile)) {
            fprintf(stderr, "%s\n", "File read failed");
            exit(EXIT_FAILURE);
        }
        // Append a null terminating character in the end
        tmp->text[noBytesRead - 1] = '\0';
        if (!feof(pFile) && insertAtTail(&list)) {
            blobCounter++;
            tmp = tmp->next;
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章