Compare commits
19 Commits
bintree_ma
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fe070ed6a | ||
|
|
6ba60e0784 | ||
|
|
6d6febe77e | ||
|
|
386d9564a5 | ||
|
|
4b0630ecef | ||
|
|
759ac1fc23 | ||
|
|
e3bad2e8a8 | ||
|
|
e74a88bd44 | ||
|
|
f9bca135ec | ||
|
|
e4b2c4b8eb | ||
|
|
14fa468431 | ||
|
|
81fbacabdc | ||
|
|
75291daff4 | ||
|
|
104af9a567 | ||
|
|
8145c8ee6b | ||
|
|
1b022d5b2e | ||
|
|
47a158ca14 | ||
|
|
9dd8161e10 | ||
|
|
242c0e8e26 |
16
bintree.c
16
bintree.c
@ -80,31 +80,33 @@ void *nextTreeData(TreeNode *root)
|
|||||||
|
|
||||||
if (stack)
|
if (stack)
|
||||||
{
|
{
|
||||||
freeStack(stack);
|
clearStack(stack);
|
||||||
stack = NULL;
|
stack = NULL;
|
||||||
}
|
}
|
||||||
|
//leeren Stack initialisieren
|
||||||
stack = createStack();
|
stack = NULL;
|
||||||
|
|
||||||
// alle linken Knoten vom Wurzelknoten pushen
|
// alle linken Knoten vom Wurzelknoten pushen
|
||||||
currentNode = root;
|
currentNode = root;
|
||||||
while (currentNode)
|
while (currentNode)
|
||||||
{
|
{
|
||||||
push(stack, currentNode);
|
stack = push(stack, currentNode);
|
||||||
currentNode = currentNode->left;
|
currentNode = currentNode->left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Stack ist leer, keine Daten mehr
|
// Stack ist leer, keine Daten mehr
|
||||||
if (!stack || isEmpty(stack))
|
if (!stack)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
TreeNode *newNode = pop(stack); // nächster Knoten
|
TreeNode *newNode = (TreeNode *)top(stack);
|
||||||
|
// Stack-Knoten entfernen
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
// Wenn rechter Teilbaum vorhanden → alle linken Knoten pushen
|
// Wenn rechter Teilbaum vorhanden → alle linken Knoten pushen
|
||||||
currentNode = newNode->right;
|
currentNode = newNode->right;
|
||||||
while (currentNode)
|
while (currentNode)
|
||||||
{
|
{
|
||||||
push(stack, currentNode);
|
stack = push(stack, currentNode);
|
||||||
currentNode = currentNode->left;
|
currentNode = currentNode->left;
|
||||||
}
|
}
|
||||||
return newNode->data; // Daten zurückgeben
|
return newNode->data; // Daten zurückgeben
|
||||||
|
|||||||
Binary file not shown.
BIN
highscore.o
Normal file
BIN
highscore.o
Normal file
Binary file not shown.
@ -1,2 +1,10 @@
|
|||||||
|
player_name;8964
|
||||||
|
player_name;6979
|
||||||
|
player_name;5988
|
||||||
|
player_name;5987
|
||||||
|
player_name;4982
|
||||||
player1;3999
|
player1;3999
|
||||||
player_name;3993
|
player_name;3992
|
||||||
|
player_name;3989
|
||||||
|
player_name;2996
|
||||||
|
player_name;2996
|
||||||
|
|||||||
7
makefile
7
makefile
@ -35,8 +35,11 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
unitTests: numbers.o test_numbers.c bintree.o $(unityfolder)/unity.c
|
||||||
echo "needs to be implemented"
|
$(CC) $(FLAGS) -I$(unityfolder) -o runtest_numbers test_numbers.c numbers.o bintree.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
unitTestsStack: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
|
$(CC) $(FLAGS) -I$(unityfolder) $^ -o $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
|
|||||||
104
numbers.c
104
numbers.c
@ -14,13 +14,117 @@
|
|||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// 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 your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
|
|
||||||
|
/*
|
||||||
|
// ohne Binärbaum
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len <= 2)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Zufallszahlen erzeugen
|
||||||
|
srand(time(NULL));
|
||||||
|
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
||||||
|
// prüfen, ob Speicher richtig reserviert wurde
|
||||||
|
if (numbers == NULL)
|
||||||
|
{
|
||||||
|
printf("Es konnte nicht genügend Speicher reserviert werden");
|
||||||
|
free(numbers);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// einsetzen der Zahlen ins array
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
numbers[i] = rand() % ((2 * len) + 1);
|
||||||
|
// stellt sicher, dass keine Duplikate vorhanden sind
|
||||||
|
for (size_t j = 0; j < i; j++)
|
||||||
|
{
|
||||||
|
if (numbers[i] == numbers[j])
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//duplizierte Zahl hinzufügen
|
||||||
|
unsigned int dupIndex = rand() % len;
|
||||||
|
unsigned int targetIndex = rand() % len;
|
||||||
|
if (dupIndex != targetIndex)
|
||||||
|
{
|
||||||
|
numbers[targetIndex] = numbers[dupIndex];
|
||||||
|
}
|
||||||
|
return numbers;
|
||||||
|
free(numbers);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return (*(int *)a - *(int *)b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// mit Binärbaum
|
||||||
|
|
||||||
|
unsigned int *createNumbers(unsigned int len)
|
||||||
|
{
|
||||||
|
if (len <= 2)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Zufallszahlen erzeugen
|
||||||
|
srand(time(NULL));
|
||||||
|
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
||||||
|
// prüfen, ob Speicher richtig reserviert wurde
|
||||||
|
if (numbers == NULL)
|
||||||
|
{
|
||||||
|
printf("Es konnte nicht genügend Speicher reserviert werden");
|
||||||
|
free(numbers);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// fügt zufällige Zahlen in das Array ein
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
unsigned int isDup = 0;
|
||||||
|
numbers[i] = (rand() % (2 * len)) + 1;
|
||||||
|
//prüft, ob die Zahl schon vorhanden ist
|
||||||
|
root = addToTree(root, &numbers, sizeof(numbers), compare, &isDup);
|
||||||
|
if (isDup != 1)
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//duplizierte Zahl hinzufügen
|
||||||
|
unsigned int dupIndex = rand() % len;
|
||||||
|
unsigned int targetIndex = rand() % len;
|
||||||
|
//FOR-SCHLEIFE VERWENDEN!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
if (dupIndex != targetIndex)
|
||||||
|
{
|
||||||
|
numbers[targetIndex] = numbers[dupIndex];
|
||||||
|
}
|
||||||
|
return numbers;
|
||||||
|
clearTree(root);
|
||||||
|
free(numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
|
unsigned int dobble;
|
||||||
|
// neues Array zum reinkopieren initialisieren
|
||||||
|
unsigned int *nums = malloc(len * sizeof(unsigned int));
|
||||||
|
for (int l = 0; l < len; l++)
|
||||||
|
nums[l] = numbers[l];
|
||||||
|
|
||||||
|
// array sortieren
|
||||||
|
qsort(nums, len, sizeof(unsigned int), compare);
|
||||||
|
for (int k = 0; k < len; k++)
|
||||||
|
{
|
||||||
|
if (nums[k] == nums[k + 1])
|
||||||
|
{
|
||||||
|
dobble = nums[k];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dobble;
|
||||||
|
free(nums);
|
||||||
}
|
}
|
||||||
29
stack.c
29
stack.c
@ -10,24 +10,53 @@
|
|||||||
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
1
stack.h
1
stack.h
@ -12,6 +12,7 @@ typedef struct Node {
|
|||||||
void *data;
|
void *data;
|
||||||
struct Node *next;
|
struct Node *next;
|
||||||
} StackNode;
|
} 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);
|
||||||
|
|
||||||
|
|||||||
54
test_numbers.c
Normal file
54
test_numbers.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "numbers.h"
|
||||||
|
#include "bintree.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
//überprüft, dass die Funktion nicht NULL zurückgibt
|
||||||
|
void test_create_Numbers_notNull(void){
|
||||||
|
unsigned int* numbers = createNumbers(10);
|
||||||
|
TEST_ASSERT_NOT_NULL(numbers);
|
||||||
|
free(numbers);
|
||||||
|
}
|
||||||
|
// überprüft, ob die generierten Zufallszahlen innerhalb des Intervalls sind
|
||||||
|
void test_create_Numbers_randoms_inside_Value_range(void){
|
||||||
|
unsigned int len = 10;
|
||||||
|
unsigned int* numbers = createNumbers(len);
|
||||||
|
|
||||||
|
for(size_t i = 0; i < len; i++)
|
||||||
|
TEST_ASSERT_LESS_OR_EQUAL(2 * len + 1, numbers[i]);
|
||||||
|
|
||||||
|
free(numbers);
|
||||||
|
}
|
||||||
|
|
||||||
|
// überprüft, ob getDuplicate,die richtige doppelte Zahl findet
|
||||||
|
void test_get_duplicate_returns_correct_dobble(void){
|
||||||
|
unsigned int numbers [5] = {1,2,3,4,2};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int expected_result = 2;
|
||||||
|
unsigned int result = getDuplicate(numbers, len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(expected_result, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void setUp(void) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nNumbers tests\n============================\n");
|
||||||
|
RUN_TEST(test_get_duplicate_returns_correct_dobble);
|
||||||
|
RUN_TEST(test_create_Numbers_notNull);
|
||||||
|
RUN_TEST(test_create_Numbers_randoms_inside_Value_range);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
78
test_stack.c
Normal file
78
test_stack.c
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user