Implement stack, basic test program & makefile entry for the test

This commit is contained in:
Timo Hertel 2025-11-23 16:07:25 +01:00
parent cb034be9f9
commit 2c10463852
4 changed files with 122 additions and 8 deletions

View File

@ -30,7 +30,13 @@ doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble $(CC) $(FLAGS) $^ -o doble
$(program_obj_filesobj_files): %.o: %.c $(program_obj_filesobj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@ $(CC) -c $(FLAGS) -g3 -fPIC $^ -o $@
# --------------------------
# Stack Tests
# --------------------------
stackTests : stack.o test_stack.c
$(CC) $(FLAGS) -g3 $^ -o stackTest
# -------------------------- # --------------------------
# Unit Tests # Unit Tests

View File

@ -10,24 +10,47 @@
// 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)
{ {
// ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben; Ist das sinnvoll? kein Anzeichen, kein Rückgabewert...
if (data != NULL)
{
StackNode *newNode;
newNode = (StackNode *)malloc(sizeof(StackNode));
newNode->value = data;
// ,-→ bedeutet Liste war leer - das neue Element hat keinen Nachfolger (next = NULL)
if (stack == NULL) {
newNode->next = NULL;
} else {
newNode->next = stack;
}
stack = newNode;
}
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)
{ {
if(stack != NULL)
{
StackNode *nextNode = stack->next;
free(stack);
stack = nextNode;
}
return stack;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
return stack->value;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
while (stack != NULL) {
stack = pop(stack);
}
} }

View File

@ -7,7 +7,12 @@ The latest element is taken from the stack. */
#include <stdlib.h> #include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen //DONE: passenden Datentyp als struct anlegen
typedef struct Node
{
void *value;
struct Node* next;
} StackNode;
// 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);

80
I2_Dobble/test_stack.c Normal file
View File

@ -0,0 +1,80 @@
#include <bits/types/stack_t.h>
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void inspectStack(StackNode *stack)
{
if (stack != NULL)
{
printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->value);
while (stack->next != NULL)
{
printf(" %d", *(int*)stack->next->value);
stack = stack->next;
}
putchar('\n');
}
else
{
printf("Der Stack ist leer\n");
}
}
int main()
{
StackNode *stack;
stack = NULL; // initialisierung mit NULL -> leere Liste
printf("...ein Element wird eingefügt...\n");
int toBeRemoved = 42;
stack = push(stack, &toBeRemoved);
inspectStack(stack);
printf("...das Element wird wieder entfernt...\n");
stack = pop(stack);
inspectStack(stack);
printf("...pop auf leeren Stack...\n");
stack = pop(stack);
inspectStack(stack);
putchar('\n');
int data[5] = {1, 2, 3, 4, 5};
// alle 5 werte der reihe nach auf den Satck legen - 1 unten ... 5 oben
for (int i = 0; i < 5; i++)
{
stack = push(stack, &data[i]);
}
//alle Elemente mit Test-Funktion ausgeben
inspectStack(stack);
// Elemente stück für Stück ausgeben und entfernen
printf("1. Element war: %d\n", *(int*)top(stack));
stack = pop(stack);
printf("2. Element war: %d\n", *(int*)top(stack));
stack = pop(stack);
printf("3. Element war: %d\n", *(int*)top(stack));
stack = pop(stack);
printf("4. Element war: %d\n", *(int*)top(stack));
stack = pop(stack);
printf("5. Element war: %d\n", *(int*)top(stack));
}
/*
int main()
{
UNITY_BEGIN();
printf("============================\n Stack tests\n============================\n");
RUN_TEST(test_createNodeFailsOnZeroData);
return UNITY_END();
}
*/