-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_linked_operations.c
61 lines (59 loc) · 1.13 KB
/
double_linked_operations.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
#include "monty.h"
/**
* is_number - iterates each character of string to check of isdigit
* @n: integer
* Return: 0 if is number, else -1 if not
*/
int is_number(const char *n)
{
int i = 0;
if (*n == '-')
i = 1;
for (; *(n + i) != '\0'; i++)
{
if (isdigit(*(n + i)) == 0)
return (-1);
}
return (0);
}
/**
* push - adds node to the start of dlinkedlist
* @h: head of linked list (node at the bottom of stack)
* @line_number: bytecode line number
* @n: integer
*/
void push(stack_t **h, unsigned int line_number, const char *n)
{
if (!h)
return;
if (is_number(n) == -1)
{
printf("L%u: usage: push integer\n", line_number);
free_dlist(h);
exit(EXIT_FAILURE);
}
else
{
if (add_end_node(h, atoi(n)) == -1)
{
free_dlist(h);
exit(EXIT_FAILURE);
}
}
}
/**
* pop - removes node at front of dlinkedlist
* @h: head of linked list (node at the bottom of stack)
* @line_number: bytecode line number
*/
void pop(stack_t **h, unsigned int line_number)
{
if (h == NULL || *h == NULL)
{
printf("L%u: can't pop an empty stack\n", line_number);
free_dlist(h);
exit(EXIT_FAILURE);
}
else
delete_end_node(h);
}