-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_ops.c
82 lines (73 loc) · 1.53 KB
/
stack_ops.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
#include "monty.h"
/**
* Push - pushes to stack;
*
* @stack: stack
* @number: line_number
*/
void Push(stack_t **stack, unsigned int number)
{
int value;
if (Input.Bytecodes[1] == NULL || isNum(Input.Bytecodes[1]) == FALSE)
ErrExit(*stack, "L%d: usage: push integer\n", number);
value = atoi(Input.Bytecodes[1]);
switch (Input.Mode)
{
case STACK_MODE: /* Operate in stack mode */
if (fpush(stack, value) == NULL)
ErrExit(*stack, "Error: malloc failed\n");
break;
case QUEUE_MODE: /* Operate in queue mode */
if (enqueue(stack, value) == NULL)
ErrExit(*stack, "Error: malloc failed\n");
break;
}
}
/**
* Pop - pops from stack.
*
* @stack: a stack.
* @number: line number.
*/
void Pop(stack_t **stack, unsigned int number)
{
if (isEmpty(*stack))
ErrExit(*stack, "L%d: can't pop an empty stack\n", number);
fpop(stack);
}
/**
* Rotr - rotates the stack to the bottom
*
* @stack: a stack.
* @number: line number.
*/
void Rotr(stack_t **stack, unsigned int number)
{
UNUSED(number);
if (stackLen(*stack) > 1)
fpush(stack, lpop(stack));
}
/**
* Rotl - rotates the stack to the top
*
* @stack: a stack.
* @number: line number.
*/
void Rotl(stack_t **stack, unsigned int number)
{
UNUSED(number);
if (!isEmpty(*stack))
enqueue(stack, fpop(stack));
}
/**
* Swap - swaps top two element.
*
* @stack: a stack.
* @number: line number.
*/
void Swap(stack_t **stack, unsigned int number)
{
if (stackLen(*stack) < 2)
ErrExit(*stack, "L%d: can't swap, stack too short\n", number);
fswap(stack);
}