From c904dfa93cfae7779e22828e9eb8ccb6ea83898b Mon Sep 17 00:00:00 2001 From: Collins331 Date: Fri, 15 Sep 2023 22:44:48 +0300 Subject: [PATCH] modified 3-add_dnodeint_end.c --- 0x17-doubly_linked_lists/3-add_dnodeint_end.c | 48 +++++++-------- 0x17-doubly_linked_lists/8-delete_dnodeint.c | 58 +++++++++---------- .../{Assets => }/8-main.c | 0 3 files changed, 54 insertions(+), 52 deletions(-) rename 0x17-doubly_linked_lists/{Assets => }/8-main.c (100%) diff --git a/0x17-doubly_linked_lists/3-add_dnodeint_end.c b/0x17-doubly_linked_lists/3-add_dnodeint_end.c index 9e74e4f..59b349b 100644 --- a/0x17-doubly_linked_lists/3-add_dnodeint_end.c +++ b/0x17-doubly_linked_lists/3-add_dnodeint_end.c @@ -1,35 +1,37 @@ #include "lists.h" + /** - * add_dnodeint_end - inserts node at the end of linked list - * @head: pointer to the linked list - * @n: the value to insert - * Return: the pointer to the inserted node + * add_dnodeint_end - adds a new node at the end of a dlistint_t list. + * @head: double pointer to the head of the list + * @n: integer to add to the new node + * + * Return: the address of the new element, or NULL if it failed */ + dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) { - dlistint_t *newnode; + dlistint_t *new_node; - newnode = malloc(sizeof(dlistint_t)); - if (newnode == NULL) - { + new_node = malloc(sizeof(dlistint_t)); + + if (new_node == NULL) return (NULL); - } - newnode->n = n; - newnode->next = NULL; + + new_node->n = n; + new_node->next = NULL; + if (*head == NULL) { - newnode->prev = NULL; - *head = newnode; - return (newnode); + new_node->prev = NULL; + *head = new_node; + return (new_node); } - else - { - dlistint_t *tmp = *head; - while (tmp->next != NULL) - tmp = tmp->next; - tmp->next = newnode; - newnode->prev = tmp; - } - return (newnode); + while ((*head)->next != NULL) + *head = (*head)->next; + + (*head)->next = new_node; + new_node->prev = *head; + + return (new_node); } diff --git a/0x17-doubly_linked_lists/8-delete_dnodeint.c b/0x17-doubly_linked_lists/8-delete_dnodeint.c index e5eab43..3427ce1 100644 --- a/0x17-doubly_linked_lists/8-delete_dnodeint.c +++ b/0x17-doubly_linked_lists/8-delete_dnodeint.c @@ -1,33 +1,33 @@ -#include "lists.h" +#include "lists.h" -/** - * delete_dnodeint_at_index - deletes the node at index of a list - * @head: head - * @index: index - * Return: 1 if it succeeded, -1 if it failed - */ +/** + * delete_dnodeint_at_index - deletes the node at index of a list + * @head: head + * @index: index + * Return: 1 if it succeeded, -1 if it failed + */ -int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) -{ - dlistint_t *current = *head; - unsigned int i = 0; +int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) +{ + dlistint_t *current = *head; + unsigned int i = 0; - if (*head == NULL || head == NULL) - return (-1); + if (*head == NULL || head == NULL) + return (-1); - while (i < index) - { - if (current == NULL) - return (-1); - current = current->next; - i++; - } - if (current == *head) - *head = current->next; - if (current->next != NULL) - current->next->prev = current->prev; - if (current->prev != NULL) - current->prev->next = current->next; - free(current); - return (1); -} + while (i < index) + { + if (current == NULL) + return (-1); + current = current->next; + i++; + } + if (current == *head) + *head = current->next; + if (current->next != NULL) + current->next->prev = current->prev; + if (current->prev != NULL) + current->prev->next = current->next; + free(current); + return (1); +} diff --git a/0x17-doubly_linked_lists/Assets/8-main.c b/0x17-doubly_linked_lists/8-main.c similarity index 100% rename from 0x17-doubly_linked_lists/Assets/8-main.c rename to 0x17-doubly_linked_lists/8-main.c