“我正在尝试实现一个链表,该链表稍后将利用字典排序功能,但在listnode中,由于类型冲突,我一直遇到错误。
我不知道是什么原因造成的。以前,我在尝试使用sizeof进行malloc时遇到问题,并且发现解决方案将next声明为listnode *指针。它解决了该问题,但类型仍有冲突。当我尝试编译时,出现以下错误消息:
ls.c:18:3:错误:“ listnode”的类型冲突
} listnode:
ls.c:12:14:注意先前的'listnode'声明在这里
typedef节点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 node;
关于c - c-链表结构中的错误冲突类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58383115/