-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack_stack.c
66 lines (55 loc) · 1.09 KB
/
track_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
59
60
61
62
63
64
65
66
#include "binary_trees.h"
/**
* stack_create - creates a stack by repurposing binary nodes
*
* Return: pointer to the top of the stack
*/
binary_tree_t *stack_create(void)
{
binary_tree_t *top;
top = malloc(sizeof(binary_tree_t));
if (top == NULL)
exit(1);
top->n = 0;
top->parent = NULL;
top->left = NULL;
top->right = NULL;
return (top);
}
/**
* stack_pop - removes top of the stack
* @top: top of the stack
*
* Return: pointer to binary node
*/
binary_tree_t *stack_pop(binary_tree_t **top)
{
binary_tree_t *current, *old_top;
current = (*top)->parent;
old_top = *top;
*top = (*top)->right;
if (*top != NULL)
(*top)->left = NULL;
free(old_top);
return (current);
}
/**
* stack_push - reimplements binary_tree_t as a stack
* @top: top of the stack
* @current: node to push to the top
*
* Return: void
*/
void stack_push(binary_tree_t **top, binary_tree_t *current)
{
binary_tree_t *new;
new = malloc(sizeof(binary_tree_t));
if (new == NULL)
exit(1);
new->n = 0;
new->parent = current;
new->left = NULL;
new->right = *top;
(*top)->left = new;
*top = new;
}