0

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

#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 循环,我可以在其中打印出使用 insert(Head, path) 函数时链接的节点数据。但是,当我使用 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 循环相同?

4

1 回答 1

1
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 失败也是一个好习惯。

于 2019-11-07T15:48:57.220 回答