Refactor createNumbers and getDuplicate functions for improved error handling and code clarity
This commit is contained in:
parent
38e5ff6a0a
commit
11101adb0b
42
numbers.c
42
numbers.c
@ -25,42 +25,43 @@ int compareUInts(const void *arg1, const void *arg2){
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
unsigned int* createNumbers(unsigned int len)
|
||||
{
|
||||
if(len == 0){
|
||||
return NULL;
|
||||
return NULL; //Check if len ist 0o
|
||||
}
|
||||
|
||||
unsigned int* numbers = (unsigned int*)malloc(len * sizeof(unsigned int));
|
||||
unsigned int* numbers = (unsigned int*)malloc(len * sizeof(unsigned int)); //Allocate Memory for numbers Array
|
||||
if(numbers == NULL){
|
||||
return NULL;
|
||||
return NULL; //Fallback if memory allocation error
|
||||
}
|
||||
|
||||
TreeNode *tree = NULL;
|
||||
|
||||
unsigned int i = 0;
|
||||
while(i < len){
|
||||
unsigned int tmp = (rand() % (2*len)) + 1;
|
||||
unsigned int tmp = (rand() % (2*len)) + 1; //Generate Random value
|
||||
|
||||
int isDuplicate = 0;
|
||||
tree = addToTree(tree, &tmp, sizeof(unsigned int), compareUInts, &isDuplicate);
|
||||
tree = addToTree(tree, &tmp, sizeof(unsigned int), compareUInts, &isDuplicate); //Add random value to tree
|
||||
|
||||
if(!isDuplicate){
|
||||
numbers[i] = tmp;
|
||||
numbers[i] = tmp; //Add number to array if no duplicate
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
clearTree(tree);
|
||||
clearTree(tree); //Free Memory
|
||||
|
||||
unsigned int rIdx = rand() % len;
|
||||
unsigned int duplicate = numbers[rIdx];
|
||||
unsigned int rIdx = rand() % len; //Select duplicate number
|
||||
unsigned int duplicate = numbers[rIdx];
|
||||
|
||||
unsigned int tIdx = rand() % len;
|
||||
while(tIdx == rIdx){
|
||||
tIdx = rand() % len;
|
||||
tIdx = rand() % len; //Select target index
|
||||
}
|
||||
numbers[tIdx] = duplicate;
|
||||
numbers[tIdx] = duplicate;
|
||||
|
||||
return numbers;
|
||||
}
|
||||
@ -69,24 +70,27 @@ unsigned int* createNumbers(unsigned int len)
|
||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||
{
|
||||
if(numbers == NULL || len < 2){
|
||||
return 0;
|
||||
return 0; //Fallback
|
||||
}
|
||||
|
||||
unsigned int* sorted = (unsigned int*)malloc(len * sizeof(unsigned int));
|
||||
unsigned int* sorted = (unsigned int*)malloc(len * sizeof(unsigned int)); //Allocate Memory for sorted array
|
||||
if(sorted == NULL){
|
||||
return 0;
|
||||
return 0; //Fallback if memory allocation error
|
||||
}
|
||||
|
||||
//Create tree for duplicate search
|
||||
TreeNode *tree = NULL;
|
||||
for(unsigned int i = 0; i < len; i++){
|
||||
tree = addToTree(tree, &numbers[i], sizeof(unsigned int), compareUInts, NULL);
|
||||
tree = addToTree(tree, &numbers[i], sizeof(unsigned int), compareUInts, NULL);
|
||||
}
|
||||
|
||||
//Write sorted Elements from Tree to arary
|
||||
sorted[0] = *(unsigned int *)nextTreeData(tree);
|
||||
|
||||
for(unsigned int j = 1; j < len; j++){
|
||||
sorted[j] = *(unsigned int*)nextTreeData(NULL);
|
||||
sorted[j] = *(unsigned int*)nextTreeData(NULL);
|
||||
}
|
||||
|
||||
//Get duplicate
|
||||
unsigned int duplicate = 0;
|
||||
for(unsigned int k = 0; k < len - 1; k++){
|
||||
if(sorted[k] == sorted[k+1]){
|
||||
@ -94,6 +98,10 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Free memory
|
||||
clearTree(tree);
|
||||
free(sorted);
|
||||
|
||||
return duplicate;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user