循环vs While循环在C中的链接列表上迭代的分段错误

在查看了有关链表的信息之后(仍然试图让我围绕这个话题)。我重写了以下代码,这是最终版本:(该代码旨在提示目录和通配符,以使用该文件扩展名创建在该目录中找到的文件的链表。

#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循环,在该循环中,我可以打印出我链接时的Nodes数据。 使用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,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循环相同来对链表进行迭代?

simonwong000 回答:循环vs While循环在C中的链接列表上迭代的分段错误

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

本文链接:https://www.f2er.com/3143737.html

大家都在问