#include <iostream>
#include <cstdlib>
#include <ctime>
struct TStack
{
int value;
TStack* next;
};
//-----------------------------------------------------------------------------
TStack* Push(TStack*& stack, int value)
{
TStack* node = new TStack;
node->value = value;
node->next = stack;
stack = node;
return stack;
}
//-----------------------------------------------------------------------------
void Clear(TStack*& stack)
{
TStack* node;
while (stack)
{
node = stack;
stack = stack->next;
delete node;
}
}
//-----------------------------------------------------------------------------
bool IsNegative(int value)
{
return (value < 0);
}
//-----------------------------------------------------------------------------
TStack*& FindElement(TStack*& stack, bool func(int))
{
if (stack && !func(stack->value))
{
return FindElement(stack->next, func);
}
return stack;
}
//-----------------------------------------------------------------------------
TStack* DeleteFirstNegative(TStack*& stack)
{
TStack*& find = FindElement(stack, IsNegative);
if (find)
{
TStack* node = find;
find = find->next;
delete node;
}
return stack;
}
//-----------------------------------------------------------------------------
int Random(int min, int max)
{
return (rand() % (max - min) + min);
}
//-----------------------------------------------------------------------------
TStack* GetGenStack(size_t count, int min, int max)
{
TStack* stack = NULL;
while (count--)
{
Push(stack, Random(min, max));
}
return stack;
}
//-----------------------------------------------------------------------------
std::ostream& operator << (std::ostream& out, const TStack* stack)
{
for (; stack; stack = stack->next)
{
out << stack->value << " ";
}
return out;
}
//-----------------------------------------------------------------------------
int main()
{
srand(time(NULL));
TStack* stack = GetGenStack(10, -10, 10);
std::cout << "source: " << stack << std::endl;
DeleteFirstNegative(stack);
std::cout << "result: " << stack << std::endl;
Clear(stack);
return 0;
}