Compare commits
4 Commits
04c1328230
...
3abe1477ac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3abe1477ac | ||
|
|
5deaf21a61 | ||
|
|
69f44a5aa0 | ||
|
|
bc6ec040b7 |
23
stack.c
23
stack.c
@ -10,24 +10,43 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
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
|
||||
// freed by caller.)
|
||||
StackNode *pop(StackNode *stack)
|
||||
{
|
||||
if (stack == NULL)
|
||||
return NULL;
|
||||
|
||||
StackNode *next = stack->next;
|
||||
free(stack);
|
||||
return next;
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
|
||||
if (stack == NULL)
|
||||
return NULL;
|
||||
return stack->data;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
StackNode *current = stack;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
StackNode *next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
8
stack.h
8
stack.h
@ -10,13 +10,11 @@ The latest element is taken from the stack. */
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
|
||||
|
||||
typedef struct Node{
|
||||
int *data;
|
||||
struct Node *next;
|
||||
|
||||
typedef struct StackNode {
|
||||
void *data;
|
||||
struct StackNode *next;
|
||||
} StackNode;
|
||||
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user