-
Notifications
You must be signed in to change notification settings - Fork 0
/
_pop.c
40 lines (37 loc) · 1.21 KB
/
_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
#include "monty.h"
/**
* _pop - pops the very top
* @stack: stack given by main
* @line_cnt: line number for error messages
*
* Return: void
* - The function pop takes a pointer to the stack and
* the line count as arguments.
* - It first checks if the stack is empty or if the stack
* pointer is NULL. If either condition is true, it prints
* an error message indicating that the stack is empty and
* sets the status variable to EXIT_FAILURE .
* - If the stack is not empty, it creates a temporary pointer
* tmp and assigns the next node after the top of the stack to it.
* - It frees the memory allocated for the top of the stack using
* the free function.
* - It updates the stack pointer to point to the next node ( tmp ).
* - If the stack is not empty after the pop operation, it updates
* the prev pointer of the new top node to NULL .
*/
void _pop(stack_t **stack, unsigned int line_cnt)
{
stack_t *tmp = NULL;
if (!stack || !*stack)
{
fprintf(stderr, "L%u: can't pop an empty stack\n", line_cnt);
status = EXIT_FAILURE;
return;
}
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
if (!*stack)
return; /* prevents errors cause next line might assign a NULL */
(*stack)->prev = NULL;
}