-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
98 lines (92 loc) · 2.91 KB
/
push_swap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschott <jschott@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/26 10:41:35 by jschott #+# #+# */
/* Updated: 2024/08/08 11:20:27 by jschott ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/*
void print_stack(t_list *list, int res)
{
long int num;
if (!list)
return ;
else if (list->next)
{
while (list->next)
{
num = (long int) list->index;
printf ("%*li | ", 5, num);
num = (long int) list->index;
printf ("%*li | ", 3, num / res);
num = (long int) list->content;
printf ("%li\n", num);
list = list->next;
}
}
num = (long int) list->index;
printf ("%*li | ", 5, num);
num = (long int) list->index;
printf ("%*li | ", 3, num / res);
num = (long int) list->content;
printf ("%li\n", num);
}
*/
/**
* Checks if a stack is sorted in ascending order based on index.
*
* @param stack A pointer to the top of the stack.
* @return 1 if the stack is sorted in ascending order, 0 otherwise.
*/
int is_ascending(t_list *stack)
{
t_list *nxt;
int one;
int two;
nxt = stack->next;
while (nxt)
{
one = stack->index;
two = nxt->index;
if (one > two)
return (0);
stack = nxt;
nxt = nxt->next;
}
return (1);
}
/**
* The main function of a program that sorts a stack of numbers.
*
* This function serves as the entry point for a program that sorts a stack of numbers. It takes command-line
* arguments (`argc`, `argv`), where `argv` contains the numbers to be sorted. It initializes two stacks, `a` and
* `b`, and attempts to read the input numbers into stack `a` using the `read_input` function. If the input is valid
* and stack `a` is not already sorted in ascending order, it calls `sort_five` to sort the stack. Finally, it
* cleans up by freeing the memory allocated for both stacks.
*
* @param argc The number of command-line arguments.
* @param argv An array of command-line arguments.
* @return 1 if the input is invalid or less than two arguments are provided, 0 otherwise.
*/
int main(int argc, char **argv)
{
t_list *a;
t_list *b;
a = NULL;
b = NULL;
if (argc < 2)
return (1);
a = read_input(&argv[1], argc);
if (!a)
return (write(2, "Error\n", 6));
if (!is_ascending(a))
sort_five(&a, &b);
ft_lstclear(&a, free);
ft_lstclear(&b, free);
return (0);
}