ps_list_utils.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ps_list_utils.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2022/11/13 22:04:05 by gychoi #+# #+# */ 9 /* Updated: 2022/12/04 21:38:52 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "push_swap.h" 14 15 t_list *ps_lstlast(t_list *lst) 16 { 17 if (lst == NULL) 18 return (NULL); 19 while (lst->next != NULL) 20 lst = lst->next; 21 return (lst); 22 } 23 24 t_list *ps_lstnew(int value) 25 { 26 t_list *new; 27 28 new = (t_list *)malloc(sizeof(t_list)); 29 if (new == NULL) 30 exit(1); 31 new->data = value; 32 new->next = NULL; 33 new->prev = NULL; 34 return (new); 35 } 36 37 void ps_lstadd_back(t_list **lst, t_list *new) 38 { 39 t_list *curr; 40 41 if (lst == NULL || new == NULL) 42 return ; 43 if (*lst == NULL) 44 { 45 *lst = new; 46 return ; 47 } 48 curr = ps_lstlast(*lst); 49 curr->next = new; 50 new->prev = curr; 51 new->next = NULL; 52 } 53 54 void ps_lstclear(t_deque *deque) 55 { 56 t_list *next; 57 t_list *node; 58 59 node = deque->head; 60 while (node) 61 { 62 next = node->next; 63 free(node); 64 node = next; 65 } 66 free(node); 67 }