#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct _TNode
{
int value;
struct _TNode* next;
} TNode;
//-----------------------------------------------------------------------------
TNode* Push(TNode** list, int value)
{
TNode* node = malloc(sizeof(TNode));
node->value = value;
node->next = *list;
*list = node;
return *list;
}
//-----------------------------------------------------------------------------
int Pop(TNode** list)
{
TNode* node = *list;
int value = node->value;
*list = node->next;
free(node);
return value;
}
//-----------------------------------------------------------------------------
int IsNegative(int value)
{
return (value < 0);
}
//-----------------------------------------------------------------------------
TNode* Remove(TNode** list, int (*Func)(int))
{
TNode** node = list;
while (*node)
{
if (Func((*node)->value))
{
Pop(node);
}
else
{
node = &(*node)->next;
}
}
return *list;
}
//-----------------------------------------------------------------------------
void Print(const TNode* list)
{
for (; list; list = list->next)
{
printf("%d ", list->value);
}
printf("\n");
}
//-----------------------------------------------------------------------------
int main()
{
TNode* list = NULL;
unsigned i = 10;
srand(time(NULL));
while (i--)
{
Push(&list, rand() % 20 - 10);
}
Print(list);
Remove(&list, IsNegative);
Print(list);
return 0;
}