generated from freudenreichan/info2Praktikum-DobleSpiel
Implement stack functions
This commit is contained in:
parent
981a8998eb
commit
f7fa0bf67e
43
stack.c
43
stack.c
@ -1,33 +1,60 @@
|
||||
#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.)
|
||||
// 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)
|
||||
{
|
||||
StackNode *nextNode = NULL;
|
||||
|
||||
if (stack != NULL)
|
||||
{
|
||||
nextNode = stack->next;
|
||||
free(stack);
|
||||
}
|
||||
|
||||
return nextNode;
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
void *data = NULL;
|
||||
|
||||
if (stack != NULL)
|
||||
{
|
||||
data = stack->data;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
StackNode *currentNode = stack;
|
||||
StackNode *nextNode = NULL;
|
||||
|
||||
while (currentNode != NULL)
|
||||
{
|
||||
nextNode = currentNode->next;
|
||||
free(currentNode);
|
||||
currentNode = nextNode;
|
||||
}
|
||||
}
|
||||
15
stack.h
15
stack.h
@ -7,13 +7,22 @@ The latest element is taken from the stack. */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
/*
|
||||
* One node of the stack.
|
||||
* data stores a generic pointer.
|
||||
* next points to the next element below this one.
|
||||
*/
|
||||
typedef struct StackNode
|
||||
{
|
||||
void *data;
|
||||
struct StackNode *next;
|
||||
} StackNode;
|
||||
|
||||
// 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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user