-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_ops.c
93 lines (82 loc) · 1.87 KB
/
stack_ops.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
#include "monty.h"
/**
* _push - adds int to the top of the stack
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _push(stack_t **stack, __attribute__((unused))unsigned int line_number)
{
stack_t *top = malloc(sizeof(stack_t));
(void)line_number;
if (top == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
top->n = var_global.push_arg;
top->prev = NULL;
top->next = *stack;
if (*stack != NULL)
(*stack)->prev = top;
*stack = top;
}
/**
* _pall - prints all the values on the stack, starting from top
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _pall(stack_t **stack, __attribute__((unused))unsigned int line_number)
{
stack_t *temp = *stack;
while (temp != NULL)
{
printf("%d\n", temp->n);
temp = temp->next;
}
}
/**
* _pint - prints the value at the top of the stack, followed by a new line
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _pint(stack_t **stack, unsigned int line_number)
{
if (*stack == NULL)
{
fprintf(stderr, "L%d: can't pint, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
printf("%d\n", (*stack)->n);
}
/**
* _pop - removes the top element of the stack
* @stack: pointer to the top of the stack
* @line_number: line number of opcode occurs on
*/
void _pop(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
if (stack == NULL || *stack == NULL)
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
exit(EXIT_FAILURE);
}
*stack = temp->next;
if (*stack != NULL)
(*stack)->prev = NULL;
free(temp);
}
/**
* free_dlistint - frees a dlistint_t list
* @head: pointer to the top of the stack
*/
void free_dlistint(stack_t *head)
{
stack_t *temp;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}