链表错误

发布于 2024-09-27 15:26:52 字数 2925 浏览 2 评论 0原文

我在尝试用 c 编译链表实现时出现错误和警告

警告:来自不兼容指针类型的赋值

错误:取消引用指向不完整类型的指针 这是列表.h

#ifndef LIST_H
#define LIST_H
#include <stdio.h>

typedef struct _ListElmt{

void                *data;
struct ListElmt     *next;

} ListElmt;
typedef struct _List{

int                 size;
int                 (*match)(const void *key1, const void *key2);
void                (*destroy)(void *data);

ListElmt             *head;
ListElmt             *tail;

} List;

void list_init(List *list, void (*destroy)(void *data));

void list_destroy(List *list);

int list_ins_next(List *list, ListElmt *element, const void *data);

int list_rem_next(List *list, ListElmt *element, void **data);

int list_size(const List *list);

ListElmt *list_head(const List *list);

ListElmt *list_tail(const List *list);

int list_is_head(const ListElmt *element);

int list_is_tail(const ListElmt *element);

void *list_data(const ListElmt *element);

ListElmt *list_next(const ListElmt *element);
#endif

,这是list.c

#include <stdlib.h>
#include <string.h>
#include "List.h"

void list_init(List *list, void (*destroy)(void *data)){

list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail  = NULL;

return;
}

void list_destroy(List *list){

void                *data;

while(list_size(list) > 0){

    if(list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL){

        list->destroy(data);
    }
}
memset(list, 0, sizeof(List));
return;
}

int list_ins_next(List *list, ListElmt *element, const void *data){

ListElmt            *new_element;

if((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL) // not yet understanded
    return -1;

new_element->data = (void *)data;

if(element == NULL){
    if(list_size(list) == 0)
        list->tail = new_element;

    new_element->next = list->head;
    list->head = new_element;
}

else{
    if(element->next == NULL)
        list->tail = new_element;

    new_element->next = element->next;
    element->next = new_element;
}

list->size++;
return 0;
}

int list_rem_next(List *list, ListElmt *element, void **data){

ListElmt            *old_element;

if(list_size(list) == 0)
    return -1;

if(element == NULL){ // Handle removal of the head

        *data = list->head->data;
        old_element = list->head;
        list->head = list->head->next;

        if(list_size(list) == 1)
            list->tail = NULL;
}

else{ // Handle removal from somewhere else

    if(element->next == NULL)
        return -1;

    *data = element->next->data;
    old_element = element->next;
    element->next = element->next->next;

   if (element->next == NULL)
      list->tail = element;
}

free(old_element);

list->size--;
return 0;
}

I have an error and a warning while trying to compile my linked list implementation in c

warning: assignment from incompatible pointer type

error: dereferencing pointer to incomplete type
here is the list.h

#ifndef LIST_H
#define LIST_H
#include <stdio.h>

typedef struct _ListElmt{

void                *data;
struct ListElmt     *next;

} ListElmt;
typedef struct _List{

int                 size;
int                 (*match)(const void *key1, const void *key2);
void                (*destroy)(void *data);

ListElmt             *head;
ListElmt             *tail;

} List;

void list_init(List *list, void (*destroy)(void *data));

void list_destroy(List *list);

int list_ins_next(List *list, ListElmt *element, const void *data);

int list_rem_next(List *list, ListElmt *element, void **data);

int list_size(const List *list);

ListElmt *list_head(const List *list);

ListElmt *list_tail(const List *list);

int list_is_head(const ListElmt *element);

int list_is_tail(const ListElmt *element);

void *list_data(const ListElmt *element);

ListElmt *list_next(const ListElmt *element);
#endif

and here is the list.c

#include <stdlib.h>
#include <string.h>
#include "List.h"

void list_init(List *list, void (*destroy)(void *data)){

list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail  = NULL;

return;
}

void list_destroy(List *list){

void                *data;

while(list_size(list) > 0){

    if(list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL){

        list->destroy(data);
    }
}
memset(list, 0, sizeof(List));
return;
}

int list_ins_next(List *list, ListElmt *element, const void *data){

ListElmt            *new_element;

if((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL) // not yet understanded
    return -1;

new_element->data = (void *)data;

if(element == NULL){
    if(list_size(list) == 0)
        list->tail = new_element;

    new_element->next = list->head;
    list->head = new_element;
}

else{
    if(element->next == NULL)
        list->tail = new_element;

    new_element->next = element->next;
    element->next = new_element;
}

list->size++;
return 0;
}

int list_rem_next(List *list, ListElmt *element, void **data){

ListElmt            *old_element;

if(list_size(list) == 0)
    return -1;

if(element == NULL){ // Handle removal of the head

        *data = list->head->data;
        old_element = list->head;
        list->head = list->head->next;

        if(list_size(list) == 1)
            list->tail = NULL;
}

else{ // Handle removal from somewhere else

    if(element->next == NULL)
        return -1;

    *data = element->next->data;
    old_element = element->next;
    element->next = element->next->next;

   if (element->next == NULL)
      list->tail = element;
}

free(old_element);

list->size--;
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单挑你×的.吻 2024-10-04 15:26:52

更改

struct ListElmt *next; 

struct _ListElmt *next; 

list.hstruct _ListElmt 的定义

Change

struct ListElmt *next; 

to

struct _ListElmt *next; 

in the definition of struct _ListElmt in list.h

不乱于心 2024-10-04 15:26:52

list.c 的最后一行(来自不兼容的指针 typeBlockquote )对我来说看起来很可疑。

The last line of list.c ( }from incompatible pointer typeBlockquote ) looks suspicious to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文