-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_code_functions.c
56 lines (48 loc) · 1005 Bytes
/
op_code_functions.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
#include "monty.h"
/**
* push - Pushes op_arg unto the stack.
*
* @head: Pointer to List beginning.
* @line_number: Line of code in Monty bytecode.
*
* Return: 0 if successful, 1 otherwise.
*/
void push(stack_t **head, u_int line_number)
{
stack_t *node, *current = *head;
(void) line_number;
node = malloc(sizeof(stack_t));
if (!node)
{
dprintf(2, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
node->n = op_arg;
node->next = NULL;
if (!current)
{
node->prev = NULL;
*head = node;
return;
}
while (current->next)
current = current->next;
current->next = node;
node->prev = current;
}
/**
* pall - Prints all the values on the stack form the top
*
* @head: Pointer to bottom value in stack.
* @line_number: Line of code in Monty bytecode.
*
* Return: 0 if successful, 1 otherwise.
*/
void pall(stack_t **head, u_int line_number)
{
if (!head || !(*head))
return;
if ((*head)->next)
pall(&((*head)->next), line_number);
printf("%d\n", (*head)->n);
}