Compare commits

...

1 Commits
main ... Lena

Author SHA1 Message Date
9afecd8502 wip 2025-12-07 12:28:46 +01:00
3 changed files with 47 additions and 2 deletions

22
stack.c
View File

@ -8,9 +8,29 @@
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack.
static StackNode *createEmptyElement()
{
return malloc(sizeof(StackNode));
}
StackNode *push(StackNode *stack, void *data)
{
StackNode *newElement = createEmptyElement();
newElement->data = data;
newElement->next = NULL;
if (stack == NULL)
{
stack = newElement;
}
else
{
StackNode *currentElement = stack;
while(currentElement->next != NULL)
{
currentElement = currentElement->next;
}
currentElement->next = newElement;
return stack;
}
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be

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 node
{
void *data;
struct node* next;
}StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);
@ -22,4 +27,4 @@ void *top(StackNode *stack);
// Clears stack and releases all memory.
void clearStack(StackNode *stack);
#endif
#endif

20
test_stack.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
void testPushAddsElementCorrectly(void)
{
int a = 10;
StackNode *node = push(NULL, &a);
TEST_ASSERT_NOT_NULL(node);
TEST_ASSERT_EQUAL_INT(a, *((int*)node->data)); //void Zeiger in integer-Zeiger umwandeln
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(testPushAddsElementCorrectly);
return UNITY_END();
}