This commit is contained in:
Kristin 2025-12-05 10:08:55 +01:00
parent a002901e2f
commit 17f4155891
3 changed files with 93 additions and 29 deletions

View File

@ -29,14 +29,20 @@ program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble
$(program_obj_filesobj_files): %.o: %.c
$(program_obj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@
# --------------------------
# Unit Tests
# --------------------------
unitTests:
echo "needs to be implemented"
TEST_BIN = runTests
unitTests: stack.o test_stack.o
$(CC) $(FLAGS) -I$(unityfolder) -o $(TEST_BIN) stack.o test_stack.o $(unityfolder)/unity.c
test_stack.o: test_stack.c
$(CC) $(FLAGS) -I$(unityfolder) -c test_stack.c -o test_stack.o
# --------------------------
# Clean

87
stack.c
View File

@ -1,33 +1,82 @@
#include <stdlib.h>
#include "stack.h"
#include <stdio.h>
#include <stdlib.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. */
/*typedef struct {
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
void *data;
struct StackNode *next;
} StackNode;*/
// 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. */
// [A] -> [B] -> [C] -> NULL
// head -> stack.next
StackNode *createNode(void *data) {
StackNode *node =
malloc(sizeof(StackNode)); // Speicher reservieren, Speicherplatz für das
// struct StackNode
if (node == NULL)
return NULL; // Speicher konnte nicht reserviert werden
node->data = data; // Zeiger auf data neuer node
node->next = NULL; // nächster Zeiger ist NULL, Ende der Liste
return node; // pointer auf den neuen Knoten zurückgeben
}
// 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)
{
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) {
StackNode *newNode = createNode(data); // Speicher für neuen Knoten
// reservieren
if (newNode == NULL) { // wenn Speicher nicht reserviert werden konnte, wird
// stack unverändert zurückgegeben
return stack;
}
newNode->next = stack; // pointer verschieben
return newNode; // Zeiger auf neuen Speicherbereich zurückgeben
}
// 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);
stack = NULL;
return nextNode;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
}
void *top(StackNode *stack) { return stack != NULL ? stack->data : NULL; }
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
StackNode *clearStack(StackNode *stack) {
while (stack != NULL) {
StackNode *next = stack->next;
free(stack);
stack = next;
}
return NULL;
}

23
stack.h
View File

@ -1,25 +1,34 @@
#ifndef 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
// TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
void *data;
struct StackNode *next;
} StackNode;
StackNode *createNode(void *data);
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
// 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);
// Returns the data of the top element.
void *top(StackNode *stack);
// Clears stack and releases all memory.
void clearStack(StackNode *stack);
StackNode *clearStack(StackNode *stack);
#endif