-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.c
47 lines (44 loc) · 820 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
45
46
47
#include "monty.h"
/**
* push - push element to stack
* @stack: stack or queue
* @line_number: line number
* Return: void
*/
void push(stack_t **stack, unsigned int line_number)
{
stack_t *new, *temp;
char *push_arg = strtok(NULL, "\n \t");
int pVal;
if (!is_int(push_arg))
{
fprintf(stdout, "L%u: usage: push integer\n", line_number);
exit(EXIT_FAILURE);
}
pVal = atoi(push_arg);
new = malloc(sizeof(stack_t));
if (new == NULL)
{
fprintf(stdout, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
new->n = pVal;
new->prev = NULL;
new->next = NULL;
if ((*stack) == NULL)
*stack = new;
else if (SQ)
{
(*stack)->prev = new;
new->next = *stack;
*stack = new;
}
else
{
temp = *stack;
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
new->prev = temp;
}
}