-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackOps2.c
67 lines (62 loc) · 1.67 KB
/
StackOps2.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
#include "monty.h"
/**
* SubNodes - Adds the top two elements of the stack.
* @stack: Pointing to a pointer pointing to top node of the stack.
* @LineNumber: line number of of the opcode.
*/
void SubNodes(stack_t **stack, unsigned int LineNumber)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
MoreError(8, LineNumber, "sub");
(*stack) = (*stack)->next;
sum = (*stack)->n - (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}
/**
* AddNodes - Adds the top two elements of the stack.
* @stack: Pointing to a pointer pointing to top node of the stack.
* @LineNumber: line number of of the opcode.
*/
void AddNodes(stack_t **stack, unsigned int LineNumber)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
MoreError(8, LineNumber, "add");
(*stack) = (*stack)->next;
sum = (*stack)->n + (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}
/**
* SwapNodes - Swaps the top two elements of the stack.
* @stack: Pointing to a pointer pointing to top node of the stack.
* @LineNumber: line number of of the opcode.
*/
void SwapNodes(stack_t **stack, unsigned int LineNumber)
{
stack_t *temp;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
MoreError(8, LineNumber, "swap");
temp = (*stack)->next;
(*stack)->next = temp->next;
if (temp->next != NULL)
temp->next->prev = *stack;
temp->next = *stack;
(*stack)->prev = temp;
temp->prev = NULL;
*stack = temp;
}
/**
* nop - No actual use.
* @stack: Pointing to a pointer pointing to top node of the stack.
* @LineNumber: line number of of the opcode.
*/
void nop(stack_t **stack, unsigned int LineNumber)
{
(void)stack;
(void)LineNumber;
}