-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_func2.c
97 lines (80 loc) · 2.38 KB
/
stack_func2.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
#include "monty.h"
/**
* nop - Does nothing.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void nop(stack_t **stack, unsigned int line_number)
{
(void)stack;
(void)line_number;
}
/**
* swap_nodes - Swaps the top two elements of the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void swap_nodes(stack_t **stack, unsigned int line_number)
{
stack_t *tmp;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
more_err(8, line_number, "swap");
tmp = (*stack)->next;
(*stack)->next = tmp->next;
if (tmp->next != NULL)
tmp->next->prev = *stack;
tmp->next = *stack;
(*stack)->prev = tmp;
tmp->prev = NULL;
*stack = tmp;
}
/**
* add_nodes - Adds the top two elements of the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void add_nodes(stack_t **stack, unsigned int line_number)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
more_err(8, line_number, "add");
(*stack) = (*stack)->next;
sum = (*stack)->n + (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}
/**
* sub_nodes - Adds the top two elements of the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void sub_nodes(stack_t **stack, unsigned int line_number)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
more_err(8, line_number, "sub");
(*stack) = (*stack)->next;
sum = (*stack)->n - (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}
/**
* div_nodes - Adds the top two elements of the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void div_nodes(stack_t **stack, unsigned int line_number)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
more_err(8, line_number, "div");
if ((*stack)->n == 0)
more_err(9, line_number);
(*stack) = (*stack)->next;
sum = (*stack)->n / (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}