# push_swap ## Allowed functions - read, write, malloc, free, exit, printf - Any libft functions in standard libraries - strlen - calloc - etc... ## push_swap #1 2つのスタックを用いて、intの配列をsortするプログラムpush_swapを作成するために必要な下記の関数と`struct stackset`を実装してください。 ``` typedef struct s_stackset t_stackset; // スタック操作 void sa(t_stackset *s); void sb(t_stackset *s); void pa(t_stackset *s); void pb(t_stackset *s); void ra(t_stackset *s); void rb(t_stackset *s); void rra(t_stackset *s); void rrb(t_stackset *s); // そのほかの操作 stackset *new_stackset(void); void add(t_stackset *s, int x) void print(t_stackset *s); ``` ### 各種スタック操作 各種スタック操作の説明は下記の通りです。 **sa** (swap a): Swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements. **sb** (swap b): Swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements. **pa** (push a): Take the first element at the top of b and put it at the top of a. Do nothing if b is empty. **pb** (push b): Take the first element at the top of a and put it at the top of b. Do nothing if a is empty. **ra** (rotate a): Shift up all elements of stack a by 1. The first element becomes the last one. **rb** (rotate b): Shift up all elements of stack b by 1. The first element becomes the last one. **rra** (reverse rotate a): Shift down all elements of stack a by 1. The last element becomes the first one. **rrb** (reverse rotate b): Shift down all elements of stack b by 1. The last element becomes the first one そのほかの操作の挙動は、下記のサンプルプログラムを参考にしてください。 ### サンプルプログラム ``` #include <stdio.h> #include "push_swap.h" void print_sep(char *s) { printf("----------------------------------------------------------------------------------------------------------\n") printf("%s\n", s) } int main(void) { stackset *s; s = new_stackset(); { print_sep("Init a and b:"); add(s, 8); add(s, 5); add(s, 6); add(s, 3); add(s, 1); add(s, 2); print(s); } { print_sep("Exec sa:"); sa(s); print(s); } { print_sep("Exec pb pb pb:"); pb(s); pb(s); pb(s); print(s); } { print_sep("Exec ra rb:"); ra(s); rb(s); print(s); } { print_sep("Exec rra rrb:"); rra(s); rrb(s); print(s); } { print_sep("Exec sa:"); sa(s); print(s); } { print_sep("Exec pa pa pa:"); pa(s); pa(s); pa(s); print(s); } } ``` Output of Example Program ``` $> gcc *.c -o stackset $> ./stackset ---------------------------------------------------------------------------------------------------------- Init a and b: 2 1 3 6 5 8 _ _ a b ---------------------------------------------------------------------------------------------------------- Exec sa: 1 2 3 6 5 8 _ _ a b ---------------------------------------------------------------------------------------------------------- Exec pb pb pb: 6 3 5 2 8 1 _ _ a b ---------------------------------------------------------------------------------------------------------- Exec ra rb (equiv. to rr): 5 2 8 1 6 3 _ _ a b ---------------------------------------------------------------------------------------------------------- Exec rra rrb (equiv. to rrr): 6 3 5 2 8 1 _ _ a b ---------------------------------------------------------------------------------------------------------- Exec sa: 5 3 6 2 8 1 _ _ a b ---------------------------------------------------------------------------------------------------------- Exec pa pa pa: 1 2 3 5 6 8 _ _ a b ``` ## push_swap #2 Using the functions and stackset implemented in push_swap #1, write a program to sort in ascending order numbers into stack a. You have to write a program named push_swap that takes as an argument the stack a ormatted as a list of integers. The first argument should be at the top of the stack (be careful about the order). ``` $> ./push_swap 2 1 3 6 5 8 sa pb pb pb sa pa pa pa $> ./push_swap 0 one 2 3 Error ```