Repository navigation
Expand file tree
/
Copy pathstack_utils.c
More file actions
84 lines (73 loc) · 1.81 KB
/
Copy pathstack_utils.c
File metadata and controls
84 lines (73 loc) · 1.81 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maslan <maslan@student.42heilbronn.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/07/18 22:17:56 by maslan #+# #+# */
/* Updated: 2026/07/18 22:17:57 by maslan ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *new_node(int value)
{
t_stack *node;
node = (t_stack *)malloc(sizeof(t_stack));
if (!node)
return (NULL);
node->value = value;
node->index = 0;
node->next = NULL;
return (node);
}
/* append to tail so argv[1] stays at head = top of the stack */
void add_back(t_stack **stack, t_stack *node)
{
t_stack *cur;
if (!*stack)
{
*stack = node;
return ;
}
cur = *stack;
while (cur->next)
cur = cur->next;
cur->next = node;
}
void free_stack(t_stack **stack)
{
t_stack *cur;
t_stack *next;
if (!stack)
return ;
cur = *stack;
while (cur)
{
next = cur->next;
free(cur);
cur = next;
}
*stack = NULL;
}
int stack_size(t_stack *stack)
{
int count;
count = 0;
while (stack)
{
count++;
stack = stack->next;
}
return (count);
}
int is_sorted(t_stack *stack)
{
while (stack && stack->next)
{
if (stack->value > stack->next->value)
return (0);
stack = stack->next;
}
return (1);
}