-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpush.c
More file actions
50 lines (48 loc) · 909 Bytes
/
Copy pathpush.c
File metadata and controls
50 lines (48 loc) · 909 Bytes
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
#include "monty.h"
/**
* _push - pushes an element to the stack
* @stack: head
* @num_line: number line
*/
void _push(stack_t **stack, unsigned int num_line)
{
stack_t *temp;
_verify2(stack, num_line);
if (global.token)
{
temp = malloc(sizeof(stack_t));
if (temp == NULL)
{
fputs("Error: malloc failed\n", stderr);
exit(EXIT_FAILURE);
}
temp->n = global.num, temp->next = NULL;
temp->prev = NULL;
if (*stack)
{
if (global.flag == 1)
{
temp->next = *stack;
(*stack)->prev = temp;
*stack = temp;
}
else
{
while ((*stack)->next)
*stack = (*stack)->next;
(*stack)->next = temp, temp->prev = *stack;
while ((*stack)->prev)
*stack = (*stack)->prev;
}
}
else
*stack = temp;
}
else
{
free(global.line), fclose(global.fil);
dprintf(2, "L%u: usage: push integer\n", num_line);
free_l(stack);
exit(EXIT_FAILURE);
}
}