[ create a new paste ] login | about

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

anmartex - C++, pasted on Apr 9:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

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

   *list = node;

   return *list;
}
//-----------------------------------------------------------------------------
void Clear(TList** list)
{
   TList* node;

   while (*list)
   {
      node = *list;
      *list = (*list)->next;
      free(node);
   }
}
//-----------------------------------------------------------------------------
TList* Find(TList* list, int value)
{
   for (; list && (list->value != value); list = list->next) { ; }

   return list;
}
//-----------------------------------------------------------------------------
void Print(TList* list)
{
   for (; list; list = list->next)
   {
      printf("%d ", list->value);
   }
   printf("\n");
}
//-----------------------------------------------------------------------------
int Random(int min, int max)
{
   return (rand() % (max - min) + min);
}
//-----------------------------------------------------------------------------
TList* GetGenList(size_t count, int min, int max)
{
   TList* list = NULL;

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

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

   for (; first; first = first->next)
   {
      if (!Find(list, first->value) && !Find(second, first->value))
      {
         Push(&list, first->value);
      }
   }

   return list;
}
//-----------------------------------------------------------------------------

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

   TList* l1 = GetGenList(10, 3, 10);
   TList* l2 = GetGenList(10, 0, 7);

   TList* l = GetUniqueFirst(l1, l2);

   printf("L1: "); Print(l1);
   printf("L2: "); Print(l2);
   printf("L : "); Print(l);

   Clear(&l);
   Clear(&l2);
   Clear(&l1);

   return 0;
}


Output:
1
2
3
L1: 5 6 5 4 6 3 8 6 7 3 
L2: 1 0 4 4 5 6 5 6 5 1 
L : 7 8 3 


Create a new paste based on this one


Comments: