gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:33:15 25 4
gpt4 key购买 nike

我正在按照以下代码工作:

#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, 0, 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;

有人可以帮我解决这个问题吗?我一直在尝试搞乱类型声明,除了将函数 ****listFilesRecursively*** 声明为 void 只是为了在其中调用 show(Head) ,而不返回我需要的链接列表之外,似乎没有什么其他作用main()。

最佳答案

声明

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

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

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

声明和定义必须相同。

关于c - 如何使用函数返回两个不同的链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com