-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_func.c
99 lines (89 loc) · 1.97 KB
/
stack_func.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
#include "monty.h"
/**
* _push - push int to a stack
* @stack: linked lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _push(stack_t **stack, __attribute__((unused)) unsigned int line_number)
{
stack_t *top;
(void)line_number;
top = malloc(sizeof(stack_t));
if (top == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
top->n = var_global.push_arg;
top->next = *stack;
top->prev = NULL;
if (*stack != NULL)
(*stack)->prev = top;
*stack = top;
}
/**
* _pall - print all function
* @stack: pointer to linked list stack
* @line_number: number of line opcode occurs on
*/
void _pall(stack_t **stack, __attribute__((unused)) unsigned int line_number)
{
stack_t *runner;
runner = *stack;
while (runner != NULL)
{
printf("%d\n", runner->n);
runner = runner->next;
}
}
/**
* _pint - print int a top of stack
* @stack: pointer to linked list stack
* @line_number: number of line opcode occurs on
*
*/
void _pint(stack_t **stack, unsigned int line_number)
{
stack_t *runner;
runner = *stack;
if (runner == NULL)
{
fprintf(stderr, "L%d: can't pint, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
printf("%d\n", runner->n);
}
/**
* _pop - remove element a list
*@stack: pointer to first node
*@line_number: integer
*Return: void
*/
void _pop(stack_t **stack, unsigned int line_number)
{
stack_t *nodo = *stack;
if (stack == NULL || *stack == NULL)
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
exit(EXIT_FAILURE);
}
*stack = nodo->next;
if (*stack != NULL)
(*stack)->prev = NULL;
free(nodo);
}
/**
* free_dlistint - free a list
* @head: pointer to first node
*
*/
void free_dlistint(stack_t *head)
{
stack_t *tmp;
while (head != NULL)
{
tmp = head->next;
free(head);
head = tmp;
}
}