Skip to content

Commit

Permalink
2-add_dnodeint.c
Browse files Browse the repository at this point in the history
  • Loading branch information
dersotakele committed Jan 11, 2024
1 parent f590f46 commit 07f071d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 0x17-doubly_linked_lists/2-add_dnodeint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "lists.h"

/**
* add_dnodeint - adds a new node at the beginning
* of a dlistint_t list
*
* @head: head of the list
* @n: value of the element
* Return: the address of the new element
*/
dlistint_t *add_dnodeint(dlistint_t **head, const int n)
{
dlistint_t *new;
dlistint_t *h;

new = malloc(sizeof(dlistint_t));
if (new == NULL)
return (NULL);

new->n = n;
new->prev = NULL;
h = *head;

if (h != NULL)
{
while (h->prev != NULL)
h = h->prev;
}

new->next = h;

if (h != NULL)
h->prev = new;

*head = new;

return (new);
}
26 changes: 26 additions & 0 deletions 0x17-doubly_linked_lists/2-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "lists.h"

/**
* main - check the code
*
* Return: Always EXIT_SUCCESS.
*/
int main(void)
{
dlistint_t *head;

head = NULL;
add_dnodeint(&head, 0);
add_dnodeint(&head, 1);
add_dnodeint(&head, 2);
add_dnodeint(&head, 3);
add_dnodeint(&head, 4);
add_dnodeint(&head, 98);
add_dnodeint(&head, 402);
add_dnodeint(&head, 1024);
print_dlistint(head);
return (EXIT_SUCCESS);
}
Binary file added 0x17-doubly_linked_lists/c
Binary file not shown.

0 comments on commit 07f071d

Please sign in to comment.