Compare commits

..

2 Commits

Author SHA1 Message Date
f77279de56 make_file eingefügt 2025-12-11 11:30:45 +01:00
87b073c60d fertige Version 2025-12-11 11:15:40 +01:00
15 changed files with 45 additions and 294 deletions

103
bintree.c
View File

@ -12,57 +12,7 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // 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 *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
if (isDuplicate)
*isDuplicate = 0;
// Wenn kein Knoten angelegt
if (root == NULL)
{
// Neuen Knoten erstellen
TreeNode *newNode = malloc(sizeof(TreeNode));
if (!newNode)
return NULL;
// mit Daten füllen
newNode->data = malloc(dataSize);
if (!(newNode->data))
{
free(newNode);
return NULL;
}
memcpy(newNode->data, data, dataSize);
newNode->left = NULL; // Kinder NULL setzen
newNode->right = NULL;
return newNode;
}
// auf Doppellungen überprüfen, daten/werte vergelichen
// int cmp = compareFct(data, root->data);
int cmp = compareFct(data, root->data);
if (cmp == 0) // Duplikat erkannt
{
if (isDuplicate) // nicht einfügen
{
*isDuplicate = 1;
return root;
}
else // einfügen erlaubt
{
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
return root;
}
}
// kein Duplikat
if (cmp < 0)
{
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
}
else if (cmp > 0)
{
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
}
return root;
} }
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction. // Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
@ -70,66 +20,17 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
static StackNode *stack = NULL; // Stack für Iterator
static TreeNode *lastRoot = NULL;
TreeNode *currentNode;
if (root != NULL) // Initialisierung bei erstem Aufruf
{
lastRoot = root;
if (stack)
{
clearStack(stack);
stack = NULL;
}
//leeren Stack initialisieren
stack = NULL;
// alle linken Knoten vom Wurzelknoten pushen
currentNode = root;
while (currentNode)
{
stack = push(stack, currentNode);
currentNode = currentNode->left;
}
}
// Stack ist leer, keine Daten mehr
if (!stack)
return NULL;
TreeNode *newNode = (TreeNode *)top(stack);
// Stack-Knoten entfernen
stack = pop(stack);
// Wenn rechter Teilbaum vorhanden → alle linken Knoten pushen
currentNode = newNode->right;
while (currentNode)
{
stack = push(stack, currentNode);
currentNode = currentNode->left;
}
return newNode->data; // Daten zurückgeben
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
if (root)
{
clearTree(root->left);
clearTree(root->right);
free(root->data);
free(root);
}
} }
// Returns the number of entries in the tree given by root. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
unsigned int size = 0;
if (root == NULL)
return 0;
size = 1 + treeSize(root->left) + treeSize(root->right);
return size;
} }

BIN
bintree.o

Binary file not shown.

BIN
doble.exe

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,10 +1 @@
player_name;8964
player_name;6979
player_name;5988
player_name;5987
player_name;4982
player1;3999 player1;3999
player_name;3992
player_name;3989
player_name;2996
player_name;2996

BIN
main.o

Binary file not shown.

View File

@ -35,12 +35,8 @@ $(program_obj_filesobj_files): %.o: %.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unitTests: numbers.o test_numbers.c bintree.o $(unityfolder)/unity.c unitTests: numbers.o test_numbers.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runtest_numbers test_numbers.c numbers.o bintree.o $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) -o runtest_numbers test_numbers.c numbers.o $(unityfolder)/unity.c
unitTestsStack: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) $^ -o $@
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------

View File

@ -15,9 +15,8 @@
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers. // creating random numbers.
/*
//ohne Binärbaum //ohne Binärbaum
unsigned int *createNumbers(unsigned int len) /*unsigned int *createNumbers(unsigned int len)
{ {
if (len <= 2) if (len <= 2)
return NULL; return NULL;
@ -26,44 +25,32 @@ unsigned int *createNumbers(unsigned int len)
srand(time(NULL)); srand(time(NULL));
unsigned int *numbers = malloc(len * sizeof(unsigned int)); unsigned int *numbers = malloc(len * sizeof(unsigned int));
//prüfen, ob Speicher richtig reserviert wurde //prüfen, ob Speicher richtig reserviert wurde
if (numbers == NULL) if(numbers == NULL){
{
printf("Es konnte nicht genügend Speicher reserviert werden"); printf("Es konnte nicht genügend Speicher reserviert werden");
free(numbers); free(numbers);
return NULL; return NULL;
} }
//einsetzen der Zahlen ins array //einsetzen der Zahlen ins array
for (size_t i = 0; i < len; i++) for (size_t i = 0; i < len; i++){
{ numbers[i] = (rand()%(2*len))+1;
numbers[i] = rand() % ((2 * len) + 1);
//stellt sicher, dass keine Duplikate vorhanden sind //stellt sicher, dass keine Duplikate vorhanden sind
for (size_t j = 0; j < i; j++) for(size_t j = 0; j < i; j++){
{ if(numbers[i] == numbers[j]){
if (numbers[i] == numbers[j])
{
i--; i--;
break; break;
} }
} }
} }
//duplizierte Zahl hinzufügen numbers[rand()% len] = (rand()% (2* len) )+ 1;
unsigned int dupIndex = rand() % len;
unsigned int targetIndex = rand() % len;
if (dupIndex != targetIndex)
{
numbers[targetIndex] = numbers[dupIndex];
}
return numbers; return numbers;
free(numbers); free(numbers);
} }*/
*/
int compare(const void *a, const void *b) int compare(const void *a, const void *b) {
{
return (*(int*)a - *(int*)b); return (*(int*)a - *(int*)b);
} }
//mit Binärbaum //mit Binärbaum
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
if (len <= 2) if (len <= 2)
@ -73,34 +60,23 @@ unsigned int *createNumbers(unsigned int len)
srand(time(NULL)); srand(time(NULL));
unsigned int *numbers = malloc(len * sizeof(unsigned int)); unsigned int *numbers = malloc(len * sizeof(unsigned int));
//prüfen, ob Speicher richtig reserviert wurde //prüfen, ob Speicher richtig reserviert wurde
if (numbers == NULL) if(numbers == NULL){
{
printf("Es konnte nicht genügend Speicher reserviert werden"); printf("Es konnte nicht genügend Speicher reserviert werden");
free(numbers); free(numbers);
return NULL; return NULL;
} }
// fügt zufällige Zahlen in das Array ein
TreeNode *root = NULL; TreeNode *root = NULL;
for (size_t i = 0; i < len; i++) for(size_t i= 0; i < len; i++){
{
unsigned int isDup = 0; unsigned int isDup = 0;
numbers[i] = (rand()%(2*len))+1; numbers[i] = (rand()%(2*len))+1;
//prüft, ob die Zahl schon vorhanden ist
root = addToTree(root, &numbers, sizeof(numbers), compare, &isDup); root = addToTree(root, &numbers, sizeof(numbers), compare, &isDup);
if (isDup != 1) if(isDup != 1){
{
i--; i--;
} }
} }
//duplizierte Zahl hinzufügen numbers[rand()% len] = (rand()% (2* len) + 1);
unsigned int dupIndex = rand() % len;
unsigned int targetIndex = rand() % len;
//FOR-SCHLEIFE VERWENDEN!!!!!!!!!!!!!!!!!!!!!!!!
if (dupIndex != targetIndex)
{
numbers[targetIndex] = numbers[dupIndex];
}
return numbers; return numbers;
clearTree(root); clearTree(root);
free(numbers); free(numbers);
@ -117,10 +93,8 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
//array sortieren //array sortieren
qsort(nums, len, sizeof(unsigned int), compare); qsort(nums, len, sizeof(unsigned int), compare);
for (int k = 0; k < len; k++) for(int k = 0; k < len; k++){
{ if(nums[k] == nums[k+1]){
if (nums[k] == nums[k + 1])
{
dobble = nums[k]; dobble = nums[k];
break; break;
} }

BIN
numbers.o

Binary file not shown.

29
stack.c
View File

@ -10,53 +10,24 @@
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
// Neues Stack-Element erstellen
StackNode *newNode = malloc(sizeof(StackNode));
if (!newNode) {
return stack; // oder NULL, je nach Fehlerstrategie
}
newNode->data = data;
newNode->next = stack; // bisheriger Stack wird nach unten geschoben
return newNode; // neuer Kopf des Stacks
} }
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
if (stack == NULL)
return NULL;
StackNode *newTop = stack->next;
free(stack);
return newTop;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
if (stack == NULL)
return NULL; // kein Element im Stack
return stack->data;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
StackNode *current = stack;
while (current != NULL)
{
StackNode *next = current->next;
free(current);
current = next;
}
} }

View File

@ -8,10 +8,6 @@ The latest element is taken from the stack. */
#include <stdlib.h> #include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef struct Node {
void *data;
struct Node *next;
} StackNode;
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data); StackNode *push(StackNode *stack, void *data);

BIN
stack.o

Binary file not shown.

View File

@ -1,78 +0,0 @@
#include <stdlib.h>
#include "stack.h"
#include "unity.h"
void test_push_and_top(void);
void test_pop(void);
void test_clearStack(void);
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_push_and_top);
RUN_TEST(test_pop);
RUN_TEST(test_clearStack);
return UNITY_END();
}
void test_push_and_top(void)
{
StackNode *stack = NULL;
int a = 10;
int b = 20;
int c = 30;
stack = push(stack, &a);
stack = push(stack, &b);
stack = push(stack, &c);
TEST_ASSERT_EQUAL_INT(30, *(int*)top(stack));
clearStack(stack);
TEST_ASSERT_NULL(stack);
}
void test_pop(void)
{
StackNode *stack = NULL;
int x = 111;
int y = 222;
stack = push(stack, &x);
stack = push(stack, &y);
// pop removes y
stack = pop(stack);
TEST_ASSERT_EQUAL_INT(111, *(int*)top(stack));
// pop removes x
stack = pop(stack);
TEST_ASSERT_NULL(stack);
}
void test_clearStack(void)
{
StackNode *stack = NULL;
int x = 5;
int y = 6;
stack = push(stack, &x);
stack = push(stack, &y);
clearStack(stack);
TEST_ASSERT_NULL(stack);
}

BIN
timer.o

Binary file not shown.