[ create a new paste ] login | about

Link: http://codepad.org/QleTo7yQ    [ raw code | output | fork ]

anmartex - C, pasted on Nov 26:
#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;
}


Output:
1
2
3 -3 3 9 6 -9 -1 8 -10 0 
3 3 9 6 8 0 


Create a new paste based on this one


Comments: