-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty_ops#1.c
97 lines (82 loc) · 2.12 KB
/
monty_ops#1.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"
/**
*op_pall - prints all the values on the stack,
*starting from the top of the stack or queue.
*@dlinkedlist: the header of the stack or queue
*@line_num: this is unused in this function
*Return: none
*/
void op_pall(stack_t **dlinkedlist, unsigned int line_num)
{
(void) line_num;
print_dlistint(*dlinkedlist);
}
/**
* op_push - pushes data to stack
* @dlinkedlist: pointer to list
* @line_num: line number of command
*/
void op_push(stack_t **dlinkedlist, unsigned int line_num)
{
/* Check if line_num is an integer */
(void) line_num;
if (carrier.state == 0)
add_dnodeint(dlinkedlist, carrier.data);
else
add_dnodeint_end(dlinkedlist, carrier.data);
}
/**
*op_pint - prints the value at the top of the stack, followed by a new line
* @dlinkedlist: pointer to list
* @line_num: line number of command
*/
void op_pint(stack_t **dlinkedlist, unsigned int line_num)
{
if (*dlinkedlist == NULL)
{
fprintf(stderr, "L%i: can't pint, stack empty\n", line_num);
free_dlistint(*dlinkedlist), free(carrier.words), free(carrier.line);
fclose(carrier.stream);
exit(EXIT_FAILURE);
}
printf("%i\n", (*dlinkedlist)->n);
}
/**
* op_pop - removes an item from a stack
* @dlinkedlist: pointer to a list
* @line_num: line number of op command
*/
void op_pop(stack_t **dlinkedlist, unsigned int line_num)
{
if (*dlinkedlist == NULL)
{
fprintf(stderr, "L%i: can't pop an empty stack\n", line_num);
free_dlistint(*dlinkedlist), free(carrier.words), free(carrier.line);
fclose(carrier.stream);
exit(EXIT_FAILURE);
}
else
{
delete_head(dlinkedlist);
}
}
/**
* op_swap - swaps the top two elements of the stack.
* @dlinkedlist: pointer to a list
* @line_num: line number of op command
*/
void op_swap(stack_t **dlinkedlist, unsigned int line_num)
{
int a, b;
if (dlistint_len(*dlinkedlist) < 2)
{
fprintf(stderr, "L%i: can't swap, stack too short\n", line_num);
free_dlistint(*dlinkedlist), free(carrier.words), free(carrier.line);
fclose(carrier.stream);
exit(EXIT_FAILURE);
}
a = (*dlinkedlist)->n;
b = (*dlinkedlist)->next->n;
(*dlinkedlist)->n = b;
(*dlinkedlist)->next->n = a;
}