Compare commits

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

No files matched your search

+65 -3
View File
@@ -12,7 +12,36 @@
// 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)
{
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.
@@ -20,17 +49,50 @@ 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)
{
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).
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.
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;
}
+2 -1
View File
@@ -23,5 +23,6 @@ void *nextTreeData(TreeNode *root);
void clearTree(TreeNode *root);
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root);
//Function to aid nextTreeData
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot);
#endif
-1
View File
@@ -1,2 +1 @@
Lukas;4985
player1;3999
+1 -3
View File
@@ -35,9 +35,7 @@ $(program_obj_filesobj_files): %.o: %.c
# --------------------------
# Unit Tests
# --------------------------
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
unitTests:
echo "needs to be implemented"
# --------------------------
+1 -32
View File
@@ -10,22 +10,6 @@
// Pushes data as pointer onto the stack.
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;
}
@@ -33,32 +17,17 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.)
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.
void *top(StackNode *stack)
{
if(stack)
return stack->stackData;
else
return NULL;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while(stack)
stack = pop(stack);
}
-5
View File
@@ -9,11 +9,6 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
struct StackNode* below;
void* stackData;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);
-97
View File
@@ -1,97 +0,0 @@
#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();
}