diff --git a/bintree.c b/bintree.c index 754b60c..54f423e 100644 --- a/bintree.c +++ b/bintree.c @@ -2,6 +2,7 @@ #include "stack.h" #include "bintree.h" +static StackNode *stack; // TODO: binären Suchbaum implementieren /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv), @@ -12,25 +13,23 @@ // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) { - TreeNode newNode = {data, NULL, NULL}; - - - if(root == NULL) + if (root == NULL) { - return &newNode; + TreeNode *newNode = malloc(sizeof(TreeNode)); + newNode->data = malloc(dataSize); + newNode->data = data; + newNode->left = NULL; + newNode->right = NULL; + return newNode; } - - if (data < root->data) + if (compareFct(root->data, data) < 0) { - root->left = addToTree(root->left, data, dataSize,compareFct, isDuplicate); + root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); } - else if(data > root->data) + else if (compareFct(root->data, data) > 0) { - root->right = addToTree(root->right, data, dataSize,compareFct, isDuplicate); + root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); } - - - return root; } @@ -39,9 +38,11 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc // push the top node and push all its left nodes. void *nextTreeData(TreeNode *root) { + return NULL; } + // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { @@ -49,7 +50,7 @@ void clearTree(TreeNode *root) { return; } - + if (root->left != NULL) { clearTree(root->left); diff --git a/numbers.c b/numbers.c index 9b5817a..5a354c6 100644 --- a/numbers.c +++ b/numbers.c @@ -34,7 +34,8 @@ unsigned int *createNumbers(unsigned int len) { srand(time(NULL)); unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int)); - int randomNr, randomPos, counter; + int randomNr, randomPos, counter, filler; + if (array == NULL) { @@ -45,45 +46,25 @@ unsigned int *createNumbers(unsigned int len) { array[i] = 0; } - - randomNr = rand() % (2 * len + 1); - randomPos = rand() % (len); - array[randomPos] = randomNr; - do + + for (int i = 0; i < len; i++) { - randomPos = rand() % (len); - } while (array[randomPos] != 0); - array[randomPos] = randomNr; - - while (!checkArray(array, len, 0)) - { - counter = 0; do { - if (counter == len) - { - return NULL; - } - - randomPos = rand() % (len); - counter++; - } while (array[randomPos] != 0); - - counter = 0; - do - { - if (counter == len * 2) - { - return NULL; - } - - randomNr = rand() % (2 * len + 1); - counter++; - } while (!checkArray(array, len, randomNr)); - - array[randomPos] = randomNr; + array[i] = rand() % (2 * len) + 1; + } while (!checkArray(array, i, array[i])); } + randomPos = rand() % len; + randomNr = array[randomPos]; + + filler = randomPos; + while(filler == randomPos) + { + filler = rand() % len; + } + array[filler] = randomNr; + return array; } diff --git a/stack.c b/stack.c index 2315722..5a10712 100644 --- a/stack.c +++ b/stack.c @@ -13,7 +13,7 @@ StackNode *push(StackNode *stack, void *data) StackNode *tempNode, *newNode; newNode = malloc(sizeof(StackNode)); - newNode->value = *(int *)data; + newNode->value = data; newNode->next = NULL; if (stack == NULL) diff --git a/stack.h b/stack.h index d7cc86a..0d1130c 100644 --- a/stack.h +++ b/stack.h @@ -9,7 +9,7 @@ The latest element is taken from the stack. */ //TODO: passenden Datentyp als struct anlegen typedef struct Node { - int value; + void *value; struct Node* next; } StackNode; diff --git a/test_stack.c b/test_stack.c index 2e59a8f..7e70b58 100644 --- a/test_stack.c +++ b/test_stack.c @@ -25,7 +25,7 @@ void test_push(void) TEST_ASSERT_EQUAL_INT(2, testNode->next->value); } -StackNode* setup(int value, StackNode* next) { +StackNode* setup(void *value, StackNode* next) { StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap if (node == NULL) { perror("malloc failed");