-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopcodes_0.c
125 lines (111 loc) · 2.63 KB
/
opcodes_0.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
#include "monty.h"
int data_num;
char *argument;
unsigned int mode;
/**
* push - Pushes a number to the stack(or queue)
* @top: Pointer to pointer to the top of the stack.
* @line_number: Line number of the instruction.
* Return: Void.
*/
void push(stack_t **top, unsigned int line_number)
{
int i;
if (argument == NULL)
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_stack(*top);
exit(EXIT_FAILURE);
}
for (i = 0; argument[i] != '\0'; i++)
if (argument[i] != '-' &&
(argument[i] < '0' || argument[i] > '9'))
{
fprintf(stderr, "L%d: usage: push integer\n",
line_number);
free_stack(*top);
exit(EXIT_FAILURE);
}
data_num = atoi(argument);
if (data_num == 0 && (strcmp(argument, "0") != 0)
&& (strcmp(argument, "-0")))
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_stack(*top);
exit(EXIT_FAILURE);
}
if (mode == 0)
add_element(top, data_num);
else if (mode == 1)
add_element_end(top, data_num);
}
/**
* pall - prints all the values on the stack,
* starting from the top of the stack
*
* @top: Pointer to pointer to the top of the stack.
* @line_number: Line number of the instruction.
* Return: Void.
*/
void pall(stack_t **top, unsigned int line_number __attribute__((unused)))
{
p_stack_t(*top);
}
/**
* pint - prints the value at the top of the stack(or queue)
* @top: Pointer to pointer to the top of the stack.
* @line_number: Line number of the instruction.
* Return: Void.
*/
void pint(stack_t **top, unsigned int line_number)
{
int top_val;
stack_t *head;
head = *top;
if (head == NULL)
{
fprintf(stderr, "L%d: can't pint, stack empty\n", line_number);
free_stack(*top);
exit(EXIT_FAILURE);
}
top_val = head->n;
printf("%d\n", top_val);
}
/**
* pop - removes the top element of the stack(or queue)
* @top: Pointer to pointer to the top of the stack.
* @line_number: Line number of the instruction.
* Return: Void.
*/
void pop(stack_t **top, unsigned int line_number)
{
if ((*top) == NULL)
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
free_stack(*top);
exit(EXIT_FAILURE);
}
delete_element(top, 0);
}
/**
* swap -swaps the top two elements of the stack(or queue)
* @top: Pointer to pointer to the top of the stack.
* @line_number: Line number of the instruction.
* Return: Void.
*/
void swap(stack_t **top, unsigned int line_number)
{
int temp;
stack_t *head;
head = *top;
if (head == NULL || head->next == NULL)
{
fprintf(stderr, "L%d: can't swap, stack too short\n",
line_number);
free_stack(*top);
exit(EXIT_FAILURE);
}
temp = head->n;
head->n = head->next->n;
head->next->n = temp;
}