For my linked list, I have the following:
typedef struct node {
char * value;
struct node *next;
} NODE;
void delete_nth(NODE **list, int n){
NODE *cur = *list;
int i;
if (n == 0){
printf("\n==========\n");
list = list->next;
cur->next = NULL;
free(cur);
} else {
for(i = 0; i < n; i ){
cur = cur->next;
}
}
return ;
}
But it returns these errors:
error: '*list' is a pointer; did you mean to use '->'?
error: request for member 'next' in something not a structure or union
What am I doing wrong here?
CodePudding user response:
The problem is NODE **. NODE ** becomes struct node ** which means pointer to pointer to struct node.
The following two lines from your program are not correct:
void delete_nth(NODE **list, int n){
NODE *cur = *list;
These lines should be changed to:
void delete_nth(NODE *list, int n){
NODE *cur = list;
So, now NODE **list
is changed to NODE *list
and NODE *cur = *list
is changed to NODE *cur = list
.
CodePudding user response:
This line is not correct:
list = list->next;
list
does not point to a struct but to a pointer to struct.
Also you don't want to update list
but where list
points to.
This would be
*list = (*list)->next;
If you change list
, the calling function would not see it as it is only a copy of the passed value.
Besides this, you forgot to actually remove the nth element after iterating through your list. But that is another topic...