它CRYS該行:
List_Node * node = (List_Node*) malloc(sizeof(List_Node));
Ansi C鏈表我做錯了什麼?
失敗上:
1>list.c(31): error C2275: 'List_Node' : illegal use of this type as an expression
1>list.c(8) : see declaration of 'List_Node'
h文件:
#ifndef _LIST_H
#define _LIST_H
typedef struct List_Node;
typedef struct List_Struct
{
unsigned int count;
struct List_Node * root;
struct List_Node * last;
int SizeOfData;
}List_Struct;
#endif
C_FILE:
typedef struct List_Node
{
void * data;
struct List_Node * next;
}List_Node;
Status List__Add (List_Struct * This,void * const item)
{
Assert(This)
Assert(item)
struct List_Node * node = (List_Node*) malloc(sizeof(List_Node));
IsAllocated(node);
node->data = malloc(This->SizeOfData);
IsAllocated(node->data);
memcpy(node->data,item,This->SizeOfData);
node->next = NULL;
if(NULL == This->root) /*if first item to be added*/
{
This->root= node;
This->last =This->root;
}
else
{
This->last->next = node;
}
return STATUS_OK;
}
在頭文件,則跳過用於'List_Node'結構的'typedef'。另外,當列表爲空時添加節點時,不要設置This-> last。 – 2012-02-18 12:36:40