-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcodes.c
97 lines (86 loc) · 2.24 KB
/
opcodes.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
#include "monty.h"
/**
* push - Adds a node to the top of the stack.
* @new_node: Pointer to the new node to be pushed.
* @line_number: Integer representing the line number of the opcode.
*/
void push(stack_t **new_node, unsigned int line_number)
{
stack_t *temp;
(void) line_number;
if (new_node == NULL || *new_node == NULL)
exit(EXIT_FAILURE);
if (head == NULL)
{
head = *new_node;
return;
}
temp = head;
head = *new_node;
head->next = temp;
temp->prev = head;
}
/**
* print_entire_stack - Prints all the values in a stack.
* @stack: Double pointer pointing to the top node of the stack.
* @line_number: line number of the opcode.
*/
void print_entire_stack(stack_t **stack,
__attribute__((unused))unsigned int line_number)
{
stack_t *temp;
if (stack == NULL)
exit(EXIT_FAILURE);
temp = *stack;
while (temp != NULL)
{
printf("%d\n", temp->n);
temp = temp->next;
}
}
/**
* print_stack_top - Prints the top node of the stack.
* @stack: Double pointer pointing to the top node of the stack.
* @line_number: Integer representing the line number of the opcode.
*/
void print_stack_top(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || *stack == NULL)
print_other_errors(6, line_number);
printf("%d\n", (*stack)->n);
}
/**
* pop - Removes the top node from the stack.
* @stack: Double pointer pointing to the top node of the stack.
* @line_number: Integer representing the line number of the opcode.
*/
void pop(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
if (stack == NULL || *stack == NULL)
print_other_errors(7, line_number);
temp = *stack;
*stack = temp->next;
if (*stack != NULL)
(*stack)->prev = NULL;
free(temp);
}
/**
* swap - Swaps the top two elements of the stack.
* @stack: Double pointer pointing to the top node of the stack.
* @line_number: Integer representing the line number of the opcode.
*/
void swap(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
print_other_errors(8, line_number, "swap");
temp = (*stack)->next;
(*stack)->next = temp->next;
if (temp->next != NULL)
temp->next->prev = *stack;
temp->next = *stack;
(*stack)->prev = temp;
temp->prev = NULL;
*stack = temp;
}