-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d82445d
commit c904dfa
Showing
3 changed files
with
54 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.