Beispiel #1
0
Datei: avl.c Projekt: const86/rfs
static bool insert(const struct avl *t, void **pp, void **px)
{
	if (*pp == NULL)
		return insert_here(t, pp, px);

	int cmp = t->cmp(*px, *pp);
	if (cmp == 0)
		return insert_here(t, pp, px);

	struct avl_node *p = node(t, *pp);
	bool dir = cmp > 0;
	int idir = dir ? 1 : -1;
	bool grow = insert(t, &p->child[dir], px);

	p->balance += idir * grow;
	if (!grow || p->balance == 0)
		return false;

	if (p->balance == idir)
		return true;

	if (node(t, p->child[dir])->balance == idir)
		rotate(t, pp, dir);
	else
		rotate2(t, pp, dir);

	return false;
}
Beispiel #2
0
int		ft_lstinsert(t_list_head *head, t_list *to_insert,
			int (*insert_here)(void *, void *))
{
	t_list	*temp;

	if (!head || !to_insert)
		return (1);
	if (!head->first || insert_here(head->first->content, to_insert->content))
		return (ft_lstpushfront(head, to_insert));
	temp = head->first;
	while (temp->next && !insert_here(temp->next->content, to_insert->content))
		temp = temp->next;
	if (!temp->next)
		head->last = to_insert;
	to_insert->next = temp->next;
	temp->next = to_insert;
	++head->len;
	return (0);
}