Compare commits

...
1 Commits
Author SHA1 Message Date
schrempfde103950 89067a7ba6 Mein Teil (Sollte Fertig sein) 2026-11-23 21:41:30 +01:00
4 changed files with 100 additions and 14 deletions

No files matched your search

+2
View File
@@ -34,6 +34,8 @@ $(program_obj_files): %.o: %.c
# --------------------------
# Unit Tests
test_stack: test_stack.c stack.o stack.h
$(CC) $(FLAGS) -I$(unityfolder) test_stack.c stack.o $(unityfolder)/unity.c -o test_stack.exe
# --------------------------
unitTests:
echo "needs to be implemented"
+22 -9
View File
@@ -1,33 +1,46 @@
#include <stdlib.h>
#include "stack.h"
//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. */
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
if (newNode == NULL) {
return stack;
}
newNode->data = data;
newNode->next = stack;
return newNode;
}
// 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) {
return NULL;
}
StackNode *nextNode = stack->next;
free(stack);
return nextNode;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if (stack == NULL) {
return NULL;
}
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while (stack != NULL) {
StackNode *nextNode = stack->next;
free(stack);
stack = nextNode;
}
}
+8 -5
View File
@@ -1,13 +1,16 @@
#ifndef STACK_H
#define STACK_H
#define STACK_H
/* A stack is a special type of queue which uses the LIFO (last in, first out) principle.
This means that with each new element all other elements are pushed deeper into the stack.
The latest element is taken from the stack. */
/* A stack is a special type of queue which uses the LIFO (Last In, First Out) principle.
This means that with each new element all other elements are pushed deeper into the stack.
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);
+68
View File
@@ -0,0 +1,68 @@
#include <assert.h>
#include <stdio.h>
#include "stack.h"
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_empty_stack(void)
{
StackNode *stack = NULL;
assert(top(stack) == NULL);
assert(pop(stack) == NULL);
clearStack(stack);
}
static void test_push_top_pop_int(void)
{
StackNode *stack = NULL;
int a = 10;
int b = 20;
int c = 30;
stack = push(stack, &a);
assert(top(stack) == &a);
assert(*(int *)top(stack) == 10);
stack = push(stack, &b);
stack = push(stack, &c);
assert(*(int *)top(stack) == 30);
stack = pop(stack);
assert(*(int *)top(stack) == 20);
stack = pop(stack);
assert(*(int *)top(stack) == 10);
stack = pop(stack);
assert(stack == NULL);
assert(top(stack) == NULL);
}
static void test_clear_stack(void)
{
StackNode *stack = NULL;
int values[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
stack = push(stack, &values[i]);
}
assert(*(int *)top(stack) == 5);
clearStack(stack);
}
int main(void)
{
test_empty_stack();
test_push_top_pop_int();
test_clear_stack();
printf("Alle Stack-Tests erfolgreich bestanden!\n");
return 0;
}