c - Loop 与 While 循环在 C 中迭代链表时的段错误

标签 c linked-list

查看有关链接列表的信息后(仍在尝试围绕该主题进行思考)。我重写了以下代码,这是最终版本:(该代码旨在提示输入目录和通配符,以创建在该目录中找到的具有此文件扩展名的文件的链接列表。

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

typedef struct nlist{
    char *data;
    struct nlist *next;
}Node;


Node* insert(Node*, char*);


void show(Node*);


Node* insert(Node *Head, char *value)
{
    Node *new_string;
    new_string = (Node *)malloc(sizeof(Node));
    new_string->data = malloc(strlen(value)+1);
    strcpy(new_string->data,value);
    Node *check;
    check = (Node *)malloc(sizeof(Node));

    if(Head == NULL){
        Head = new_string;
        Head->next = NULL;
    }
    else{
        check = Head;
        while(check->next != NULL)
            check = check->next;

        check->next = new_string;
        new_string->next = NULL;
    }
    return Head;
}

void show(Node *Head)
{
    Node *check;
    check = (Node *)malloc(sizeof(Node));
    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s", check->data);
        check=check->next;
    }
    printf("\n");
}

void listFilesRecursively(char *path, char *suffix);


int main()
{
    char path[100];
    char suffix[100];

    // Input path from user
    // Suffix Band Sentinel-2 of Type B02_10m.tif

    printf("Enter path to list files: ");
    scanf("%s", path);
    printf("Enter the wildcard: ");
    scanf("%s", suffix);

    listFilesRecursively(path, suffix);

    return 0;
}


int string_ends_with(const char * str, const char * suffix)
{
    int str_len = strlen(str);
    int suffix_len = strlen(suffix);

    return 
        (str_len >= suffix_len) &&
        (0 == strcmp(str + (str_len-suffix_len), suffix));
}

void listFilesRecursively(char *basePath, char *suffix)
{
    char path[1000];
    struct dirent *dp;
    DIR *dir = opendir(basePath);
    Node *Head = NULL;

    if (!dir)
        return;

    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {
            strcpy(path, basePath);
            strcat(path, "/");
            strcat(path, dp->d_name);

            if (string_ends_with(path, suffix))
                Head = insert(Head, path);
            listFilesRecursively(path, suffix);
        }
    }
    //show(Head);
    /*
    Node *check;
    check = (Node *)malloc(sizeof(Node));
    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s", check->data);
        //open_array(check->data);
        check=check->next;
    }
    printf("\n");
    //return Head;
    */

    Node *p;
    p = (Node *)malloc(sizeof(Node));
    for (p = &Head; p != NULL; p = p->next){
        printf(stdout, "Data: %s\n", p->data);
    }

    closedir(dir);
}

我的问题: 我评论了 while 循环,在该循环中我能够打印出我链接的节点数据 使用插入(头,路径)函数。但是,当我使用 for 循环执行相同操作时

Node *p // to create a Node pointer
p = (Node*)malloc(sizeof(Node)); // to allocate space for that node
for (p = &Head; p!= NULL; p = p->next){
    printf(stdout, "Data: %s\n", p->data); // to print the nodes in the for loop starting by getting the address of the Head of the linked list
}

为什么会出现段错误?是否可以使用 for 循环对链表进行迭代,就像在 C 中使用 while 循环一样?

最佳答案

Node *p;

if (!(p = (Node*)malloc(sizeof(Node))))
    return;
for (p = Head; p != NULL; p = p->next){
    printf(stdout, "Data: %s\n", p->data);
}

无需引用您的主管地址,因为它已经是一个。

保护您的程序免受 malloc 失败的影响也是一个好习惯。

关于c - Loop 与 While 循环在 C 中迭代链表时的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58751639/

相关文章:

c - 链表中的所有节点都指向同一个对象

c - 我无法 typedef 指向已进行 typedef 编辑的结构的指针

exception-handling - 尝试删除第一个对象时,如何在 Smalltalk 中捕获空的 LinkedList 异常?

java - 如何将整数数组转换为数组列表和链表?

c - 如何在我的 C 程序中正确使用指针

C lang 基本问题 - 为什么我得到两个结构的相同地址?

创建一个进程并告诉它 sleep ?

c - 子进程是否也应该解锁被阻塞的 SIGCHLD 信号?

c - 如何修复字符串数组的条件错误

c - `Build Error` 错误 -1073741819 - C