diff --git a/bintree.c b/bintree.c index 54f423e..a843c1d 100644 --- a/bintree.c +++ b/bintree.c @@ -1,8 +1,10 @@ #include +#include #include "stack.h" #include "bintree.h" static StackNode *stack; +static TreeNode *tree = NULL; // TODO: binären Suchbaum implementieren /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), * `clearTree`: gibt den gesamten Baum frei (rekursiv), @@ -17,19 +19,29 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc { TreeNode *newNode = malloc(sizeof(TreeNode)); newNode->data = malloc(dataSize); - newNode->data = data; + memcpy(newNode->data, data, dataSize); newNode->left = NULL; newNode->right = NULL; return newNode; } - if (compareFct(root->data, data) < 0) + + int cmp = compareFct(root->data, data); + if (cmp < 0) { root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); } - else if (compareFct(root->data, data) > 0) + else if (cmp > 0) { root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); } + else + { + if (isDuplicate != NULL) + { + *isDuplicate = 1; // Mark as duplicate if needed. + } + } + return root; } @@ -38,11 +50,24 @@ 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) { + if (root != NULL) + { + clearStack(stack); + buildStack(root); + } + + if(stack != NULL) + { + + void* data = top(stack); + stack = pop(stack); + return data; + } + return NULL; } - // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { @@ -70,4 +95,17 @@ void clearTree(TreeNode *root) unsigned int treeSize(const TreeNode *root) { return root == NULL ? 0 : treeSize(root->left) + treeSize(root->right) + 1; -} \ No newline at end of file +} + +void buildStack(TreeNode *root) +{ + + if (root == NULL) + { + return; + } + + buildStack(root->left); // biggest first + stack = push(stack, root->data); // push + buildStack(root->right); +} diff --git a/bintree.h b/bintree.h index 25e16b2..f7be496 100644 --- a/bintree.h +++ b/bintree.h @@ -24,4 +24,6 @@ void clearTree(TreeNode *root); // Returns the number of entries in the tree given by root. unsigned int treeSize(const TreeNode *root); +void buildStack(TreeNode *root); + #endif \ No newline at end of file diff --git a/highscore.c b/highscore.c index fe8a458..8d3ef5c 100644 --- a/highscore.c +++ b/highscore.c @@ -79,6 +79,8 @@ int addHighscore(const char *name, double timeInSeconds, unsigned int len) { HighscoreEntry entry = createHighscoreEntry(name, calculateScore(timeInSeconds, len)); highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL); + //HighscoreEntry *temp = highscoreTree->data; + //printf("%s%d\n", temp->name, temp->score); return entry.score; } diff --git a/highscores.txt b/highscores.txt index e69de29..d1dcaac 100644 --- a/highscores.txt +++ b/highscores.txt @@ -0,0 +1,5 @@ +nick;9963 +nick;9946 +simon;4965 +alex;2996 +simon;2996 diff --git a/makefile b/makefile index 5cca804..890cc50 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,7 @@ $(program_obj_filesobj_files): %.o: %.c # -------------------------- # Unit Tests # -------------------------- -unitTests: stack.o test_stack.c $(unityfolder)/unity.c +stackTests: stack.o test_stack.c $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c # -------------------------- diff --git a/stack.c b/stack.c index 5a10712..8762fa3 100644 --- a/stack.c +++ b/stack.c @@ -10,46 +10,24 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { - StackNode *tempNode, *newNode; - - newNode = malloc(sizeof(StackNode)); - newNode->value = data; - newNode->next = NULL; - - if (stack == NULL) - { - stack = newNode; - return stack; - } - - tempNode = stack; - while (tempNode->next != NULL) - { - tempNode = tempNode->next; - } - tempNode->next = newNode; - - return stack; + StackNode *newNode = malloc(sizeof(StackNode)); + newNode->data = data; + newNode->next = stack; // Set the new node's next pointer to the current top of the stack. + return newNode; // Return the new node as the top of the stack. } // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // freed by caller.) StackNode *pop(StackNode *stack) { - StackNode *tempNode; - if (stack == NULL) { - return stack; + return NULL; // Nothing to pop if stack is empty. } - tempNode = stack; - while (tempNode->next->next != NULL) - { - tempNode = tempNode->next; - } - free(tempNode->next); - tempNode->next = NULL; + StackNode *tempNode = stack; + stack = stack->next; // Move the stack pointer to the next node. + free(tempNode); // Free the old top node. return stack; } @@ -57,35 +35,19 @@ StackNode *pop(StackNode *stack) // Returns the data of the top element. void *top(StackNode *stack) { - StackNode *tempNode; - if (stack == NULL) { - return NULL; + return NULL; // Return NULL if stack is empty. } - tempNode = stack; - while (tempNode->next != NULL) - { - tempNode = tempNode->next; - } - - return &tempNode->value; + return stack->data; // Return the value of the top node. } // Clears stack and releases all memory. void clearStack(StackNode *stack) { - StackNode *tempNode; - - if (stack == NULL) + while (stack != NULL) { - return; - } - - tempNode = stack; - while (tempNode != NULL) - { - tempNode = pop(tempNode); + stack = pop(stack); // Pop each element and free memory. } } \ No newline at end of file diff --git a/stack.h b/stack.h index 0d1130c..69ac923 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 { - void *value; + void *data; struct Node* next; } StackNode; diff --git a/stackTest.c b/stackTest.c new file mode 100644 index 0000000..971b172 --- /dev/null +++ b/stackTest.c @@ -0,0 +1,100 @@ +#include "unity.h" +#include "stack.h" // Stack-Header-Datei +#include + +// Test Setup and Teardown Functions +void setUp(void) { + // Setup code (falls notwendig, wie Initialisierungen) +} + +void tearDown(void) { + // Cleanup code (falls notwendig) +} + +// Test for push operation +void test_push(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Check if the stack is not empty + TEST_ASSERT_NOT_NULL(stack); + + // Check if the top element is correct + int *topData = top(stack); + TEST_ASSERT_EQUAL_INT(20, *topData); // The last pushed element should be on top +} + +// Test for pop operation +void test_pop(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Pop the top element + stack = pop(stack); + + // Check if the top element is now the first pushed element + int *topData = top(stack); + TEST_ASSERT_EQUAL_INT(10, *topData); // After popping, the first element should be on top + + // Pop the last element + stack = pop(stack); + + // Check if the stack is empty now + TEST_ASSERT_NULL(stack); // Stack should be NULL now +} + +// Test for top operation +void test_top(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Check the top element + int *topData = top(stack); + TEST_ASSERT_EQUAL_INT(20, *topData); // The top element should be 20 (last pushed) + + // Pop the top element and check the new top + stack = pop(stack); + topData = top(stack); + TEST_ASSERT_EQUAL_INT(10, *topData); // Now the top element should be 10 +} + +// Test for clearStack operation +void test_clearStack(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Clear the stack + clearStack(stack); + + // The stack should be empty now + TEST_ASSERT_NULL(stack); // Stack should be NULL +} + +// Run all tests +int main(void) { + UNITY_BEGIN(); + + // Run the individual test functions + RUN_TEST(test_push); + RUN_TEST(test_pop); + RUN_TEST(test_top); + RUN_TEST(test_clearStack); + + return UNITY_END(); +} diff --git a/test_stack.c b/test_stack.c index 7e70b58..c611dba 100644 --- a/test_stack.c +++ b/test_stack.c @@ -3,16 +3,33 @@ #include "stack.h" #include "unity.h" -void test_push(void) +void test_push(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Check if the stack is not empty + TEST_ASSERT_NOT_NULL(stack); + + // Check if the top element is correct + int *topData = top(stack); + TEST_ASSERT_EQUAL_INT(20, *topData); // The last pushed element should be on top +} + +void test_push1(void) { - StackNode *testNode; + StackNode *testNode = NULL; int data = 1; // Test für leeren Stack - testNode = push(NULL, &data); + testNode = push(testNode, &data); TEST_ASSERT_NOT_NULL(&testNode); TEST_ASSERT_NULL(testNode->next); - TEST_ASSERT_EQUAL_INT(1, testNode->value); + int *temp = testNode->data; + TEST_ASSERT_EQUAL_INT(1, *temp); data = 2; @@ -21,26 +38,53 @@ void test_push(void) TEST_ASSERT_NOT_NULL(&testNode); TEST_ASSERT_NOT_NULL(testNode->next); TEST_ASSERT_NULL(testNode->next->next); - TEST_ASSERT_EQUAL_INT(1, testNode->value); - TEST_ASSERT_EQUAL_INT(2, testNode->next->value); + temp = testNode->data; + TEST_ASSERT_EQUAL_INT(2, *temp); + testNode = testNode->next; + temp = testNode->data; + TEST_ASSERT_EQUAL_INT(1, *temp); } -StackNode* setup(void *value, StackNode* next) { +StackNode* setup(void *data, StackNode* next) { StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap if (node == NULL) { perror("malloc failed"); exit(EXIT_FAILURE); // or handle the error differently } - node->value = value; + node->data = data; node->next = next; return node; } +void test_pop(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; -void test_pop(void) + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Pop the top element + stack = pop(stack); + + // Check if the top element is now the first pushed element + int *topData = top(stack); + TEST_ASSERT_EQUAL_INT(10, *topData); // After popping, the first element should be on top + + // Pop the last element + stack = pop(stack); + + // Check if the stack is empty now + TEST_ASSERT_NULL(stack); // Stack should be NULL now +} +void test_pop2(void) { - StackNode* node2 = setup(3, NULL); - StackNode* node1 = setup(2, node2); - StackNode* header = setup(1, node1); + int x,y,z; + x = 1; + y = 2; + z = 3; + StackNode* node2 = setup(&z, NULL); + StackNode* node1 = setup(&y, node2); + StackNode* header = setup(&x, node1); StackNode* temp; temp = pop(header); @@ -56,21 +100,62 @@ void test_pop(void) TEST_ASSERT_NULL(node1->next); } -void test_top(void) +void test_top(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Check the top element + int *topData = top(stack); + TEST_ASSERT_EQUAL_INT(20, *topData); // The top element should be 20 (last pushed) + + // Pop the top element and check the new top + stack = pop(stack); + topData = top(stack); + TEST_ASSERT_EQUAL_INT(10, *topData); // Now the top element should be 10 +} + +void test_top2(void) { - StackNode* node2 = setup(3, NULL); - StackNode* node1 = setup(2, node2); - StackNode* header = setup(1, node1); + int x,y,z; + x = 1; + y = 2; + z = 3; + StackNode* node2 = setup(&z, NULL); + StackNode* node1 = setup(&y, node2); + StackNode* header = setup(&x, node1); int data = *(int *)top(header); - TEST_ASSERT_EQUAL_INT(node2->value, data); + TEST_ASSERT_EQUAL_INT(node2->data, data); +} + +void test_clearStack(void) { + StackNode *stack = NULL; + int data1 = 10, data2 = 20; + + // Push elements to the stack + stack = push(stack, &data1); + stack = push(stack, &data2); + + // Clear the stack + clearStack(stack); + + // The stack should be empty now + TEST_ASSERT_NULL(stack); // Stack should be NULL } void test_clear() { - StackNode* node2 = setup(3, NULL); - StackNode* node1 = setup(2, node2); - StackNode* header = setup(1, node1); + int x,y,z; + x = 1; + y = 2; + z = 3; + StackNode* node2 = setup(&z, NULL); + StackNode* node1 = setup(&y, node2); + StackNode* header = setup(&x, node1); StackNode* temp; clearStack(header); @@ -106,7 +191,7 @@ int main() RUN_TEST(test_push); RUN_TEST(test_pop); RUN_TEST(test_top); - RUN_TEST(test_clear); + RUN_TEST(test_clearStack); return UNITY_END(); } \ No newline at end of file