42 Exam Rank 03 Today

Good luck, and may your pointers never be dangling!

t_btree *node; struct s_queue *next; t_queue; void enqueue(t_queue **q, t_btree *node)

int count = 0; while (begin_list) count++; begin_list = begin_list->next; return (count);

// add to end

Do not use recursion here — unnecessary and slower. 2. ft_list_remove_if (Medium) void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)())

> 3 ex00? y Submitted. Moulinette: OK (2/2)

if (cmp(current->data, data_ref) == 0) if (previous) previous->next = current->next; else *begin_list = current->next; free(current); current = previous ? previous->next : *begin_list; else previous = current; current = current->next; 42 Exam Rank 03

if (!node) return (NULL); if (cmp(node->item, ref) == 0) return (node->item); void *left = search(node->left, ref, cmp); if (left) return (left); return (search(node->right, ref, cmp));

| Exercise | Difficulty | Data Structure | Recursion Required? | |----------|------------|----------------|----------------------| | ft_list_size | Easy | Singly linked list | No (iterative) | | ft_list_push_back | Easy | Singly linked list | No | | ft_list_last | Easy | Singly linked list | No | | ft_list_foreach | Easy | Singly linked list | No | | ft_list_remove_if | Medium | Singly linked list | No | | ft_list_reverse | Medium | Singly linked list | No | | ft_list_sort | Medium-Hard | Singly linked list | Optional | | ft_list_merge | Medium | Singly linked list | No | | ft_itoa_base | Medium | String (no struct) | No (but tricky) | | ft_atoi_base | Medium | String (no struct) | No | | ft_split_whitespaces | Easy | String → array | No | | ft_range | Easy | Array | No | | ft_ultimate_range | Easy | Array | No | | ft_strjoin | Easy | String | No | | ft_btree_create_node | Easy | Binary tree | No | | ft_btree_apply_prefix | Medium | Binary tree | Yes | | ft_btree_apply_infix | Medium | Binary tree | Yes | | ft_btree_apply_suffix | Medium | Binary tree | Yes | | ft_btree_insert_data | Hard | Binary tree | Yes | | ft_btree_search_item | Medium | Binary tree | Yes | | ft_btree_level_count | Medium | Binary tree | Yes | | ft_btree_apply_by_level | Hard | Binary tree | Yes (with queue) | When you start Rank 03:

if (!*root) *root = ft_btree_create_node(item); return; if (cmpf(item, (*root)->item) < 0) ft_btree_insert_data(&(*root)->left, item, cmpf); else ft_btree_insert_data(&(*root)->right, item, cmpf); Good luck, and may your pointers never be dangling

struct s_list *next; void *data; t_list; typedef struct s_btree

> 1 (to start ex00)

| Mistake | Consequence | Prevention | |---------|------------|------------| | Forgetting to include #include <stdlib.h> | Implicit function declaration → Moulinette fails | Write includes at top | | Memory leak in list remove_if | Fails strict test | Always free removed node | | Not handling NULL input | Segmentation fault in tests | Check if (!list) return | | Using recursion for deep lists | Stack overflow (not tested but bad style) | Use iteration for long lists | | Modifying original pointer without pointer-to-pointer | Head lost | Use t_list ** when head can change | | ft_itoa_base INT_MIN bug | Wrong output for -2147483648 | Special case: convert to unsigned | | Not checking base bounds | Undefined behavior → fails | if (base < 2 \|\| base > 16) return (NULL); | Recursion Cheat Sheet for Rank 03 Pattern 1: Traversal (no return value) void traverse(t_btree *node) next : *begin_list