Exemple #1
0
t_bool list_add_elem_at_position(t_list * front_ptr, void *elem, unsigned int position)
{
  unsigned int	i;
  t_list head;
  t_list prev;
  t_list tmp;

  i = -1;
  head = *front_ptr;
  prev = head;
  if (position == 0)
    return (list_add_elem_at_front(front_ptr, elem));
  while (head != NULL && ++i < position)
    {
      prev = head;
      head = head->next;
    }
  if (i < position && head == NULL)
    return (FALSE);
  if ((tmp = malloc(sizeof(tmp))) == NULL)
    return (FALSE);
  tmp->value = elem;
  tmp->next = head;
  prev->next = tmp;
  return (TRUE);
}
Exemple #2
0
t_bool list_add_elem_at_position(t_list* front_ptr, void* elem,
    unsigned int position)
{
  unsigned int i;
  t_list prev_elem;
  t_list new_elem;

  if (position == 0)
    return list_add_elem_at_front(front_ptr, elem);
  if (*front_ptr == NULL)
    return LFALSE;

  if ((new_elem = malloc(sizeof(t_node))) == NULL)
    return LFALSE;
  new_elem->value = elem;

  prev_elem = *front_ptr;
  i = 1;
  while (prev_elem->next != NULL && i != position)
  {
    ++i;
    prev_elem = prev_elem->next;
  }
  prev_elem->next = new_elem;
  return LTRUE;
}
Exemple #3
0
t_bool		list_add_elem_at_position(t_list *front_ptr, void *elem,
					  unsigned int position)
{
  t_list	new_elem;
  t_list	tmp;
  unsigned int	i;

  i = 0;
  tmp = *front_ptr;
  if ((new_elem = malloc(sizeof(t_node))) == NULL ||
      position > list_get_size(*front_ptr))
    return (FALSE);
  if (*front_ptr == NULL || position == 0)
    return (list_add_elem_at_front(front_ptr, elem));
  else
    {
      new_elem->value = elem;
      while (tmp->next && i < position - 1)
	{
	  tmp = tmp->next;
	  i++;
	}
      new_elem->next = tmp->next;
      tmp->next = new_elem;
      return (TRUE);
    }
  return (FALSE);
}
Exemple #4
0
t_bool list_add_elem_at_position(t_list* front_ptr, void *elem, unsigned int position) {
	if (position == 0) {
		return list_add_elem_at_front(front_ptr, elem);
	}
	if (!*front_ptr) {
		return FALSE;
	}
	t_list list = *front_ptr;
	while (--position) {
		list = list->next;
		if (!list) {
			return FALSE;
		}
	}
	t_node *node = list_create_node(elem);
	if (!node) {
		return FALSE;
	}
	node->next = list->next;
	list->next = node;
	return TRUE;
}
Exemple #5
0
t_bool stack_push(t_stack *stack_ptr, void* elem) {
    return list_add_elem_at_front(stack_ptr, elem);
}