For a project I need a linked list implementation in C which enables me to delete the last element.
However, I don't know how to achieve this. The idea was to create a function (deleteLast) which iterates over the list until the next of the next element is NULL (so until the second last element is reached) to then free the reference to the last element.
However, I get an error "expression must have pointer-to-struct-or-union type" when trying to compile.
#include <stdio.h>
#include <stdlib.h>
struct cell{
int x_coord,y_coord;
struct cell *next;
} cell;
struct cell AddToList (struct cell *list, int x,int y);
int listlength(struct cell * list);
void deleteLast(struct cell **list);
struct cell AddToList(struct cell *list,int x,int y){
struct cell *new_cell;
new_cell = malloc(sizeof(struct cell));
new_cell->x_coord=x;
new_cell->y_coord=y;
printf("Added new coordinates %i %i",x,y);
}
int listlength(struct cell *list){
int i=0;
while(list->next != NULL){
i++;
}
return i;
}
//takes a pointer as reference, because in C parameters are given as values
//see: https://stackoverflow.com/a/35021864
//calls should look like deleteLast( &list )
void deleteLast(struct cell **list){
struct cell *currentcell = *list;
while(*list->next->next != NULL){ //expression must have pointer-to-struct-or-union type
//free list->next
}
}
Where's the error?