gpt4 book ai didi

c- 链表结构中的类型冲突错误

转载 作者:行者123 更新时间:2023-11-30 16:12:13 25 4
gpt4 key购买 nike

“我正在尝试实现一个链接列表,稍后将使用字典排序函数,但我不断收到有关列表节点中冲突类型的错误。

我不知道这个问题的原因是什么。以前,我在尝试使用 sizeof 进行 malloc 时遇到问题,我发现解决方案是将 next 声明为 listnode* 指针。它解决了这个问题,但仍然存在冲突的类型。当我尝试编译时收到此错误消息:

ls.c:18:3: error: conflicting types for 'listnode'
} listnode:
ls.c:12:14: note previous declaration of 'listnode' was here
typedef node listnode;

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int strcmp(const char *str1, const char *str2);

//This chunk of code is dedicated to the formation and functions of Linked list
//which will be used to store and sort file names.
typedef struct node node;
typedef node listnode;
#define EMPTY NULL;

typedef struct listnode{
struct listnode* next;
char* string;
} listnode;

struct listnode* head;

//This function will print the entire list
void printlist(listnode* head){
struct listnode* current = head;

while(current != NULL){
printf("%s \n", current->string);
current = current->next;
}
}

//This function creates a new node
listnode* newNode(char* str, listnode* node){
listnode* new = (listnode*)malloc(sizeof(listnode*));
if(new == NULL){
printf("Error creating new listnode.\n");
exit(0);
}
new->string = str;
new->next = node;

return new;
}

//This function will append a new node onto the list
listnode* list_append(listnode* head, char* str){
if (head == NULL){
listnode* new = newNode(str, head);
head = new;
return head;
}
else{
listnode* current = head;
while(current-> next != NULL){
current = current->next;
}

listnode* new = newNode(str,NULL);
current -> next = new;
}
return head;
}


//This function earses the list freeing up space
void list_free(listnode* head){
listnode* current;
listnode* temp;

if(head != NULL){
current = head->next;

if(head !=NULL){
current = head -> next;
head ->next = NULL;
while(current != NULL){
temp = current -> next;
free(current);
current = temp;
}
}
}
free(head);
}



//This is the end of the linked list code

int main(int argc, char **argv){
char *current_dir = NULL;
DIR *direct_ptr = NULL;
struct dirent *dir_ptr = NULL;
unsigned int fileNum = 0;
int c;
listnode* head = NULL;

current_dir = getenv("PWD");
if(NULL == current_dir){
printf("\n Error: Couldn't grab current directory.\n");
return -1;
}

direct_ptr = opendir((const char*)current_dir);
if(NULL == direct_ptr){
printf("\n Error: couldn't open current directory\n");
return -1;
}

if(argc == 1){
for(fileNum=0; NULL != (dir_ptr = readdir(direct_ptr)); fileNum++){
if(dir_ptr->d_name[0] != '.'){
head = list_append(head, dir_ptr->d_name);
}
}
}
else{
if(strcmp(argv[1], "-a") || strcmp(argv[1], "[-a]")){
for(fileNum=0; NULL != (dir_ptr = readdir(direct_ptr)); fileNum++){
head = list_append(head, dir_ptr->d_name);
}
}
else{
printf("\n Unrecognized argument in command line.\n");
return -1;
}
}
return 0;
}

最佳答案

实际上,您对 listnode 的定义确实存在冲突。您首先在此处定义类型:

typedef struct node node;
typedef node listnode;

然后在这里再次定义它:

typedef struct listnode{
struct listnode* next;
char* string;
} listnode;

在一种情况下,它是 struct node 的别名,在另一种情况下,它是 struct listnode 的别名。

删除第一个以及typedef struct node节点;

关于c- 链表结构中的类型冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58383115/

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