-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
58 lines (45 loc) · 902 Bytes
/
stack.c
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
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"
Node *push(Node *node, int data)
{
Node *new_node = NULL;
new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = node;
return new_node;
}
int pop(Node **node)
{
if (is_empty(*node)) {
return false;
}
Node *auxiliar = *node;
int data = auxiliar->data;
*node = (*node)->next;
free(auxiliar);
return data;
}
bool is_empty(Node *node)
{
if (node != NULL) {
return false;
}
return true;
}
void free_stack(Node **stack)
{
if (is_empty(*stack)) {
return;
}
Node *walker = *stack, *auxiliar = NULL;
while (walker != NULL) {
auxiliar = walker;
walker = walker->next;
auxiliar->data = 0;
auxiliar->next = NULL;
free(auxiliar);
}
*stack = NULL;
}