【发布时间】:2013-05-19 18:02:11
【问题描述】:
基本上我想要一个链表数组,每个链表都有自己的标题。 这是我的代码:
struct node{
int location;
struct node *next;
struct node *previous;
};
typedef struct ListHeader{
nodeType *first;
nodeType *current;
nodeType *last;
} ListHeader;
struct adjList{
ListHeader *header;
int size;
};
struct List{
adjListType *list;
int size;
};
ListType newList(int numVerts){
ListType new = malloc(sizeof(struct List));
new->list = calloc(numVerts, sizeof(adjListType));
new->size = numVerts;
int i;
for(i = 0; i <= numVerts; i++){
new->list[i] = newAdjList();
}
return new;
}
adjListType newAdjList(void){
adjListType new = malloc(sizeof(struct adjList));
new->header = malloc(sizeof(ListHeader));
new->header->first = NULL;
new->header->current = NULL;
new->header->last = NULL;
new->size = 0;
return new;
}
nodeType newNode(int location){
nodeType new = malloc(sizeof(struct node));
new->location = location;
return new;
}
当我尝试使用此代码移动到链表中的下一个节点时,它给了我一个错误 (ListType l, int 位置)
l->list[location]->header->current = l->list[location]->header->current->next;
这是我得到的错误:
成员引用基类型“nodeType”(又名“struct node*”)不是结构或联合
【问题讨论】:
-
在我的头文件中,我将 node 定义为 typedef struct node *nodeType;
-
@user2416553 对我来说,那里有很多指针,你可能根本不需要。
-
@user2416553 就“设计”而言,Kraken 的建议非常好。我用你的设计更新了我的答案。
标签: c arrays linked-list nodes