duplicate removed
This commit is contained in:
parent
599e4d7e75
commit
d124049c95
@ -1,6 +1,7 @@
|
||||
elena;24999
|
||||
player_name;14999
|
||||
player_name;6999
|
||||
player_name;6999
|
||||
elena;4999
|
||||
player1;3999
|
||||
elena;2999
|
||||
|
||||
27
numbers.c
27
numbers.c
@ -11,23 +11,22 @@
|
||||
* Duplizieren eines zufälligen Eintrags im Array.
|
||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
||||
|
||||
// Comparison function for unsigned integers used in the binary search tree
|
||||
// Comparison function for unsigned integers used in BST and sorting operations.
|
||||
// Returns: <0 if a < b, 0 if a == b, >0 if a > b
|
||||
// This function is passed to addToTree to determine ordering
|
||||
int compareUnsignedInts(const void *a, const void *b)
|
||||
// This is an internal function (static) since it's not part of the public API.
|
||||
static int compareUnsignedInts(const void *a, const void *b)
|
||||
{
|
||||
unsigned int valA = *(const unsigned int *)a;
|
||||
unsigned int valB = *(const unsigned int *)b;
|
||||
|
||||
// Simple subtraction works for unsigned integers in this case
|
||||
// (Note: in general subtraction can overflow, but our range is small)
|
||||
// Safe comparison without overflow concerns for our range
|
||||
if (valA < valB) return -1;
|
||||
if (valA > valB) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||
// Returns NULL on errors. Use the implementation of the binary search tree to check for possible duplicates while
|
||||
// creating random numbers.
|
||||
//
|
||||
// Algorithm:
|
||||
@ -66,7 +65,7 @@ unsigned int *createNumbers(unsigned int len)
|
||||
int isDuplicate = 0;
|
||||
usedNumbers = addToTree(usedNumbers, &randomNumber, sizeof(unsigned int), compareUnsignedInts, &isDuplicate);
|
||||
|
||||
if (usedNumbers == NULL)
|
||||
if (usedNumbers == NULL) // checks if addTOTree failed
|
||||
{
|
||||
free(numbers);
|
||||
return NULL;
|
||||
@ -103,17 +102,7 @@ unsigned int *createNumbers(unsigned int len)
|
||||
return numbers;
|
||||
}
|
||||
|
||||
// Comparison function for qsort to sort unsigned integers in ascending order
|
||||
// Returns: <0 if a < b, 0 if a == b, >0 if a > b
|
||||
static int compareUnsigned(const void *a, const void *b)
|
||||
{
|
||||
unsigned int valA = *(const unsigned int *)a;
|
||||
unsigned int valB = *(const unsigned int *)b;
|
||||
|
||||
if (valA < valB) return -1;
|
||||
if (valA > valB) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||
//
|
||||
@ -140,8 +129,8 @@ unsigned int getDuplicate(const unsigned int *numbers, unsigned int len)
|
||||
// Step 3: Copy the array
|
||||
memcpy(sorted, numbers, len * sizeof(unsigned int));
|
||||
|
||||
// Step 4: Sort the copy
|
||||
qsort(sorted, len, sizeof(unsigned int), compareUnsigned);
|
||||
// Step 4: Sort the copy using the shared comparison function
|
||||
qsort(sorted, len, sizeof(unsigned int), compareUnsignedInts);
|
||||
|
||||
// Step 5: Compare adjacent elements to find the duplicate
|
||||
for (unsigned int i = 0; i < len - 1; i++)
|
||||
|
||||
BIN
runTest_numbers
BIN
runTest_numbers
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user