链接列表和指向不完整类类型的指针

时间:2016-04-02 16:42:10

标签: c data-structures linked-list

我试图将一封信与数据部分进行比较,以便在链表中按字母顺序插入,这有什么问题?

typedef struct{
    char data;
    struct list_node *next;
}list_node;

typedef struct{
    list_node *head;
}list;

我试图执行以下操作:

void input_char(list *my_list, char x)
{
    list_node *node = (list_node*)calloc(1, sizeof(list_node));
    list_node *tmp = my_list->head;
    node->data = x;

    if (tmp == NULL)
        my_list->head = tmp;
    else if (tmp->next == NULL)
    {
        if (x < tmp->data)
        {
            node->next = tmp;
            my_list->head = node;
        }
        else
            tmp->next = node;

        tmp = tmp->next;
    }
    else
    {
        if (x < tmp->next->data) 
  // This following line says "Error, Pointer to incomplete type is not allowed.
        {
            node->next = tmp->next;
            tmp->next = node;
        }
        tmp = tmp->next;
    }
}

1 个答案:

答案 0 :(得分:3)

更改

typedef struct{
    char data;
    struct list_node *next;
}list_node;

typedef struct list_node{
    char data;
    struct list_node *next;
}list_node;

您的编译器不知道struct list_node是什么,因此您必须声明它。