forked from bravemaster3/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations4.c
More file actions
93 lines (80 loc) · 1.62 KB
/
operations4.c
File metadata and controls
93 lines (80 loc) · 1.62 KB
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_queue_fun - adds node at the end of a stack, i.e. in queue mode
* @stack: pointer to top (pointer to pointer)
* @line_number: liner number in the file
* Return: No return
*/
void push_queue_fun(stack_t **stack, unsigned int line_number)
{
char *arg = strtok(NULL, " \n");
stack_t *new, *ptr;
if (arg == NULL || is_valid_integer(arg) == 0)
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
errno = -999;
return;
}
if (stack == NULL)
return;
new = malloc(sizeof(stack_t));
if (new == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
errno = -999;
return;
}
new->n = atoi(arg);
new->next = NULL;
ptr = *stack;
if (*stack == NULL)
{
new->prev = NULL;
*stack = new;
return;
}
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new;
new->prev = ptr;
}
/**
* queue_fun - Changes mode to queue
* @stack: pointer to top (pointer to pointer)
* @line_number: liner number in the file
* Return: No return
*/
void queue_fun(stack_t **stack, unsigned int line_number)
{
int i;
(void)stack;
(void)line_number;
for (i = 0; all_ops[i].opcode != NULL; i++)
{
if (strcmp("push", all_ops[i].opcode) == 0)
{
all_ops[i].f = push_queue_fun;
break;
}
}
}
/**
* stack_fun - Changes mode to stack
* @stack: pointer to top (pointer to pointer)
* @line_number: liner number in the file
* Return: No return
*/
void stack_fun(stack_t **stack, unsigned int line_number)
{
int i;
(void)stack;
(void)line_number;
for (i = 0; all_ops[i].opcode != NULL; i++)
{
if (strcmp("push", all_ops[i].opcode) == 0)
{
all_ops[i].f = push_fun;
break;
}
}
}