-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_aux2_funs.c
102 lines (91 loc) · 1.77 KB
/
stack_aux2_funs.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"
/**
* dlistint_len - return length of linkedlist
* @h: doubly linkedlist
* Return: number of elements in a linked dlistint_t list
*/
size_t dlistint_len(const stack_t *h)
{
size_t length = 0;
while (h)
h = h->next, length++;
return (length);
}
/**
* is_num - check if string is a number
* @token: token string to check
* Return: 1 if is number, 0 otherwise
*/
int is_num(char *token)
{
int chr = 0;
if (!token)
return (0);
while (token[chr])
{
if (isdigit(token[chr]) == 0 && token[chr] != '-')
return (0);
chr++;
}
return (1);
}
/**
* pstr - prints the string starting at the top of the stack
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void pstr(stack_t **stack, unsigned int line_number)
{
stack_t *aux = *stack;
int len = dlistint_len(*stack);
(void)line_number;
if (len <= 0)
{
printf("\n");
return;
}
while (aux->next)
aux = aux->next;
while (aux)
{
if (isascii(aux->n) == 0 || aux->n == 0)
break;
printf("%c", aux->n);
aux = aux->prev;
}
printf("\n");
}
/**
* rotl - rotate the stack to the top
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void rotl(stack_t **stack, unsigned int line_number)
{
stack_t *aux = *stack;
(void)line_number;
if (!*stack || !stack || !(*stack)->next)
return;
while (aux->next)
aux = aux->next;
add_dnodeint(stack, aux->n);
delete_dnodeint_at_end(stack);
}
/**
* rotr - rotates the stack to the bottom
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void rotr(stack_t **stack, unsigned int line_number)
{
stack_t *aux = *stack;
(void)line_number;
if (!*stack || !stack || !(*stack)->next)
return;
add_dnodeint_end(stack, (*stack)->n);
aux = aux->next;
free(*stack);
*stack = aux;
if (*stack)
(*stack)->prev = NULL;
}