如何使用函数返回两个不同的链表?

我正在以下代码中工作:

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>



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

// Function to replace a string with another 
// string 
char* str_replace(char* string,const char* substr,const char* replacement) {
    char* tok = NULL;
    char* newstr = NULL;
    char* oldstr = NULL;
    int   oldstr_len = 0;
    int   substr_len = 0;
    int   replacement_len = 0;

    newstr = strdup(string);
    substr_len = strlen(substr);
    replacement_len = strlen(replacement);

    if (substr == NULL || replacement == NULL) {
        return newstr;
    }

    while ((tok = strstr(newstr,substr))) {
        oldstr = newstr;
        oldstr_len = strlen(oldstr);
        newstr = (char*)malloc(sizeof(char) * (oldstr_len - substr_len + replacement_len + 1));

        if (newstr == NULL) {
            free(oldstr);
            return NULL;
        }

        memcpy(newstr,oldstr,tok - oldstr);
        memcpy(newstr + (tok - oldstr),replacement,replacement_len);
        memcpy(newstr + (tok - oldstr) + replacement_len,tok + substr_len,oldstr_len - substr_len - (tok - oldstr));
        memset(newstr + oldstr_len - substr_len + replacement_len,1);

        free(oldstr);
    }

    free(string);

    return newstr;
}


Node* insert(Node*,char*);
void show(Node*);

/*
int getcount(Node *Head) 
{ 
    int count = 0;  // Initialize count 
    Node *current;
    current = (Node *)malloc(sizeof(Node));
    current = Head;  // Initialize current 

    do {
        count++;
    }
    while (current != NULL) 
    {  
        current = current->next; 
    } 
    return count; 
} 
 */

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);

    struct Node *B02List;
    B02List = listFilesRecursively(path,suffix);
    char *suffix_scl = "SCL_10m.tif";
    struct Node *SCLList;
    SCLList = listFilesRecursively(path,suffix_scl);
    show(B02List);
    show(SCLList);

    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));
}

struct Node *listFilesRecursively(char *basePath,char *suffix)
{
    char path[1000];
    struct dirent *dp;
    DIR *dir = opendir(basePath);
    Node *Head = NULL;
    Node *Head_scl = 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);
    closedir(dir);
    return Head;
}

如您所见,我将struct Node放入**** listFilesRecursively ***函数中,因此我可以通过使用return Head返回类似的值(行列表)。 我在main()中两次调用此函数,以查找具有两个不同结尾的文件。如果我只想打印列表,那很好用,为此,我将此函数的类型声明设置为void。但是,当我尝试返回链接列表以便能够在main()函数中使用它们来打印它们时,我什至无法编译代码。

在一些警告中,我得到一个错误类型

error: conflicting types for ‘listFilesRecursively’
 struct Node *listFilesRecursively(char *basePath,char *suffix)

这是构建警告和错误的完整列表:

list_dir_files_4.c: In function ‘main’:
list_dir_files_4.c:138:15: warning: implicit declaration of function ‘listFilesRecursively’ [-Wimplicit-function-declaration]
     B02List = listFilesRecursively(path,suffix);
               ^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c:138:13: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     B02List = listFilesRecursively(path,suffix);
             ^
list_dir_files_4.c:141:13: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     SCLList = listFilesRecursively(path,suffix_scl);
             ^
list_dir_files_4.c:142:10: warning: passing argument 1 of ‘show’ from incompatible pointer type [-Wincompatible-pointer-types]
     show(B02List);
          ^~~~~~~
list_dir_files_4.c:105:6: note: expected ‘Node * {aka struct nlist *}’ but argument is of type ‘struct Node *’
 void show(Node *Head)
      ^~~~
list_dir_files_4.c:143:10: warning: passing argument 1 of ‘show’ from incompatible pointer type [-Wincompatible-pointer-types]
     show(SCLList);
          ^~~~~~~
list_dir_files_4.c:105:6: note: expected ‘Node * {aka struct nlist *}’ but argument is of type ‘struct Node *’
 void show(Node *Head)
      ^~~~
list_dir_files_4.c: At top level:
list_dir_files_4.c:159:14: error: conflicting types for ‘listFilesRecursively’
 struct Node *listFilesRecursively(char *basePath,char *suffix)
              ^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c:138:15: note: previous implicit declaration of ‘listFilesRecursively’ was here
     B02List = listFilesRecursively(path,suffix);
               ^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c: In function ‘listFilesRecursively’:
list_dir_files_4.c:168:9: warning: ‘return’ with no value,in function returning non-void
         return;
         ^~~~~~
list_dir_files_4.c:159:14: note: declared here
 struct Node *listFilesRecursively(char *basePath,char *suffix)
              ^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c:185:12: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
     return Head;

有人可以帮我吗?我一直在尝试弄乱类型声明,并且似乎没有任何作用,只是为了在其中调用show(Head)而不将函数**** listFilesRecursively ***声明为void,而不返回我需要的链接列表main()。

wu540642448 回答:如何使用函数返回两个不同的链表?

声明

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

说函数不返回任何内容,并且确实与您所在的定义(实现)冲突

struct Node *listFilesRecursively(char *basePath,char *suffix)

声明和定义必须相同。

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

大家都在问