-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
50 lines (40 loc) · 812 Bytes
/
Copy pathstack.c
File metadata and controls
50 lines (40 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdlib.h>
#include "stack.h"
stack_t *stack_init()
{
stack_t *stack = (stack_t *)malloc(sizeof(stack_t));
stack->head = NULL;
stack->size = 0;
return stack;
}
int stack_is_empty(stack_t *stack)
{
return !stack->size;
}
void *stack_peek(stack_t *stack)
{
return stack->head->data;
}
void stack_push(stack_t *stack, void *data)
{
stack_node_t *stack_node = malloc(sizeof(stack_node_t));
stack_node->data = data;
stack_node->next = stack->head;
stack->head = stack_node;
++stack->size;
}
void *stack_pop(stack_t *stack)
{
stack_node_t *stack_node = stack->head;
stack->head = stack_node->next;
--stack->size;
void *result = stack_node->data;
free(stack_node);
return result;
}
void stack_free(stack_t *stack)
{
while(!stack_is_empty(stack))
stack_pop(stack);
free(stack);
}