-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.c
executable file
·44 lines (43 loc) · 862 Bytes
/
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
#include "monty.h"
#include <string.h>
/**
*push - add a neode in the list
*@stack: pointer to a pointer to the doubly linked list
*@line_number: line where there is an error
*Return: nothing
*/
void push(stack_t **stack, char *n, unsigned int line_number)
{
stack_t *new = NULL;
int i;
new = malloc(sizeof(stack_t));
if (new == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
if (n == NULL)
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
exit(EXIT_FAILURE);
}
for (i = 0; n[i]; i++)
{
if (n[0] == '-' && i == 0)
continue;
if (n[i] < 48 || n[i] > 57)
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
exit(EXIT_FAILURE);
}
}
new->n = atoi(n);
new->prev = NULL;
new->next = NULL;
if (*stack != NULL)
{
new->next = *stack;
(*stack)->prev = new;
}
*stack = new;
}