generated from freudenreichan/info2Praktikum-DobleSpiel
stack angepasst, test_stack.c hinzugefuegt
This commit is contained in:
parent
8c0ff19529
commit
127c7aa8e7
16
stack.c
16
stack.c
@ -10,7 +10,11 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
if(stack && data){
|
if (!data)
|
||||||
|
{
|
||||||
|
return stack; //Nichts pushen
|
||||||
|
}
|
||||||
|
//if(stack && data){
|
||||||
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
||||||
if(!t)
|
if(!t)
|
||||||
{
|
{
|
||||||
@ -19,7 +23,7 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
t->next = stack;
|
t->next = stack;
|
||||||
t->data = data;
|
t->data = data;
|
||||||
return t; //Gibt den ersten StackNode des Stacks zurueck
|
return t; //Gibt den ersten StackNode des Stacks zurueck
|
||||||
}
|
//}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +37,10 @@ StackNode *pop(StackNode *stack)
|
|||||||
free(stack);
|
free(stack);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
@ -50,8 +58,8 @@ void clearStack(StackNode *stack)
|
|||||||
{
|
{
|
||||||
while(stack)
|
while(stack)
|
||||||
{
|
{
|
||||||
StackNode *tmp = stack; //merkt sich den momentanen obersten KNoten
|
StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
|
||||||
stack = stack->next; //setzt den obersten Knoten auf den zweiten im Stack
|
stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
|
||||||
free(tmp->data);
|
free(tmp->data);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|||||||
4
stack.h
4
stack.h
@ -8,8 +8,8 @@ The latest element is taken from the stack. */
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
typedef{
|
typedef struct StackNode {
|
||||||
int* data;
|
void* data;
|
||||||
struct StackNode *next;
|
struct StackNode *next;
|
||||||
}StackNode;
|
}StackNode;
|
||||||
|
|
||||||
|
|||||||
61
test_stack.c
Normal file
61
test_stack.c
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
//Testfunkionen zu push, pull, top & clearStack schreiben
|
||||||
|
|
||||||
|
StackNode *push(StackNode *stack, void *data)
|
||||||
|
{
|
||||||
|
if (!data)
|
||||||
|
{
|
||||||
|
return stack; //Nichts pushen
|
||||||
|
}
|
||||||
|
//if(stack && data){
|
||||||
|
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
||||||
|
if(!t)
|
||||||
|
{
|
||||||
|
return NULL; //Speicherfehler
|
||||||
|
}
|
||||||
|
t->next = stack;
|
||||||
|
t->data = data;
|
||||||
|
return t; //Gibt den ersten StackNode des Stacks zurueck
|
||||||
|
//}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
|
||||||
|
free(stack);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the data of the top element.
|
||||||
|
void *top(StackNode *stack)
|
||||||
|
{
|
||||||
|
if(stack)
|
||||||
|
{
|
||||||
|
return stack->data;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clears stack and releases all memory.
|
||||||
|
void clearStack(StackNode *stack)
|
||||||
|
{
|
||||||
|
while(stack)
|
||||||
|
{
|
||||||
|
StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
|
||||||
|
stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
|
||||||
|
free(tmp->data);
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user