簡體   English   中英

c-鏈表結構中的錯誤沖突類型

[英]c- Error Conlficting types within linked list struct

“我正在嘗試實現一個鏈接列表,該鏈接列表稍后將使用字典排序 function,但我一直收到有關 listnode 中沖突類型的錯誤。

我不知道這個問題的原因是什么。 以前我在嘗試使用 sizeof 進行 malloc 時遇到問題,我發現解決方案將 next 聲明為 listnode* 指針。 它解決了這個問題,但它仍然有沖突的類型。 當我嘗試編譯時,我收到此錯誤消息:

ls.c:18:3:錯誤:“ listnode ”的類型沖突
} 列表節點:
ls.c:12:14: 注意之前的“ listnode ”聲明在這里
typedef 節點列表節點;

我的代碼:

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

您有兩個listnode定義:

typedef node listnode;

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

第一個是不必要的,並且不起作用,因為它使用node ,它被定義為

typedef struct node node;

但是您從未在任何地方定義struct node

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM