-
Notifications
You must be signed in to change notification settings - Fork 1
/
func2.c
77 lines (67 loc) · 1.43 KB
/
func2.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
#include "monty.h"
/**
* push - add an element to the stack/queue
* @stack_ptr: pointer to dlinked list
* @arg_data: data of node to be pushed
* @count: line number
*/
void push(stack_t **stack_ptr, char *arg_data, unsigned int count)
{
stack_t *node;
if (arg_data == NULL)
{
fprintf(stderr, "L%d: usage: push integer\n", count);
free(*stack_ptr);
*stack_ptr = NULL;
exit(EXIT_FAILURE);
};
node = add_dnodeint(stack_ptr, atoi(arg_data));
if (node == NULL)
{
puts("Error: malloc failed");
free_stack(*stack_ptr);
exit(EXIT_FAILURE);
}
}
/**
* pall - prints top of stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void pop(stack_t **stack_ptr, unsigned int count)
{
if (stack_ptr != NULL)
delete_dnodeint(stack_ptr, count);
else
{
printf("L%d: can't pop an empty stack", count);
exit (EXIT_FAILURE);
}
}
/**
* pall - prints top of stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void swap(stack_t **stack_ptr, unsigned int count)
{
int buff;
if ((*stack_ptr) == NULL || (*stack_ptr)->next == NULL)
{
printf("L%d: can't swap, stack too short", count);
exit(EXIT_FAILURE);
}
buff = (*stack_ptr)->n;
(*stack_ptr)->n = (*stack_ptr)->next->n;
(*stack_ptr)->next->n = buff;
}
/**
* pall - prints top of stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void nop(stack_t **stack_ptr, unsigned int count)
{
(void)stack_ptr;
(void)count;
}