push_swap 명령어 구현

이민규·2023년 7월 7일
0

42seoul

목록 보기
11/24

sa sb(swap_stack)

플래그를 이용하여 sa sb키워드를 구현했다

void	swap_stack(t_dlist	*stack_a, t_command_dlist *stack_command, int flag)
{
	t_node	*temp_node;

	if (flag == 1)
		add_last_command_node(stack_command, "sa");
	else if (flag == 2)
		add_last_command_node(stack_command, "sb");
	if (stack_a->size <= 1)
		return ;
	else if (stack_a->size == 2)
	{
		stack_a->head = stack_a->head->next;
		return ;
	}
	temp_node = stack_a->head->next;
	temp_node->prev = stack_a->head->prev;
	temp_node->next->prev = stack_a->head;
	stack_a->head->next = temp_node->next;
	stack_a->head->prev = temp_node;
	temp_node->next = stack_a->head;
	stack_a->head = temp_node;
}

ss(swap_stack_all)

swap_stack 함수를 stack_a stack_b 두번 호출하여 구현하였다

void	swap_stack_all(t_dlist *stack_a, t_dlist *stack_b, \
t_command_dlist *stack_command)
{
	add_last_command_node(stack_command, "ss");
	swap_stack(stack_a, NULL, 3);
	swap_stack(stack_b, NULL, 3);
}

pa pb(push_stack)

flag를 이용하여 pa pb를 구현하였다

void	push_stack(t_dlist *stack_a, t_dlist *stack_b, \
t_command_dlist *stack_command, int flag)
{
	if (flag == 1)
		add_last_command_node(stack_command, "pa");
	else if (flag == 2)
		add_last_command_node(stack_command, "pb");
	if (stack_b->size == 0)
		return ;
	add_first_node(stack_a, stack_b->head->value);
	delete_first_node(stack_b);
}

ra rb(rotate_stack)

flag를 이용하여 ra rb를 구현하였다

void	rotate_stack(t_dlist *stack_a, t_command_dlist *stack_command, int flag)
{
	if (flag == 1)
		add_last_command_node(stack_command, "ra");
	else if (flag == 2)
		add_last_command_node(stack_command, "rb");
	if (!(stack_a == NULL || stack_a->size <= 1))
	{
		add_last_node(stack_a, stack_a->head->value);
		delete_first_node(stack_a);
	}
}

rr(rotate_stack_all)

rotate_stack함수를 이용하여 stack_a stack_b 각각 호출하여 구현하였다

void	rotate_stack_all(t_dlist *stack_a, t_dlist *stack_b, \
t_command_dlist *stack_command)
{
	add_last_command_node(stack_command, "rr");
	if (!(stack_a == NULL || stack_a->size <= 1))
		rotate_stack(stack_a, NULL, 3);
	if (!(stack_b == NULL || stack_b->size <= 1))
		rotate_stack(stack_b, NULL, 3);
}

rra rrb(reverse_rotate_stack)

flag를 이용하여 rra rrb를 구현하였다

void	reverse_rotate_stack(t_dlist *stack_a, t_command_dlist \
*stack_command, int flag)
{
	if (flag == 1)
		add_last_command_node(stack_command, "rra");
	else if (flag == 2)
		add_last_command_node(stack_command, "rrb");
	else if (flag == 3)
		;
	if (stack_a == NULL || stack_a->size <= 1)
		return ;
	add_first_node(stack_a, stack_a->head->prev->value);
	delete_last_node(stack_a);
}

rrr(reverse_rotate_stack_all)

reverse_rotate함수를 이용하여 stack_a stack_b두번호출하여 rrr을 구현하였다

void	reverse_rotate_stack_all(t_dlist *stack_a, t_dlist *stack_b, \
t_command_dlist *stack_command)
{
	add_last_command_node(stack_command, "rrr");
	if (!(stack_a == NULL || stack_a->size <= 1))
		reverse_rotate_stack(stack_a, NULL, 3);
	if (!(stack_b == NULL || stack_b->size <= 1))
		reverse_rotate_stack(stack_b, NULL, 3);
}
profile
프로그래머 희망자(휴직중)

0개의 댓글