[ create a new paste ] login | about

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

anmartex - C, pasted on Sep 8:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct node_t
{
    int value;
    struct node_t* next;
}   TList;

//-----------------------------------------------------------------------------
TList* PushFront(TList** list, int value)
{
    TList* node = malloc(sizeof(TList));
    node->value = value;
    node->next = *list;

    *list = node;

    return *list;
}
//-----------------------------------------------------------------------------
int Random(int min, int max)
{
    return rand() % (max - min + 1) + min;
}
//-----------------------------------------------------------------------------
TList* GetRandomList(size_t count, int min, int max)
{
    TList* list = NULL;

    while (count--)
    {
        PushFront(&list, Random(min, max));
    }

    return list;
}
//-----------------------------------------------------------------------------
TList* GetDifferenceList(const TList* first, const TList* second)
{
    TList* result = NULL;

    const TList* tmp;

    for (; first; first = first->next)
    {
        tmp = second;

        for (; tmp && first->value != tmp->value; tmp = tmp->next) { ; }

        if (tmp == NULL)
        {
            PushFront(&result, first->value);
        }
    }

    return result;
}
//-----------------------------------------------------------------------------
void PrintList(const TList* list)
{
    for (; list; list = list->next)
    {
        printf("%d, ", list->value);
    }
    printf("\b\b \n");
}
//-----------------------------------------------------------------------------

int main()
{
    srand(time(NULL));

    TList* first = GetRandomList(5, 1, 10);
    TList* second = GetRandomList(5, 5, 15);

    printf("first:  ");
    PrintList(first);

    printf("second: ");
    PrintList(second);

    TList* result = GetDifferenceList(first, second);
    
    printf("result: ");
    PrintList(result);

    return 0;
}


Output:
1
2
3
first:  7, 8, 1, 4, 5,  
second: 14, 9, 11, 8, 5,  
result: 4, 1, 7,  


Create a new paste based on this one


Comments: