Simon May 2025-11-25 14:12:31 +01:00
commit 8b0fa4601a
2 changed files with 113 additions and 90 deletions

180
stack.c
View File

@ -1,91 +1,91 @@
#include <stdlib.h> #include <stdlib.h>
#include "stack.h" #include "stack.h"
// TODO: grundlegende Stackfunktionen implementieren: // TODO: grundlegende Stackfunktionen implementieren:
/* * `push`: legt ein Element oben auf den Stack, /* * `push`: legt ein Element oben auf den Stack,
* `pop`: entfernt das oberste Element, * `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück, * `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */ * `clearStack`: gibt den gesamten Speicher frei. */
// 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)
{ {
StackNode *tempNode, *newNode; StackNode *tempNode, *newNode;
newNode = malloc(sizeof(StackNode)); newNode = malloc(sizeof(StackNode));
newNode->value = 3; newNode->value = *(int *)data;
newNode->next = NULL; newNode->next = NULL;
if (stack == NULL) if (stack == NULL)
{ {
stack = newNode; stack = newNode;
return stack; return stack;
} }
tempNode = stack; tempNode = stack;
while (tempNode->next != NULL) while (tempNode->next != NULL)
{ {
tempNode = tempNode->next; tempNode = tempNode->next;
} }
tempNode->next = newNode; tempNode->next = newNode;
return stack; return stack;
} }
// 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)
{ {
StackNode *tempNode; StackNode *tempNode;
if (stack == NULL) if (stack == NULL)
{ {
return stack; return stack;
} }
tempNode = stack; tempNode = stack;
while (tempNode->next->next != NULL) while (tempNode->next->next != NULL)
{ {
tempNode = tempNode->next; tempNode = tempNode->next;
} }
free(tempNode->next); free(tempNode->next);
tempNode->next = NULL; tempNode->next = NULL;
return stack; return stack;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
StackNode *tempNode; StackNode *tempNode;
if (stack == NULL) if (stack == NULL)
{ {
return NULL; return NULL;
} }
tempNode = stack; tempNode = stack;
while (tempNode->next != NULL) while (tempNode->next != NULL)
{ {
tempNode = tempNode->next; tempNode = tempNode->next;
} }
return &tempNode->value; return &tempNode->value;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
StackNode *tempNode; StackNode *tempNode;
if (stack == NULL) if (stack == NULL)
{ {
return; return;
} }
tempNode = stack; tempNode = stack;
while (tempNode != NULL) while (tempNode != NULL)
{ {
tempNode = pop(tempNode); tempNode = pop(tempNode);
} }
} }

View File

@ -3,6 +3,28 @@
#include "stack.h" #include "stack.h"
#include "unity.h" #include "unity.h"
void test_push(void)
{
StackNode *testNode;
int data = 1;
// Test für leeren Stack
testNode = push(NULL, &data);
TEST_ASSERT_NOT_NULL(&testNode);
TEST_ASSERT_NULL(testNode->next);
TEST_ASSERT_EQUAL_INT(1, testNode->value);
data = 2;
// Test für nicht leeren Stack
testNode = push(testNode, &data);
TEST_ASSERT_NOT_NULL(&testNode);
TEST_ASSERT_NOT_NULL(testNode->next);
TEST_ASSERT_NULL(testNode->next->next);
TEST_ASSERT_EQUAL_INT(1, testNode->value);
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
}
StackNode* setup(int value, StackNode* next) { StackNode* setup(int value, StackNode* next) {
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
if (node == NULL) { if (node == NULL) {
@ -81,6 +103,7 @@ int main()
printf("============================\nStack tests\n============================\n"); printf("============================\nStack tests\n============================\n");
RUN_TEST(test_push);
RUN_TEST(test_pop); RUN_TEST(test_pop);
RUN_TEST(test_top); RUN_TEST(test_top);
RUN_TEST(test_clear); RUN_TEST(test_clear);