-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_functions.c
146 lines (125 loc) · 3.03 KB
/
stack_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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "monty.h"
void push_it(stack_t **stack, unsigned int tracker);
void pall_it(stack_t **stack, unsigned int tracker);
void pop_it(stack_t **stack, unsigned int tracker);
void nop_nothing(stack_t **stack, unsigned int tracker);
void pint_top(stack_t **stack, unsigned int tracker);
/**
* pall_it - function that prints all elements in the stack
* @stack: pointer to the stack
* @tracker: point at which the function was called
*/
void pall_it(stack_t **stack, unsigned int tracker)
{
stack_t *fornow;
if (args->head == NULL)
return;
(void) tracker;
(void) stack;
fornow = args->head;
while (fornow != NULL)
{
printf("%d\n", fornow->n);
fornow = fornow->next;
}
}
/**
* nop_nothing - a function that does nothing
* @stack: pointer to the head of stack
* @tracker: point at which this function is called
*/
void nop_nothing(stack_t **stack, unsigned int tracker)
{
(void) stack;
(void) tracker;
}
/**
* pint_top - a function that prints the top element of a stack
* @stack: pointer to the head of the stack
* @tracker: point where the function is called
*/
void pint_top(stack_t **stack, unsigned int tracker)
{
(void) stack;
if (args->head == NULL)
{
dprintf(2, "L%d: can't pint, stack empty\n", tracker);
free_resources();
exit(EXIT_FAILURE);
}
printf("%d\n", args->head->n);
}
/**
* push_it - Pushes an integer onto the stack or queue
* @stack: Double pointer to the top of the stack/queue
* @tracker: Position at which this function is called within the file
*
*function pushes an integer onto the stack or queue based on behavior defined.
*handles error cases and performs necessary memory allocation and assignments.
*/
void push_it(stack_t **stack, unsigned int tracker)
{
if (args->array_of_toks <= 1 || !is_valid_integer(args->toks[1]))
{
free_args();
dprintf(2, "L%d: usage: push integer\n", tracker);
exit(EXIT_FAILURE);
}
*stack = malloc(sizeof(stack_t));
if (*stack == NULL)
{
malloc_error();
free_resources();
}
(*stack)->next = (*stack)->prev = NULL;
(*stack)->n = atoi(args->toks[1]);
if (args->stack)
{
if (args->head == NULL)
{
args->head = *stack;
}
else
{
(*stack)->next = args->head;
args->head->prev = *stack;
args->head = *stack;
}
}
else
{
if (args->head == NULL)
{
args->head = *stack;
}
else
{
stack_t *first_node = args->head;
while (first_node->next)
first_node = first_node->next;
first_node->next = *stack;
(*stack)->prev = first_node;
}
}
args->element_count += 1;
}
/**
* pop_it - Removes the top element from the stack
* @stack: Pointer to a pointer to the stack
* @tracker: Line number tracker for error reporting
*
* This function removes the top element from the stack and updates the stack.
* If the stack is empty, it prints an error message and exits.
*/
void pop_it(stack_t **stack, unsigned int tracker)
{
(void) stack;
if (args->head == NULL)
{
fprintf(stderr, "L%d: can't pop an empty stack\n", tracker);
free_resources();
exit(EXIT_FAILURE);
}
pop_stack_node();
args->element_count -= 1;
}