-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_ops_III.c
84 lines (68 loc) · 1.47 KB
/
stack_ops_III.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
#include "monty.h"
/**
* _pstr - prints ASCII value of elements in stack until it encounters 0
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _pstr(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
int asc;
(void)line_number;
while (temp)
{
asc = temp->n;
if (asc == 0 || _isalpha(asc) == 0)
break;
putchar(asc);
temp = temp->next;
}
putchar('\n');
}
/**
* _rotl - rotates the stack to the top
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _rotl(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
int first;
if (!line_number || !stack || !*stack || !(*stack)->next)
return;
first = temp->n;
while (temp->next)
{
/**
* 1. Set the current node's value to the next node's value
* 2. Move to the next node
* Another way :
* temp = temp->next;
* temp->prev->n = temp->n;
*/
temp->n = temp->next->n;
temp = temp->next;
}
temp->n = first;
}
/**
* _rotr - rotates the stack to the bottom
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _rotr(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
int last;
if (!line_number || !stack || !*stack || !(*stack)->next)
return;
while (temp->next)
temp = temp->next;
last = temp->n;
while (temp->prev)
{
temp->n = temp->prev->n;
temp = temp->prev;
}
temp->n = last;
}