-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_func1.c
76 lines (66 loc) · 1.6 KB
/
stack_func1.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
#include "monty.h"
/**
* add_to_stack - Adds a node to the stack.
* @new_node: Pointer to the new node.
* @ln: Interger representing the line number of of the opcode.
*/
void add_to_stack(stack_t **new_node, __attribute__((unused))unsigned int ln)
{
stack_t *tmp;
if (new_node == NULL || *new_node == NULL)
exit(EXIT_FAILURE);
if (head == NULL)
{
head = *new_node;
return;
}
tmp = head;
head = *new_node;
head->next = tmp;
tmp->prev = head;
}
/**
* print_stack - Adds a node to the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: line number of the opcode.
*/
void print_stack(stack_t **stack, unsigned int line_number)
{
stack_t *tmp;
(void) line_number;
if (stack == NULL)
exit(EXIT_FAILURE);
tmp = *stack;
while (tmp != NULL)
{
printf("%d\n", tmp->n);
tmp = tmp->next;
}
}
/**
* pop_top - Adds a node to the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void pop_top(stack_t **stack, unsigned int line_number)
{
stack_t *tmp;
if (stack == NULL || *stack == NULL)
more_err(7, line_number);
tmp = *stack;
*stack = tmp->next;
if (*stack != NULL)
(*stack)->prev = NULL;
free(tmp);
}
/**
* print_top - Prints the top node of the stack.
* @stack: Pointer to a pointer pointing to top node of the stack.
* @line_number: Interger representing the line number of of the opcode.
*/
void print_top(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || *stack == NULL)
more_err(6, line_number);
printf("%d\n", (*stack)->n);
}