I have a singly linked list with node and list structs defined as:
typedef struct _node
{
int data;
node* next;
} node;
typedef struct _list
{
node* start;
} list;
I wrote a function to remove the leading entries of a particular value from the list, but it doesn’t seem to be working as intended. After running my function and reprinting my list, instead of the element no longer being there, there is a large value in its place (around 134520848). My function for removing leading values of a defined value is:
void removeLead(list* l, int n)
{
node* current = l->start;
node* temp = NULL;
while (current->data == n)
{
temp = current;
l->start = current->next;
free(temp);
}
}
If I have 4 leading values == n then, after running removeLead(), I have 3 leading entries of ~134520848.
There is a mistake in the code. You are not reassigning the current variable.