-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpint_pop.c
45 lines (43 loc) · 938 Bytes
/
pint_pop.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
#include "monty.h"
/**
* pint - The opcode pint prints the value at the top
* of the stack, followed by a new line.
* @stack_node: pointer to stack
* @count: line count
* Return: void
*/
void pint(stack_t **stack_node, unsigned int count)
{
if (!*stack_node || !stack_node)
{
fprintf(stderr, "L%d: can't pint, stack empty\n", count);
free_stack();
exit(EXIT_FAILURE);
}
printf("%d\n", (*stack_node)->n);
}
/**
* pop - The opcode pop removes the top element of the stack.
* @stack_node: pointer to stack head
* @count: line count
* Return: void
*/
void pop(stack_t **stack_node, unsigned int count)
{
stack_t *current;
if (!*stack_node || !stack_node)
{
fprintf(stderr, "L%u: can't pop an empty stack\n", count);
free_stack();
exit(EXIT_FAILURE);
}
current = *stack_node;
if (current->next)
{
current->next->prev = NULL;
*stack_node = current->next;
}
else
*stack_node = NULL;
free(current);
}