Skip to content

Commit

Permalink
modified 3-add_dnodeint_end.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Collins331 committed Sep 15, 2023
1 parent d82445d commit c904dfa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 52 deletions.
48 changes: 25 additions & 23 deletions 0x17-doubly_linked_lists/3-add_dnodeint_end.c
Original file line number Diff line number Diff line change
@@ -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);
}
58 changes: 29 additions & 29 deletions 0x17-doubly_linked_lists/8-delete_dnodeint.c
Original file line number Diff line number Diff line change
@@ -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);
}
File renamed without changes.

0 comments on commit c904dfa

Please sign in to comment.