-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_utils.c
More file actions
117 lines (106 loc) · 2.25 KB
/
stack_utils.c
File metadata and controls
117 lines (106 loc) · 2.25 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/24 11:26:39 by miissa #+# #+# */
/* Updated: 2026/01/05 11:34:58 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int stack_is_sorted_asc(t_stack *s)
{
t_node *temp;
if (!s || s->size <= 1)
return (1);
temp = s->top;
while (temp && temp->next)
{
if (temp->value >= temp->next->value)
return (0);
temp = temp->next;
}
return (1);
}
void stack_free_all(t_stack *s)
{
t_node *current;
t_node *to_delete;
current = s->top;
while (current)
{
to_delete = current;
current = current->next;
free(to_delete);
to_delete = NULL;
}
s->bottom = NULL;
s->top = NULL;
s->size = 0;
}
int stack_pos_of_min_index(t_stack *a)
{
t_node *cur;
int min;
int pos;
int best;
if (!a || a->size == 0)
return (0);
cur = a->top;
min = cur->index;
pos = 0;
best = 0;
while (cur)
{
if (cur->index < min)
{
min = cur->index;
best = pos;
}
cur = cur->next;
pos++;
}
return (best);
}
int stack_pos_of_max_index(t_stack *a)
{
t_node *cur;
int max;
int pos;
int best;
if (!a || a->size == 0)
return (0);
cur = a->top;
max = cur->index;
pos = 0;
best = 0;
while (cur)
{
if (cur->index > max)
{
max = cur->index;
best = pos;
}
cur = cur->next;
pos++;
}
return (best);
}
t_node *stack_pop_back(t_stack *s)
{
t_node *n;
if (!s || s->size == 0)
return (NULL);
n = s->bottom;
s->bottom = n->prev;
if (s->bottom)
s->bottom->next = NULL;
else
s->top = NULL;
n->next = NULL;
n->prev = NULL;
s->size--;
return (n);
}