-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpstrpcharrotlrotr.c
102 lines (91 loc) · 2.32 KB
/
pstrpcharrotlrotr.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
#include "monty.h"
/**
* pchar - function that prints characters at the top of the stack
* @stack: the top of the stack containg characters to be printed
* @line: the line number of the stack with chars to be printed
*/
void pchar(stack_t **stack, unsigned int line)
{
if (stack == NULL || *stack == NULL)
{
fprintf(stderr, "L%u: can't pchar, stack empty\n", line);
exit(EXIT_FAILURE);
}
if ((*stack)->n < 0 || (*stack)->n > 127)
{
fprintf(stderr, "L%u: can't pchar, value out of range\n", line);
exit(EXIT_FAILURE);
}
printf("%c\n", (*stack)->n);
}
/**
* pstr - a function that prints strings starting at the top of the stack
* @stack: top of the stack containing strings to be printed
* @line: contains the line number of the strings to be printed
*/
void pstr(stack_t **stack, unsigned int line)
{
stack_t *temp = *stack;
int ascii;
(void)line;
if (stack == NULL || *stack == NULL)
{
printf("\n");
return;
}
while (temp != NULL)
{
ascii = temp->n;
if (ascii <= 0 || ascii > 127)
break;
printf("%c", ascii);
temp = temp->next;
}
printf("\n");
}
/**
* rotl - function that rotates the stack to the top
* @stack: the pointer to the top of the stack being rotated
* @line: the line number in the monty bytes of the stack being rotated
*/
void rotl(stack_t **stack, unsigned int line)
{
stack_t *curr;
(void)line;
if (stack == NULL || *stack == NULL || (*stack)-> next == NULL)
return;
curr = *stack;
while (curr->next != NULL)
curr = curr->next;
curr->next = *stack;
(*stack)->prev = curr;
*stack = (*stack)->next;
(*stack)->prev->next = NULL;
(*stack)->prev = NULL;
}
/**
* rotr - function that rotates a stack to the bottom
* @stack: the pointer to the top of the stack being rotated
* @line: the line number in the monty bytes of the stack being rotated
*/
void rotr(stack_t **stack, unsigned int line)
{
stack_t *tmp;
int ascii;
(void)line;
if ((*stack) != NULL)
{
tmp = *stack;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
ascii = tmp->n;
while (tmp->prev != NULL)
{
tmp->n = tmp->prev->n;
tmp = tmp->prev;
}
tmp->n = ascii;
}
}