-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
102 lines (85 loc) · 1.66 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "monty.h"
/**
* op_stack - set stack mode to last-in first-out (default)
* @pStack: stack pointer
*/
void op_stack(stack_t **pStack __attribute__((unused)))
{
op_env.mode = LIFO;
}
/**
* op_pop - remove the element at the top of the stack
* @pStack: stack pointer
*/
void op_pop(stack_t **pStack)
{
stack_t *top = NULL;
if (!*pStack)
print_err("L%u: can't pop an empty stack\n", op_env.lineno);
if (*pStack != (*pStack)->prev)
{
top = (*pStack)->prev;
(*pStack)->next->prev = top;
top->next = (*pStack)->next;
}
free(*pStack);
*pStack = top;
}
/**
* op_push - push an element onto the stack
* @pStack: stack pointer
*/
void op_push(stack_t **pStack)
{
stack_t *new = NULL;
const char *nstr = op_env.argv[1];
if (!(nstr && isinteger(nstr)))
print_err("L%u: usage: push integer\n", op_env.lineno);
new = malloc(sizeof(*new));
if (!new)
print_err("Error: malloc failed\n");
new->n = atoi(nstr);
if (*pStack)
{
new->prev = (*pStack);
new->next = (*pStack)->next;
new->next->prev = new;
(*pStack)->next = new;
if (op_env.mode == LIFO)
(*pStack) = new;
}
else
{
new->prev = new;
new->next = new;
(*pStack) = new;
}
}
/**
* free_stack - free and nullify a stack
* @pStack: pointer to a pointer to the top of a stack
*/
void free_stack(stack_t **pStack)
{
stack_t *top = NULL;
if (pStack && *pStack)
{
top = *pStack;
top->next->prev = NULL;
do {
*pStack = top->prev;
free(top);
} while ((top = *pStack));
}
}
/**
* free_env - clear stack operation environment
*/
void free_env(void)
{
free_stack(&op_env.sp);
free(op_env.argv);
free(op_env.line);
op_env.argv = NULL;
op_env.line = NULL;
}