-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_functions_a.c
89 lines (82 loc) · 1.8 KB
/
op_functions_a.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
#include "monty.h"
/**
* push - Adds a new node at the beginning of a stack_t list.
* @stack: pointer to stack
* @line_number: line number of .m file
* @push_value: value to push
*/
void push(stack_t **stack, unsigned int line_number, int push_value)
{
stack_t *new_node;
(void)line_number;
if (!stack)
{
exit(EXIT_FAILURE);
}
new_node = malloc(sizeof(stack_t));
if (!new_node)
{
printf("Error: malloc failed\n");
exit(EXIT_FAILURE);
}
new_node->n = push_value;
if (*stack)
{
(*stack)->prev = new_node;
new_node->next = *stack;
new_node->prev = NULL;
}
*stack = new_node;
}
/**
* pall - Function that prints all the elements of a stack_t list.
* @stack: pointer to the head of the stack_t list
* @line_number: line number of .m file
* Return: Number of nodes
*/
void pall(stack_t **stack, unsigned int line_number)
{
stack_t *tmp = *stack;
(void)line_number;
while (tmp)
{
printf("%d\n", tmp->n);
tmp = tmp->next;
}
}
/**
* pint - Function that prints the value at the top of the stack.
* @line_number: node value
* @stack: pointer to the head of the stack_t list
*/
void pint(stack_t **stack, unsigned int line_number)
{
int value;
(void)line_number;
if (!stack || !*stack)
{
fprintf(stderr, "L%d: can't pint, stack empt\n", line_number);
exit(EXIT_FAILURE);
}
value = (*stack)->n;
printf("%d\n", value);
}
/**
* pop - deletes the node at the top of te stack
* @stack: A pointer to the head of the stack_t.
* @line_number: index of the node that should be deleted.
*/
void pop(stack_t **stack, unsigned int line_number)
{
stack_t *tmp = *stack;
(void)line_number;
if (*stack == NULL || stack == NULL)
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
free(*stack);
exit(EXIT_FAILURE);
}
tmp = (*stack)->next;
*stack = tmp;
/* free(stack); */
}