-
Notifications
You must be signed in to change notification settings - Fork 0
/
medium_stack_func.c
75 lines (65 loc) · 1.4 KB
/
medium_stack_func.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
#include "monty.h"
/**
* pcharStack - Free the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void pcharStack(stack_t **stack, unsigned int line_number)
{
if (*stack == NULL)
{
fprintf(stderr, "L%u: can't pchar, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n < 0 || (*stack)->n > 127)
{
fprintf(stderr, "L%u: can't pchar, value out of range\n", line_number);
exit(EXIT_FAILURE);
}
printf("%c\n", (*stack)->n);
}
/**
* pstrStack - Prints the string starting at the top of the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void pstrStack(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
(void) line_number;
while (temp != NULL)
{
if (temp->n > 0 && temp->n < 128)
printf("%c", temp->n);
else
break;
temp = temp->next;
}
printf("\n");
}
/**
* rotlStack - Prints the string starting at the top of the stack
* @stack: Head of the list
* @line_number: Head of the list
*
* Return: nothing
*/
void rotlStack(stack_t **stack, unsigned int line_number)
{
stack_t *top = *stack, *temp = *stack;
(void) line_number;
if (*stack == NULL || (*stack)->next == NULL)
return;
if (temp->next != NULL)
temp->next->prev = NULL;
*stack = temp->next;
while (temp->next != NULL)
temp = temp->next;
temp->next = top;
top->prev = temp;
top->next = NULL;
}