Skip to content

Commit b8acceb

Browse files
committed
add tasks 5,8
1 parent 185ec26 commit b8acceb

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "lists.h"
2+
3+
/**
4+
* insert_dnodeint_at_index - inserts a new node at a given position
5+
* @h: header of double linked list
6+
* @idx: index of the node, starting from 0
7+
* @n: is a given number
8+
* Return: a address of nth node
9+
*/
10+
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n)
11+
{
12+
dlistint_t *new, *headcopy = *h;
13+
unsigned int i;
14+
15+
new = malloc(sizeof(dlistint_t));
16+
if (new == NULL)
17+
return (NULL);
18+
new->n = n;
19+
while (headcopy != NULL && headcopy->prev != NULL)
20+
{
21+
headcopy = headcopy->prev;
22+
*h = (*h)->prev;
23+
}
24+
if (idx == 0)
25+
{
26+
free(new);
27+
return (add_dnodeint(h, n));
28+
}
29+
30+
for (i = 0; (i < idx - 1) && headcopy != NULL; i++)
31+
headcopy = headcopy->next;
32+
if (headcopy == NULL)
33+
{
34+
free(new);
35+
return (NULL);
36+
}
37+
if (headcopy->next == NULL)
38+
{
39+
new->next = NULL;
40+
new->prev = headcopy;
41+
headcopy->next = new;
42+
} else
43+
{
44+
new->next = headcopy->next;
45+
new->prev = headcopy;
46+
headcopy->next->prev = new;
47+
headcopy->next = new;
48+
}
49+
return (new);
50+
}

0x17-doubly_linked_lists/7-main.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
insert_dnodeint_at_index(&head, 5, 4096);
27+
print_dlistint(head);
28+
free_dlistint(head);
29+
head = NULL;
30+
return (EXIT_SUCCESS);
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "lists.h"
2+
3+
/**
4+
* delete_dnodeint_at_index - Deletes the node at idx of a double linked list
5+
* @head: header of double linked list
6+
* @index: index of the node, starting from 0
7+
* Return: 1 if it succeeded, -1 if it failed
8+
*/
9+
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index)
10+
{
11+
dlistint_t *pre, *headcopy = *head;
12+
unsigned int i;
13+
14+
while (headcopy != NULL && headcopy->prev != NULL)
15+
{
16+
headcopy = headcopy->prev;
17+
*head = (*head)->prev;
18+
}
19+
if (headcopy != NULL && index != 0)
20+
{
21+
for (i = 0; i < index && headcopy != NULL; i++)
22+
{
23+
pre = headcopy;
24+
headcopy = headcopy->next;
25+
}
26+
if (headcopy != NULL)
27+
{
28+
pre->next = headcopy->next;
29+
if (pre->next != NULL)
30+
headcopy->next->prev = pre;
31+
free(headcopy);
32+
return (1);
33+
}
34+
return (-1);
35+
}
36+
if (headcopy != NULL && index == 0)
37+
{
38+
pre = headcopy->next;
39+
if (pre == NULL)
40+
*head = NULL;
41+
else
42+
{
43+
pre->prev = NULL;
44+
free(headcopy);
45+
*head = pre;
46+
}
47+
return (1);
48+
}
49+
return (-1);
50+
}

0x17-doubly_linked_lists/j

16.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)