-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_core_funs.c
107 lines (101 loc) · 2.14 KB
/
stack_core_funs.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
#include "monty.h"
/**
* pall - print all function
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void pall(stack_t **stack, unsigned int line_number)
{
(void) line_number;
print_dlistint(*stack);
}
/**
* pint - print int
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void pint(stack_t **stack, unsigned int line_number)
{
stack_t *aux = *stack;
(void) line_number;
if (!(*stack))
{
dprintf(2, "L%d: can't pint, stack empty\n", initial.line_cnt);
free_dlistint(initial.head);
free(initial.readed);
fclose(initial.monty_file);
exit(EXIT_FAILURE);
}
while (aux->next)
aux = aux->next;
printf("%d\n", aux->n);
}
/**
* pop - remove element from stack/queue
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void pop(stack_t **stack, unsigned int line_number)
{
(void) line_number;
if (!(*stack))
{
dprintf(2, "L%d: can't pop an empty stack\n", initial.line_cnt);
free_dlistint(initial.head);
free(initial.readed);
fclose(initial.monty_file);
exit(EXIT_FAILURE);
}
else
delete_dnodeint_at_end(stack);
}
/**
* swap - swap last two elements
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void swap(stack_t **stack, unsigned int line_number)
{
stack_t *aux = *stack;
int len = dlistint_len(*stack);
int tmp;
(void)line_number;
if (len < 2)
{
dprintf(2, "L%d: can't swap, stack too short\n", initial.line_cnt);
free_dlistint(initial.head);
free(initial.readed);
fclose(initial.monty_file);
exit(EXIT_FAILURE);
}
while (aux->next)
aux = aux->next;
tmp = aux->n;
aux->n = aux->prev->n;
aux->prev->n = tmp;
}
/**
* add - add last to elements in one
* @stack: stack/queue structure
* @line_number: line_cnt
*/
void add(stack_t **stack, unsigned int line_number)
{
stack_t *aux = *stack;
int len = dlistint_len(*stack);
int tmp;
(void) line_number;
if (len < 2)
{
dprintf(2, "L%d: can't add, stack too short\n", initial.line_cnt);
free_dlistint(initial.head);
free(initial.readed);
fclose(initial.monty_file);
exit(EXIT_FAILURE);
}
while (aux->next)
aux = aux->next;
tmp = aux->n + aux->prev->n;
aux->prev->n = tmp;
delete_dnodeint_at_end(stack);
}