-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpop_pint_swap_add.c
More file actions
97 lines (84 loc) · 1.76 KB
/
Copy pathpop_pint_swap_add.c
File metadata and controls
97 lines (84 loc) · 1.76 KB
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
#include "monty.h"
/**
* _pop - deletes the node
* @stack: pointer to a stack_t list
* @n: line number
*
* Return: void has no return
*/
void _pop(stack_t **stack, unsigned int n)
{
stack_t *node;
(void) n;
if (!stack || !(*stack))
{
fprintf(stderr, "L%u: can't pop an empty stack\n", n);
exit(EXIT_FAILURE);
}
node = *stack;
if ((*stack)->next)
{
*stack = (*stack)->next;
(*stack)->prev = NULL;
}
else
*stack = NULL;
free(node);
}
/**
* _pint - prints the top nodes
* @stack: pointered to a stack_t lists
* @n: line counts
*
* Return: void has no return
*/
void _pint(stack_t **stack, unsigned int n)
{
if (!stack || !(*stack))
{
fprintf(stderr, "L%u: can't pint, stack empty\n", n);
exit(EXIT_FAILURE);
}
fprintf(stdout, "%d\n", (*stack)->n);
}
/**
* _swap - swaps the position of data
* @stack: pointer to a stack_t
* @line_number: line count
*
* Return: void has no return
*/
void _swap(stack_t **stack, unsigned int line_number)
{
int store = 0;
if ((stack == NULL) || (*stack == NULL) || ((*stack)->next) == NULL)
{
fprintf(stderr, "L%u: can't swap, stack too short\n", line_number);
if (*stack)
free_stack(stack);
exit(EXIT_FAILURE);
}
(void) line_number;
store = (*stack)->next->n;
(*stack)->next->n = (*stack)->n;
(*stack)->n = store;
}
/**
* _add - adds a node to the node
* @stack: pointer to a stack_t lists
* @line_number: line count
*
* Return: void has no return
*/
void _add(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || (*stack == NULL) || ((*stack)->next == NULL))
{
fprintf(stderr, "L%u: can't add, stack too short\n", line_number);
if (*stack)
free_stack(stack);
exit(EXIT_FAILURE);
}
(*stack)->next->n = ((*stack)->next->n) + ((*stack)->n);
_pop(stack, line_number);
}