Currently I am working on a function which deletes the number of lasts occurrences of a list element I choose. I passed to realize a function to delete the last occurrence of a list element and I was wondering how to program my last function.
The function which delete last occurrence of a specific element :
void DeleteLastOccurence(int x, List *l)
{
List *p = l;
List *m = NULL;
while(*p != NULL)
{
if((**p).number == x)
{
m = p;
}
p = &(**p).next;
}
if(m != NULL)
{
depile(m);
}
}
My List structure :
typedef struct Block
{
int number;
struct Block *next;
} Block;
typedef Block *List;
depile subfunction, which removes the first element of the list in parameter :
void depile(List *L)
{
List tmp = *L ;
*L = Lnext(*L) ;
free(tmp) ;
}
and Lnext subfunction, returns a new List corresponding to the one in parameter without its first element :
Liste Lnext(List l)
{
return l->next;
}
I test my current function with the list [2;1;1;2;4;4;4;4] and x = 2 and I get [2;1;1;4;4;4; 4].
screen of my terminal :
I think I need to add an integer k to delete the k lasts occurrences of the element.
In my last function, with the same list, x = 4 and k = 3, I should get the list [2;1;1;2;4].
So I would like to know if I could just modify this function a bit to get the final version?
for example, previously I realize a function which delete first occurrence of a list element :
void DeleteFirstOccurence(int x, Liste *l)
{
if(isEmpty(*l))
{
return;
}
else
{
if(x == firstElement(*l))
{
depile(l);
}
else
{
DeleteFirstOccurence(x, &(*l)->suivant);
}
}
}
with FirstElement and isEmpty subfunctions :
int FirstElement(List l)
{
return l->number ;
}
bool isEmpty(List l)
{
return l == NULL ;
}
and by using it, I was able to implement the function that eliminates the first k occurrences :
void DeleteKfirstsOccurrences(int x, int k, List *l)
{
if(isEmpty(*l))
{
return;
}
else
{
for(int i = 0; i < k; ++i)
{
if(x == FirstElement(*l))
{
depile(l);
}
else
{
DeleteFirstOccurence(x, &(*l)->next);
}
}
}
}
DeleteFirstOccurence with x = 0 and list [4;2;0] and DeleteKFirstsOccurences with x = 5, k = 5 and list = 2 1 1 2 4 4 4 4:
if it would be possible to perform a similar process on my first function to get the desired result, knowing that the structure is not the same and that I personally did not succeed.
P.S : In my different functions, if k > number of occurrence of x in list, delete all occurrences.
Thank you for your time and your different suggestions,
Sincerely.
(Sorry for my English level, I don't usually write so much)