链表:C:不存储我要查找的值

链表:C:不存储我要查找的值,c,scope,linked-list,singly-linked-list,C,Scope,Linked List,Singly Linked List,我有以下代码: #include <dirent.h> #include <stdio.h> #include <string.h> typedef struct stringData { char *s; struct stringData *next; } Node; Node *createNode(char *s) { Node *newNode = (Node *)malloc(sizeof(Node)); new

我有以下代码:

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

typedef struct stringData {
    char *s;
    struct stringData *next;
} Node;

Node *createNode(char *s) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->s = s;
    newNode->next = NULL;
    return newNode;
}


void insert(Node **link, Node *newNode) {
    newNode->next = *link;
    *link = newNode;
}

void printList(Node *head) {
    while (head != NULL) {
        printf("%s\n", head->s);
        head = head->next;
    }
}


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


int main()
{
    // Directory path to list files
    char path[100];
    char suffix[100];

    // Suffix Band Sentinel-2 of Type B02_10m.tif

    // Input path from user
    printf("Enter path to list files: ");
    scanf("%s", path);
    printf("Enter the bands ending: ");
    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));
}

/**
 * Lists all files and sub-directories recursively 
 * considering path as base path.
 */


void listFilesRecursively(char *basePath, char *suffix)
{
    char path[1000];
    struct dirent *dp;
    DIR *dir = opendir(basePath);
    //node_s *head, *first, *temp=0;
    //head = malloc(sizeof(node_s));
    Node *head = NULL;
    Node *tail = NULL;
    Node *n;


    // Unable to open directory stream
    if (!dir)
        return;

    while ((dp = readdir(dir)) != NULL)
    {


        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {
            //printf("%s\n", dp->d_name);

            // Construct new path from our base path
            strcpy(path, basePath);
            strcat(path, "/");
            strcat(path, dp->d_name);

            if (string_ends_with(path, suffix))
            {
                n = createNode(path);
                insert(&head, n);
                tail = n;   
                printf("%s\n", path);
            }
            listFilesRecursively(path, suffix);
        }
    }

    //printList(head);

    closedir(dir);
}
#包括
#包括
#包括
类型定义结构stringData{
char*s;
struct stringData*下一步;
}节点;
节点*创建节点(字符*s){
Node*newNode=(Node*)malloc(sizeof(Node));
newNode->s=s;
newNode->next=NULL;
返回newNode;
}
无效插入(节点**链接,节点*新节点){
新建节点->下一步=*链接;
*link=newNode;
}
无效打印列表(节点*头){
while(head!=NULL){
printf(“%s\n”,head->s);
头部=头部->下一步;
}
}
递归地作废列表文件(char*路径,char*后缀);
int main()
{
//列出文件的目录路径
字符路径[100];
字符后缀[100];
//B02_10m.tif型后缀带Sentinel-2
//来自用户的输入路径
printf(“输入列表文件的路径:”);
scanf(“%s”,路径);
printf(“输入以“.”结尾的标注栏);
scanf(“%s”,后缀);
递归地列出文件(路径、后缀);
返回0;
}
int字符串以(常量字符*字符串,常量字符*后缀)结尾
{
int str_len=strlen(str);
int后缀_len=strlen(后缀);
返回
(str_len>=后缀_len)&&
(0==strcmp(str+(str_len-后缀_len),后缀));
}
/**
*递归列出所有文件和子目录
*将路径视为基本路径。
*/
递归作废列表文件(字符*基本路径,字符*后缀)
{
字符路径[1000];
结构方向*dp;
DIR*DIR=opendir(基本路径);
//节点头,*第一,*温度=0;
//头=malloc(节点的大小);
Node*head=NULL;
Node*tail=NULL;
节点*n;
//无法打开目录流
如果(!dir)
返回;
而((dp=readdir(dir))!=NULL)
{
如果(strcmp(dp->d_name,“.”)=0和&strcmp(dp->d_name,“…”)!=0)
{
//printf(“%s\n”,dp->d_名称);
//从基本路径构造新路径
strcpy(路径、基本路径);
strcat(路径“/”;
strcat(路径,dp->d_名称);
if(字符串以(路径,后缀)结尾)
{
n=创建节点(路径);
插入(&head,n);
尾=n;
printf(“%s\n”,路径);
}
递归地列出文件(路径、后缀);
}
}
//印刷品清单(标题);
closedir(dir);
}
其目的是将递归搜索的值存储在目录中的链表中。我为指向链表下一个元素的字符串数据的节点创建了结构。我还添加了一些函数来插入新数据并指向下一个数据。最后,我可以通过调用printLIst函数打印出链表的值。但是,当我停用调用printList函数的第109行时,第103行中打印的值是正确的。如果我对第103行进行注释,并使用存储在链接列表中的值调用printLIst函数,则会出现一个文件列表,其中的值与第103行中打印的值完全不同

是C语言中的一种黑魔法吗?或者为什么会有这种奇怪的行为

在这个声明中

n = createNode(path); 
您正在每个节点中存储指向同一本地数组
路径的指针。请参见函数
createNode
的定义

Node *createNode(char *s) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->s = s;
    newNode->next = NULL;
    return newNode;
}
因此,至少由于这个错误,程序具有未定义的行为


您应该复制传递的字符串,而不仅仅是将指向它的指针分配给数据成员
s

newNode->s=s不复制
s
!如果您可以使用POSIX功能,这将非常有用: