Skip to content

Commit 377a100

Browse files
committed
Implement delete_dnodeint_at_index function to delete the node at a given index in a doubly linked list
Traverse the list to find the node at the specified index If the node is found: If it is the first node, update the head pointer and the prev pointer of the next node If it is not the first node, update the next pointer of the previous node and the prev pointer of the next node Free the memory of the node Return 1 to indicate successful deletion If the node is not found (index out of range), return -1 to indicate deletion failure Added proper documentation and comments
1 parent c533402 commit 377a100

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "lists.h"
2+
3+
/**
4+
* delete_dnodeint_at_index - deletes the node at a given index in linked list
5+
* @head: pointer to the head of the list
6+
* @index: index of the node to be deleted
7+
*
8+
* Return: 1 if deletion succeeded, -1 if it failed
9+
*/
10+
11+
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index)
12+
{
13+
dlistint_t *head1 = *head, *head2;
14+
unsigned int i;
15+
16+
if (head1)
17+
/* Move to the first node if not already */
18+
while (head1->prev)
19+
head1 = head1->prev;
20+
i = 0;
21+
while (head1)
22+
{
23+
if (i == index)
24+
{
25+
if (i == 0)
26+
{
27+
/* Update head if deleting first node */
28+
*head = head1->next;
29+
if (*head != NULL)
30+
(*head)->prev = NULL;
31+
}
32+
else
33+
{
34+
/* Update previous node's next pointer */
35+
head2->next = head1->next;
36+
/* Update next node's previous pointer */
37+
if (head1->next != NULL)
38+
head1->next->prev = head2;
39+
}
40+
free(head1);
41+
return (1);
42+
}
43+
head2 = head1; /* Moves head2 to the current node */
44+
head1 = head1->next; /* Moves head1 to the nest node */
45+
i++;
46+
}
47+
/* Index out of range, deletion failed */
48+
return (-1);
49+
}

0x17-doubly_linked_lists/8-main.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include "lists.h"
5+
6+
/**
7+
* main - check the code
8+
*
9+
* Return: Always EXIT_SUCCESS.
10+
*/
11+
int main(void)
12+
{
13+
dlistint_t *head;
14+
15+
head = NULL;
16+
add_dnodeint_end(&head, 0);
17+
add_dnodeint_end(&head, 1);
18+
add_dnodeint_end(&head, 2);
19+
add_dnodeint_end(&head, 3);
20+
add_dnodeint_end(&head, 4);
21+
add_dnodeint_end(&head, 98);
22+
add_dnodeint_end(&head, 402);
23+
add_dnodeint_end(&head, 1024);
24+
print_dlistint(head);
25+
printf("-----------------\n");
26+
delete_dnodeint_at_index(&head, 5);
27+
print_dlistint(head);
28+
printf("-----------------\n");
29+
delete_dnodeint_at_index(&head, 0);
30+
print_dlistint(head);
31+
printf("-----------------\n");
32+
delete_dnodeint_at_index(&head, 0);
33+
print_dlistint(head);
34+
printf("-----------------\n");
35+
delete_dnodeint_at_index(&head, 0);
36+
print_dlistint(head);
37+
printf("-----------------\n");
38+
delete_dnodeint_at_index(&head, 0);
39+
print_dlistint(head);
40+
printf("-----------------\n");
41+
delete_dnodeint_at_index(&head, 0);
42+
print_dlistint(head);
43+
printf("-----------------\n");
44+
delete_dnodeint_at_index(&head, 0);
45+
print_dlistint(head);
46+
printf("-----------------\n");
47+
delete_dnodeint_at_index(&head, 0);
48+
printf("-----------------\n");
49+
delete_dnodeint_at_index(&head, 0);
50+
printf("-----------------\n");
51+
delete_dnodeint_at_index(&head, 0);
52+
printf("-----------------\n");
53+
delete_dnodeint_at_index(&head, 0);
54+
printf("-----------------\n");
55+
delete_dnodeint_at_index(&head, 0);
56+
printf("-----------------\n");
57+
delete_dnodeint_at_index(&head, 0);
58+
printf("-----------------\n");
59+
delete_dnodeint_at_index(&head, 0);
60+
printf("-----------------\n");
61+
delete_dnodeint_at_index(&head, 0);
62+
printf("-----------------\n");
63+
delete_dnodeint_at_index(&head, 0);
64+
printf("-----------------\n");
65+
delete_dnodeint_at_index(&head, 0);
66+
print_dlistint(head);
67+
return (0);
68+
}

0x17-doubly_linked_lists/k

16.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)