-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstack_strings.c
95 lines (79 loc) · 2 KB
/
stack_strings.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
#include "monty.h"
/**
* print_char - Prints the Ascii value.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void print_char(stack_t **stack, unsigned int line_number)
{
int ascii;
if (stack == NULL || *stack == NULL)
string_err(11, line_number);
ascii = (*stack)->n;
if (ascii < 0 || ascii > 127)
string_err(10, line_number);
printf("%c\n", ascii);
}
/**
* print_str - Prints a string.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @ln: Interger representing the line number of of the opcode.
*/
void print_str(stack_t **stack, __attribute__((unused))unsigned int ln)
{
int ascii;
stack_t *tmp;
if (stack == NULL || *stack == NULL)
{
printf("\n");
return;
}
tmp = *stack;
while (tmp != NULL)
{
ascii = tmp->n;
if (ascii <= 0 || ascii > 127)
break;
printf("%c", ascii);
tmp = tmp->next;
}
printf("\n");
}
/**
* rotl - Rotates the first node of the stack to the bottom.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @ln: Interger representing the line number of of the opcode.
*/
void rotl(stack_t **stack, __attribute__((unused))unsigned int ln)
{
stack_t *tmp;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
return;
tmp = *stack;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = *stack;
(*stack)->prev = tmp;
*stack = (*stack)->next;
(*stack)->prev->next = NULL;
(*stack)->prev = NULL;
}
/**
* rotr - Rotates the last node of the stack to the top.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @ln: Interger representing the line number of of the opcode.
*/
void rotr(stack_t **stack, __attribute__((unused))unsigned int ln)
{
stack_t *tmp;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
return;
tmp = *stack;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = *stack;
tmp->prev->next = NULL;
tmp->prev = NULL;
(*stack)->prev = tmp;
(*stack) = tmp;
}