Compare commits

..

2 Commits

7 changed files with 104 additions and 17 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"stdio.h": "c",
"unity.h": "c"
}
}

View File

@ -17,11 +17,20 @@ typedef struct node
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
// 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.
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
// push the top node and push all its left nodes.
// push the right top node and push all its left nodes.
// Hilfsfunktion pushAllLeft (node) auf rechten nachfolger
// wenn neuer Baum dann pushallleft auf wurzel
// immer aufräumen!!!
// kein vorsortiertes array, sonst entarteter Baum
void *nextTreeData(TreeNode *root);
// Releases all memory resources (including data copies).
// sortierte Ausgabe
// aufrufen wie mit strtok
// welcher Baum durchlaufen
// immer null aufrufen bis fertig durchlaufen weil null zurückgegeben wird
void clearTree(TreeNode *root);
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root);
//rekursiv die Anzahl der gesetzten Knoten ermitteln
#endif

View File

@ -36,8 +36,11 @@ $(program_obj_filesobj_files): %.o: %.c
# Unit Tests
# --------------------------
test_numbers:
$(CC) -o test_numbers test__numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
$(CC) -o test_numbers test_numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
test_stack:
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
# --------------------------
# Clean
# --------------------------

54
stack.c
View File

@ -1,39 +1,65 @@
#include <stdlib.h>
#include "stack.h"
//TODO: grundlegende Stackfunktionen implementieren:
// TODO: grundlegende Stackfunktionen implementieren:
/* * `push`: legt ein Element oben auf den Stack,
* `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
* `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
static StackNode *createNewElement()
{
return malloc(sizeof(StackNode));
}
// Pushes data as pointer onto the stack.
typedef struct stackNode
{
void *data;
struct stackNode *next;
} StackNode;
StackNode *push(StackNode *stack, void *data)
{
StackNode *node = createNewElement();
if (node != NULL)
{
node->data = data;
node->next = stack;
return node;
}
return stack;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack)
{
if (stack != NULL)
{
StackNode *currentElement = stack;
stack = stack->next;
free(currentElement);
}
return stack;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if (stack != NULL)
{
return stack->data;
}
return NULL;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
StackNode *nextElement = stack;
}
while (stack != NULL)
{
nextElement = stack->next;
free(stack);
stack = nextElement;
}
}

View File

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

38
test_stack.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/unity.h"
#include "stack.h"
void test_stackOrderIsCorrect()
{
int expectedValues[] = {1, 2, 3, 4};
const unsigned int exprectedLen = sizeof(expectedValues) / sizeof(expectedValues[0]);
unsigned int observedLen = 0;
StackNode *stack = NULL;
for(int i = 0; i < exprectedLen; i++)
{
stack = push(stack, &expectedValues[i]);
}
for(int i = exprectedLen-1; i >= 0 && stack != NULL; i--)
{
TEST_ASSERT_EQUAL(expectedValues[i], *(int *)top(stack));
observedLen++;
stack = pop(stack);
}
TEST_ASSERT_EQUAL_UINT32(exprectedLen, observedLen);
clearStack(stack);
}
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_stackOrderIsCorrect);
return UNITY_END();
}