-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode_fns.c
109 lines (92 loc) · 1.94 KB
/
opcode_fns.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
#include "monty.h"
#include <stdio.h>
/**
* _push - Function that pushes data to stack
*
* @stack: pointer to stack
* @line_number: line number
*/
void _push(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *new_node;
/*Create node*/
new_node = malloc(sizeof(stack_t));
/*Exit if node creation fails*/
if (new_node == NULL)
kill("Error: malloc failed");
/*Set node*/
new_node->n = gv->data;
new_node->prev = NULL;
new_node->next = NULL;
/*Set new node if head is NULL*/
if (gv->head == NULL)
gv->head = new_node;
else
{
/*Set new node on list*/
new_node->next = gv->head;
gv->head = new_node;
new_node->next->prev = new_node;
}
}
/**
* _pall - Function that prints all values on the stack
*
* @stack: pointer to stack
* @line_number: line number
*/
void _pall(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *head = gv->head;
if (head == NULL)
return;
while (head != NULL)
{
printf("%d\n", head->n);
head = head->next;
}
}
/**
* _pint - Print the value at the top of the stack
* followed by a new line
*
* @stack: pointer to stack
* @line_number: line number
*/
void _pint(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *head = gv->head;
/*Return if head is NULL*/
if (head == NULL)
{
fprintf(stderr, "L%d: can't pint, stack empty\n",
gv->line_number);
kill(NULL);
}
printf("%d\n", head->n);
}
/**
* _pop - Removes the top element of the stack
*
* @stack: pointer to stack
* @line_number: line number
*/
void _pop(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *temp = gv->head;
/*Return if head is NULL*/
if (gv->head == NULL)
{
fprintf(stderr, "L%d: can't pop an empty stack\n",
gv->line_number);
kill(NULL);
}
/*remove top node*/
gv->head = gv->head->next;
/*Free removed stack*/
free(temp);
}