Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7127fe6d3f | ||
|
|
44434a8c67 | ||
|
|
5ee0f97dc6 | ||
|
|
6dd963c40e | ||
|
|
3b0a8a24ff | ||
|
|
8f4ab58000 | ||
|
|
be37b3ff21 | ||
|
|
1c1ba9e471 |
@@ -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,64 +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)
|
|
||||||
{
|
|
||||||
freeStack(stack);
|
|
||||||
stack = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stack = createStack();
|
|
||||||
|
|
||||||
// alle linken Knoten vom Wurzelknoten pushen
|
|
||||||
currentNode = root;
|
|
||||||
while (currentNode)
|
|
||||||
{
|
|
||||||
push(stack, currentNode);
|
|
||||||
currentNode = currentNode->left;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Stack ist leer, keine Daten mehr
|
|
||||||
if (!stack || isEmpty(stack))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
TreeNode *newNode = pop(stack); // nächster Knoten
|
|
||||||
|
|
||||||
// Wenn rechter Teilbaum vorhanden → alle linken Knoten pushen
|
|
||||||
currentNode = newNode->right;
|
|
||||||
while (currentNode)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
Binary file not shown.
@@ -1,2 +1 @@
|
|||||||
player1;3999
|
player1;3999
|
||||||
player_name;3993
|
|
||||||
|
|||||||
@@ -35,8 +35,10 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
unitTestsStack: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
echo "needs to be implemented"
|
$(CC) $(FLAGS) -I$(unityfolder) $^ -o $@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
|
|||||||
@@ -10,24 +10,54 @@
|
|||||||
// 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;
|
||||||
|
|
||||||
|
// Daten gehen verloren!
|
||||||
|
// Caller KANN sie nicht freigeben.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user