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 <stdlib.h>
|
||||||
#include "stack.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.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data)
|
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
|
// Deletes the top element of the stack (latest added element) and releases its memory.
|
||||||
// freed by caller.)
|
// Pointer to data has to be freed by caller.
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
StackNode *nextNode = NULL;
|
||||||
|
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
nextNode = stack->next;
|
||||||
|
free(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nextNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
void *data = NULL;
|
||||||
|
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
data = stack->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
StackNode *currentNode = stack;
|
||||||
|
StackNode *nextNode = NULL;
|
||||||
|
|
||||||
|
while (currentNode != NULL)
|
||||||
|
{
|
||||||
|
nextNode = currentNode->next;
|
||||||
|
free(currentNode);
|
||||||
|
currentNode = nextNode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
21
stack.h
21
stack.h
@ -1,19 +1,28 @@
|
|||||||
#ifndef STACK_H
|
#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.
|
/* 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.
|
This means that with each new element all other elements are pushed deeper into the stack.
|
||||||
The latest element is taken from the stack. */
|
The latest element is taken from the stack. */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#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.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
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
|
// Deletes the top element of the stack (latest added element) and releases its memory.
|
||||||
// freed by caller.)
|
// Pointer to data has to be freed by caller.
|
||||||
StackNode *pop(StackNode *stack);
|
StackNode *pop(StackNode *stack);
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
@ -22,4 +31,4 @@ void *top(StackNode *stack);
|
|||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack);
|
void clearStack(StackNode *stack);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user