Skip to content

Commit

Permalink
Double linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
99omniaashraf committed Jan 24, 2024
1 parent 09e10bb commit 6d6a749
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Binary file added 0x00-challenge/4-delete_dnodeint/delete_dnodeint
Binary file not shown.
53 changes: 53 additions & 0 deletions 0x00-challenge/4-delete_dnodeint/delete_dnodeint_at_index.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "lists.h"
#include <stdlib.h>

/**
* delete_dnodeint_at_index - Delete a node at a specific index from a list
*
* @head: A pointer to the first element of a list
* @index: The index of the node to delete
*
* Return: 1 on success, -1 on failure
*/
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index)
{
dlistint_t *saved_head;
dlistint_t *tmp;
unsigned int p;

if (*head == NULL)
{
return (-1);
}
saved_head = *head;
p = 0;
while (p < index && *head != NULL)
{
*head = (*head)->next;
p++;
}
if (p != index)
{
*head = saved_head;
return (-1);
}
if (0 == index)
{
tmp = (*head)->next;
free(*head);
*head = tmp;
if (tmp != NULL)
{
tmp->prev = NULL;
}
}
else
{
(*head)->prev->next = (*head)->next;
free(*head);
if ((*head)->next)
(*head)->next->prev = (*head)->prev;
*head = saved_head;
}
return (1);
}

0 comments on commit 6d6a749

Please sign in to comment.