Compare commits

..
7 Commits
7 changed files with 142 additions and 69 deletions

No files matched your search

+3 -65
View File
@@ -12,36 +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(!root)
{
root = malloc(sizeof(node));
*node newNode;
*newNode.data = malloc(dataSize);
strcpy(*newNode.data, data);
*newNode.left = NULL;
*newNode.right = NULL;
if(isDuplicate)
*isDuplicate = 0;
return newNode;
}
if(compareFct(*data, *root->data) > 0)
{
*right = addToTree(right, data, dataSize, compareFct, isDuplicate);
}
else if(compareFct(*data, *root->data) < 0)
{
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
}
else
{
if(!isDuplicate)
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
else
{
*isDuplicate = 1;
}
}
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.
@@ -49,50 +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)
{ {
StackNode *stackRoot = NULL;//Generates a new, empty stack
stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree
stackNode * tempBuffer = pop(stackRoot);
clearStack(stackRoot);
return tempBuffer;
}
//Function to aid nextTreeData
StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
{
if(!root)
{
return stackRoot;
}
stackRoot = iterateThroughTree(root->left, stackRoot);
stackNode = push(stackNode, root->data);
stackRoot = iterateThroughTree(root->right, stackRoot);
return stackRoot;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) void clearTree(TreeNode *root)
{ {
if(left)
clearTree(left);
if(right)
clearTree(right);
free(data);
data = NULL;
free(root);
root = NULL;
} }
// 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)
{ {
if(root)
{
unsigned int tempSize = 0;
if(left)
tempSize += treeSize(left);
if(right)
tempSize += treeSize(right);
return tempSize + 1;
}
return 0;
} }
+1 -2
View File
@@ -23,6 +23,5 @@ void *nextTreeData(TreeNode *root);
void clearTree(TreeNode *root); void clearTree(TreeNode *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);
//Function to aid nextTreeData
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot);
#endif #endif
+1
View File
@@ -1 +1,2 @@
Lukas;4985
player1;3999 player1;3999
+3 -1
View File
@@ -35,7 +35,9 @@ $(program_obj_filesobj_files): %.o: %.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unitTests: stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
echo "needs to be implemented" echo "needs to be implemented"
# -------------------------- # --------------------------
+32 -1
View File
@@ -10,6 +10,22 @@
// 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)
{ {
if(!(stack)) { // check if stack is empty
stack = malloc(sizeof(StackNode));
if(!stack)
return NULL;
stack->stackData = data;
stack->below = NULL;
return stack;
}
StackNode* newStack = malloc(sizeof(StackNode));
if(!newStack)
return stack;
newStack->below = stack;
newStack->stackData = data;
stack = newStack;
return stack;
} }
@@ -17,17 +33,32 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
if(stack) {
StackNode* temp = stack;
stack = stack->below;
temp->stackData = NULL;
free(temp);
temp = NULL;
return stack;
} else {
return NULL;
}
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
if(stack)
return stack->stackData;
else
return NULL;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
while(stack)
stack = pop(stack);
} }
+5
View File
@@ -9,6 +9,11 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
struct StackNode* below;
void* stackData;
} 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);
+97
View File
@@ -0,0 +1,97 @@
#include "stack.h"
#include "unity.h"
void test_firstNodeAddedCorrectly(void) {
printf("\nStarting first test...\n");
StackNode* modelStack = malloc(sizeof(StackNode));
int modelData = 5;
modelStack->below = NULL;
modelStack->stackData = &modelData;
StackNode* testStack = NULL;
int testData = 5;
testStack = push(testStack, &testData);
TEST_ASSERT_EQUAL_INT(*(int*)modelStack->stackData, *(int*)testStack->stackData);
TEST_ASSERT_NULL(testStack->below);
clearStack(modelStack);
clearStack(testStack);
}
void test_topReturnsCorrectValues(void) {
printf("Starting second test...\n");
StackNode* testStack = NULL;
int data1 = 1;
testStack = push(testStack, &data1);
int* returnData1 = (int*) top(testStack);
int data2 = 0;
testStack = push(testStack, &data2);
int* returnData2 = (int*) top(testStack);
int data3 = 3;
testStack = push(testStack, &data3);
int* returnData3 = (int*) top(testStack);
size_t counter = 0;
if(*returnData1 == 1)
counter++;
if(*returnData2 == 0)
counter++;
if(*returnData3 == 3)
counter++;
TEST_ASSERT_EQUAL_INT(3, counter);
clearStack(testStack);
}
void test_popRemovesCorrectly(void) {
printf("Starting third test...\n");
StackNode* testStack = NULL;
int data1 = 1;
testStack = push(testStack, &data1);
int data2 = 0;
testStack = push(testStack, &data2);
int data3 = 3;
testStack = push(testStack, &data3);
int data4 = 9;
testStack = push(testStack, &data4);
int* returnData1 = (int*) top(testStack);
testStack = pop(testStack);
int* returnData2 = (int*) top(testStack);
testStack = pop(testStack);
int* returnData3 = (int*) top(testStack);
testStack = pop(testStack);
int* returnData4 = (int*) top(testStack);
testStack = pop(testStack);
size_t counter = 0;
if(*returnData1 == 9)
counter++;
if(*returnData2 == 3)
counter++;
if(*returnData3 == 0)
counter++;
if(*returnData4 == 1)
counter++;
TEST_ASSERT_EQUAL_INT(4, counter);
clearStack(testStack);
}
void setUp(void) {
}
void tearDown(void) {
}
int main(void) {
UNITY_BEGIN();
printf("\n----------------------------Stack-Tests----------------------------\n");
RUN_TEST(test_firstNodeAddedCorrectly);
RUN_TEST(test_topReturnsCorrectValues);
RUN_TEST(test_popRemovesCorrectly);
return UNITY_END();
}