-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.c
executable file
·114 lines (110 loc) · 1.73 KB
/
push.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
103
104
105
106
107
108
109
110
111
112
113
114
#include "monty.h"
/**
* _isdigit - verifie
* @str: string
* Return: int
*/
int _isdigit(char *str)
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == '-' && i == 0)
{
continue;
}
if (isdigit(str[i]) == 0)
{
return (-1);
}
}
return (1);
}
/**
* push - data saving
*
* @stack: data structure
* @line_number: data
*/
void push(stack_t **stack, unsigned int line_number)
{
stack_t *new = malloc(sizeof(stack_t)), *current;
char *arg;
if (new == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
arg = strtok(NULL, " \n\t\r");
if (arg == NULL || _isdigit(arg) == -1)
{
fprintf(stderr, "L%u: usage: push integer\n", line_number);
free(new);
exit(EXIT_FAILURE);
}
new->n = atoi(arg);
new->prev = NULL;
if (*stack == NULL)
{
new->next = NULL;
*stack = new;
return;
}
current = *stack;
new->next = current;
current->prev = new;
*stack = new;
}
/**
* _push - push for queue
* @stack: begin
* @line_number: line
*/
void _push(stack_t **stack, unsigned int line_number)
{
stack_t *new = malloc(sizeof(stack_t)), *current;
char *arg;
if (new == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
arg = strtok(NULL, " \n\t\r");
if (arg == NULL || _isdigit(arg) == -1)
{
fprintf(stderr, "L%u: usage: push integer\n", line_number);
free(new);
exit(EXIT_FAILURE);
}
new->n = atoi(arg);
new->next = NULL;
if (*stack == NULL)
{
new->prev = NULL;
*stack = new;
return;
}
current = *stack;
while (current->next != NULL)
{
current = current->next;
}
current->next = new;
new->prev = current;
}
/**
* pusher - push for queue or stack
* @stack: begin
* @line_number: line
*/
void pusher(stack_t **stack, unsigned int line_number)
{
if (status == STACK)
{
push(stack, line_number);
}
else
{
_push(stack, line_number);
}
}