Compare commits
No commits in common. "3abe1477acb80b7d8d9e17467f119a28d140aa4a" and "04c1328230a7f4c2dde36d9f7b645cc57031feb5" have entirely different histories.
3abe1477ac
...
04c1328230
23
stack.c
23
stack.c
@ -10,43 +10,24 @@
|
|||||||
// 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 NULL;
|
|
||||||
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. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
StackNode *next = stack->next;
|
|
||||||
free(stack);
|
|
||||||
return next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack == NULL)
|
|
||||||
return NULL;
|
|
||||||
return stack->data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
StackNode *current = stack;
|
|
||||||
|
|
||||||
while (current != NULL)
|
|
||||||
{
|
|
||||||
StackNode *next = current->next;
|
|
||||||
free(current);
|
|
||||||
current = next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
10
stack.h
10
stack.h
@ -10,10 +10,12 @@ The latest element is taken from the stack. */
|
|||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
|
|
||||||
|
|
||||||
typedef struct StackNode {
|
typedef struct Node{
|
||||||
void *data;
|
int *data;
|
||||||
struct StackNode *next;
|
struct Node *next;
|
||||||
} StackNode;
|
|
||||||
|
} 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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user