-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode_2.c
104 lines (97 loc) · 2.17 KB
/
opcode_2.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
100
101
102
103
104
#include "monty.h"
/**
* op_add - add two elements on the top of the stack
* @stack: pointer to the head of the stack
* @line_number: number of the current line
*
* Return: the number of nodes
*/
void op_add(stack_t **stack, unsigned int line_number)
{
size_t n = 0;
stack_t *temp = *stack;
line_number = line_number;
n = dlistint_len(var.stack_head);
if (n < 2)
{
handle_dlist_head(var.stack_head);
free(var.getl_info);
fclose(var.fp_struct);
fprintf(stderr, "L%u: can't add, stack too short\n", var.n_lines);
exit(EXIT_FAILURE);
}
if (*stack != NULL)
{
*stack = (*stack)->next;
(*stack)->n = (*stack)->n + (*stack)->prev->n;
(*stack)->prev = NULL;
free(temp);
}
}
/**
* op_nop - do nothing
* @stack: pointer to the head of the stack
* @line_number: number of the current line
*
* Return: the number of nodes
*/
void op_nop(stack_t **stack, unsigned int line_number)
{
stack = stack;
line_number = line_number;
}
/**
* op_pop - delete the element on the top of the stack
* @stack: pointer to the head of the stack
* @line_number: number of the current line
*
* Return: the number of nodes
*/
void op_pop(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
line_number = line_number;
if (temp == NULL)
{
handle_dlist_head(var.stack_head);
free(var.getl_info);
fclose(var.fp_struct);
fprintf(stderr, "L%u: can't pop an empty stack\n", var.n_lines);
exit(EXIT_FAILURE);
}
*stack = (*stack)->next;
if (*stack)
{
(*stack)->prev = NULL;
}
free(temp);
}
/**
* op_sub - sub two elements on the top of the stack
* @stack: pointer to the head of the stack
* @line_number: number of the current line
*
* Return: the number of nodes
*/
void op_sub(stack_t **stack, unsigned int line_number)
{
size_t n = 0;
stack_t *temp = *stack;
line_number = line_number;
n = dlistint_len(var.stack_head);
if (n < 2)
{
handle_dlist_head(var.stack_head);
free(var.getl_info);
fclose(var.fp_struct);
fprintf(stderr, "L%u: can't sub, stack too short\n", var.n_lines);
exit(EXIT_FAILURE);
}
if (*stack != NULL)
{
*stack = (*stack)->next;
(*stack)->n = (*stack)->n - (*stack)->prev->n;
(*stack)->prev = NULL;
free(temp);
}
}