generated from freudenreichan/info2Praktikum-DobleSpiel
changes treenode struct damit beide nicht den selben namen haben
This commit is contained in:
parent
1389fcc864
commit
aa83f84a21
@ -5,11 +5,11 @@
|
|||||||
|
|
||||||
typedef int (*CompareFctType)(const void *arg1, const void *arg2);
|
typedef int (*CompareFctType)(const void *arg1, const void *arg2);
|
||||||
|
|
||||||
typedef struct node
|
typedef struct treenode
|
||||||
{
|
{
|
||||||
void *data;
|
void *data;
|
||||||
struct node *left;
|
struct treenode *left;
|
||||||
struct node *right;
|
struct treenode *right;
|
||||||
} TreeNode;
|
} TreeNode;
|
||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||||
|
|||||||
87
numbers.c
87
numbers.c
@ -11,16 +11,93 @@
|
|||||||
* Duplizieren eines zufälligen Eintrags im Array.
|
* Duplizieren eines zufälligen Eintrags im Array.
|
||||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
||||||
|
|
||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
//Vergleichsfunktion von qsort
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
static int compareUnsignedInt(const void *a, const void *b)
|
||||||
// creating random numbers.
|
{
|
||||||
|
const unsigned int *x = (const unsigned int *)a;
|
||||||
|
const unsigned int *y = (const unsigned int *)b;
|
||||||
|
|
||||||
|
if (*x < *y) return -1;
|
||||||
|
if (*x > *y) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Mischen des Arrays
|
||||||
|
static void shuffleArray(unsigned int *array, unsigned int n)
|
||||||
|
{
|
||||||
|
if (n > 1)
|
||||||
|
{
|
||||||
|
for (unsigned int i = n - 1; i > 0; i--)
|
||||||
|
{
|
||||||
|
unsigned int j = rand() % (i + 1);
|
||||||
|
unsigned int temp = array[i];
|
||||||
|
array[i] = array[j];
|
||||||
|
array[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Wenn weniger als zwei Zahlen
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len < 2) return NULL;
|
||||||
|
|
||||||
|
//Dynamisches Array
|
||||||
|
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
||||||
|
if (numbers == NULL) return NULL;
|
||||||
|
//Variabelen für den Binärbaum
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int isDuplicate = 0;
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
|
|
||||||
|
while (count < len - 1)
|
||||||
|
{ //Zufallszahlen generieren
|
||||||
|
unsigned int value = (rand() % (2 * len)) + 1;
|
||||||
|
|
||||||
|
|
||||||
|
root = addToTree(root, &value, sizeof(unsigned int), compareUnsignedInt, &isDuplicate);
|
||||||
|
|
||||||
|
if (isDuplicate == 0)
|
||||||
|
{ //in array schreiben falls kein Duplikat
|
||||||
|
numbers[count] = value;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
}
|
||||||
|
//Duplikat erzeugen
|
||||||
|
unsigned int randomIndex = rand() % (len - 1);
|
||||||
|
unsigned int duplicateValue = numbers[randomIndex];
|
||||||
|
numbers[len - 1] = duplicateValue;
|
||||||
|
root = addToTree(root, &duplicateValue, sizeof(unsigned int), compareUnsignedInt, NULL);
|
||||||
|
//Array mischen damit duplikat nicht am Ende immer ist
|
||||||
|
shuffleArray(numbers, len);
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
return numbers;
|
||||||
|
}
|
||||||
|
//get Duplicate
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (numbers == NULL || len < 2) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
//Kopie vom Array anlegen
|
||||||
|
unsigned int *copy = malloc(len * sizeof(unsigned int));
|
||||||
|
|
||||||
|
if (copy == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
memcpy(copy, numbers, len * sizeof(unsigned int));
|
||||||
|
qsort(copy, len, sizeof(unsigned int), compareUnsignedInt);
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
//Duplikat finden
|
||||||
|
for (unsigned int i = 0; i + 1 < len; ++i) {
|
||||||
|
if (copy[i] == copy[i + 1]) {
|
||||||
|
duplicate = copy[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Speicher freigeben
|
||||||
|
free(copy);
|
||||||
|
return duplicate;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user