-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop.c
31 lines (25 loc) · 894 Bytes
/
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
#include "monty.h"
/**
* pop - This function removes the topmost element of a stack
* @line_number: Bytecode file number been executed
* @stack: This is the stack of a doubly linked list
*/
void pop(stack_t **stack, unsigned int line_number)
{
stack_t *top; /**Initailizes top as an element of a stack**/
if (*stack == NULL) /**Test if their is no element in the stack**/
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
free_resources();
exit(EXIT_FAILURE);
}
top = *stack; /** Initailizes topmost element of the stack to var top**/
if (top->next != NULL) /**Test if the next element is not NULL**/
{
*stack = top->next;/**Move the stack to the next node**/
(*stack)->prev = NULL;/**Update the prev stack**/
}
else
*stack = NULL; /** Stack is NULL if it is the last element**/
free(top); /** Free the memory allocated for topmost element**/
}