-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupp_func3.c
103 lines (90 loc) · 1.86 KB
/
supp_func3.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
#include "monty.h"
/**
* pchar- prints the char at the top of
* the stack, followed by a new line
* @stack: points to head of stack
* @linenum: current line no
*/
void pchar(stack_t **stack, __attribute__((unused))unsigned int linenum)
{
if (!(*stack))
{
fprintf(stderr, "L%u: can't pchar, stack empty\n"
, monty.line_number);
free_all();
exit(EXIT_FAILURE);
}
if ((*stack)->n < 0 || (*stack)->n > 127)
{
fprintf(stderr, "L%u: can't pchar, value out of range\n",
monty.line_number);
free_all();
exit(EXIT_FAILURE);
}
printf("%c\n", (*stack)->n);
}
/**
* pstr- prints the string starting
* at the top of the stack, followed by a new line.
* @stack: head of stack
* @linenum: current line num of monty
*/
void pstr(stack_t **stack, __attribute__((unused))unsigned int linenum)
{
stack_t *location = *stack;
while (location && location->n > 0 && location->n <= 127)
{
putchar(location->n);
location = location->next;
}
putchar('\n');
}
/**
* rotl -rotates the stack to the top
* @stack: head of stack
* @linenum: current ln
*/
void rotl(stack_t **stack, __attribute__((unused))unsigned int linenum)
{
stack_t *first, *second;
if (!*stack || !(*stack)->next)
{
return;
}
first = *stack;
second = (*stack)->next;
*stack = second;
second->prev = NULL;
while (second->next)
{
second = second->next;
}
second->next = first;
first->next = NULL;
first->prev = second;
}
/**
* rotr - otates the stack to the bottom.
* @stack: head of stack
* @linenum: current ln
*
*/
void rotr(stack_t **stack, __attribute__((unused))unsigned int linenum)
{
stack_t *first, *second;
if (!(*stack) || !(*stack)->next)
{
return;
}
first = *stack;
while (first->next->next)
{
first = first->next;
}
second = first->next;
first->next = NULL;
(*stack)->prev = second;
second->next = *stack;
second->prev = NULL;
*stack = second;
}