mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
wordlist: add destroy_idx and pop_idx functions
This commit is contained in:
parent
ad5eb4cc34
commit
4116c2d92f
5 changed files with 132 additions and 1 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
|
||||
/* Updated: 2025/03/10 14:26:47 by khais ### ########.fr */
|
||||
/* Updated: 2025/03/10 14:27:27 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -41,5 +41,7 @@ t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc);
|
|||
t_worddesc *wordlist_pop(t_wordlist **wordlist);
|
||||
void wordlist_debug(t_wordlist *wordlist);
|
||||
t_wordlist *wordlist_copy(const t_wordlist *wordlist);
|
||||
void wordlist_destroy_idx(t_wordlist **wordlist, int idx);
|
||||
t_worddesc *wordlist_pop_idx(t_wordlist **wordlist, int idx);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
56
src/parser/wordlist/wordlist_idx.c
Normal file
56
src/parser/wordlist/wordlist_idx.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* wordlist_idx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/09 15:14:27 by khais #+# #+# */
|
||||
/* Updated: 2025/03/10 16:08:43 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "wordlist.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
** Remove the element at the given index from the wordlist and return it.
|
||||
**
|
||||
** return null if out of range or wordlist is null
|
||||
*/
|
||||
t_worddesc *wordlist_pop_idx(t_wordlist **wordlist, int idx)
|
||||
{
|
||||
t_wordlist *iter;
|
||||
t_wordlist *temp;
|
||||
t_worddesc *out;
|
||||
|
||||
if (wordlist == NULL || *wordlist == NULL || idx < 0)
|
||||
return (NULL);
|
||||
iter = *wordlist;
|
||||
if (idx == 0)
|
||||
{
|
||||
*wordlist = (*wordlist)->next;
|
||||
out = iter->word;
|
||||
return (free(iter), out);
|
||||
}
|
||||
while (idx-- > 1)
|
||||
{
|
||||
iter = iter->next;
|
||||
if (iter == NULL)
|
||||
return (NULL);
|
||||
}
|
||||
temp = iter->next;
|
||||
if (temp == NULL)
|
||||
return (NULL);
|
||||
out = temp->word;
|
||||
iter->next = temp->next;
|
||||
return (free(temp), out);
|
||||
}
|
||||
|
||||
/*
|
||||
** Remove the element at the given index from the wordlist and destroy it.
|
||||
*/
|
||||
void wordlist_destroy_idx(t_wordlist **wordlist, int idx)
|
||||
{
|
||||
worddesc_destroy(wordlist_pop_idx(wordlist, idx));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue