-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap_queue.c
108 lines (97 loc) · 2.67 KB
/
push_swap_queue.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
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_queue.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jeongble <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 14:26:31 by jeongble #+# #+# */
/* Updated: 2022/07/22 09:32:52 by jeongble ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int (*const g_ops[11])(t_stack *stacks) = {
sa, sb, ss, pa, pb, ra, rb, rr, rra, rrb, rrr
};
t_queue *queue_new(t_stack *stacks, t_op *head, t_op *tail, int last_op)
{
t_queue *result;
result = (t_queue *)malloc(sizeof(t_queue));
result->next = 0;
result->stacks = stacks;
result->head = head;
result->tail = tail;
result->last_op = last_op;
return (result);
}
void queue_del(t_queue **front)
{
t_queue *next;
next = (*(front))->next;
stack_del((*(front))->stacks);
op_clear(&((*(front))->head));
free(*(front));
*(front) = next;
}
static void queue_add_op(t_op **head, t_op **tail, int i)
{
if (*(head))
{
(*(tail))->next = op_new(i);
*(tail) = (*(tail))->next;
}
else
{
*(head) = op_new(i);
*(tail) = *(head);
}
}
static int queue_issort(t_stack *stack_temp, t_op *op_temp, t_op **result)
{
int now;
int last;
t_num *stack;
if (stack_temp->stack_b)
return (0);
stack = stack_temp->stack_a;
last = stack->n;
stack = stack->next;
while (stack)
{
now = stack->n;
if (last > now)
return (0);
last = now;
stack = stack->next;
}
*(result) = op_temp;
stack_del(stack_temp);
return (1);
}
void queue_start(t_queue **front, t_queue **back, t_op **result, int n)
{
int i;
t_stack *stack_temp;
t_op *op_temp;
t_op *op_tail_temp;
(void)n;
i = -1;
while (++i < 11 && !*(result))
{
if (!op_valid((*(front))->last_op, i))
continue ;
stack_temp = stack_dup((*(front))->stacks);
if ((g_ops[i])(stack_temp))
stack_del(stack_temp);
else
{
op_dup(&op_temp, &op_tail_temp, (*(front))->head);
queue_add_op(&op_temp, &op_tail_temp, i);
if (queue_issort(stack_temp, op_temp, result))
continue ;
(*(back))->next = queue_new(stack_temp, op_temp, op_tail_temp, i);
*(back) = (*(back))->next;
}
}
queue_del(front);
}