加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 链接列表数组,移动到下一个节点错误;

发布时间:2020-12-16 07:04:31 所属栏目:百科 来源:网络整理
导读:基本上我想要一个链表列表,每个链表都有自己的标题. 这是我的代码: struct node{ int location; struct node *next; struct node *previous;};typedef struct ListHeader{ nodeType *first; nodeType *current; nodeType *last;} ListHeader;struct adjList
基本上我想要一个链表列表,每个链表都有自己的标题.
这是我的代码:

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 location)

l->list[location]->header->current = l->list[location]->header->current->next;

这是我得到的错误:

成员引用基类型’nodeType'(又名’struct node *’)不是结构或联合

解决方法

如果你想要链表的数组,为什么要使用指针?

struct List{
    adjListType list[10];
    int size;
};

当然你也可以使用Pointer,但是你需要向我们展示你如何使用calloc为它分配数组内存?

根据有问题的更新代码..下面是错误的固定行…

ListType newList(int numVerts){
    ListType new = malloc(sizeof(struct List));
    new->list = calloc(numVerts,sizeof(struct adjListType));//Here you missed struct
    new->size = numVerts;
    int i;
    for(i = 0; i < numVerts; i++){ // Here <= instead of < for 10 length array is 0 to 9
        new->list[i] = newAdjList();
    }
    return new;
}

此外,您可能希望返回& new作为参考,或者您最终将创建不必要的副本…

我要去你的代码,如果我发现了其他任何内容,我会更新这个答案..同时如果你能告诉我们你得到了什么错误会很好吗?

同样在您显示的代码中,您将设置next,prev和current为NULL,但是您要更改这些值…否则您将继续获取NULL POINTER EXCEPTION

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读