Array of a linked list, moving on to the next node error;

Basically, I need an array of linked lists, each linked list has its own header. This is my code:

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; } 

and this gives me an error when I try to move to the next node in a linked list using this code (ListType l, int location)

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

This is the error I get:

The reference linkbase type 'nodeType' (aka 'struct node *') is not a structure or join

+4
source share
2 answers

If you need an array of linked lists, why use a pointer?

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

Of course, you can also use a pointer, but then you need to show us how you allocate array memory using calloc ?


In accordance with the updated code in question. The following are the corrected errors ...

 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; } 

You may also need to return & new as a link, or you will create unnecessary copies ...

I will go to your code and update this answer if I find anything else. Meanwhile, it will be great if you tell us what mistake you get?

Also in your code shown, you set next and prev and current to NULL , but where you change these values ​​... otherwise you will continue to get NULL POINTER EXCEPTION

+1
source

Create an array of a pointer to a struct node . This should be enough for an array of linked lists.

Each element of the array, i.e. a pointer to a struct node will act as the heading of the list, and the list can be maintained by adding / removing an element from the list.

0
source

Source: https://habr.com/ru/post/1482514/


All Articles