From 09e10bba3106086c25c83087ea29cbb374d24c85 Mon Sep 17 00:00:00 2001 From: 99omniaashraf Date: Tue, 23 Jan 2024 17:27:00 -0800 Subject: [PATCH] Double linked list --- 0x00-challenge/4-delete_dnodeint/README.md | 1 + .../4-delete_dnodeint/add_dnodeint_end.c | 39 +++++++++++ .../4-delete_dnodeint/free_dlistint.c | 20 ++++++ 0x00-challenge/4-delete_dnodeint/lists.h | 27 ++++++++ 0x00-challenge/4-delete_dnodeint/main.c | 69 +++++++++++++++++++ .../4-delete_dnodeint/print_dlistint.c | 23 +++++++ 6 files changed, 179 insertions(+) create mode 100644 0x00-challenge/4-delete_dnodeint/README.md create mode 100644 0x00-challenge/4-delete_dnodeint/add_dnodeint_end.c create mode 100644 0x00-challenge/4-delete_dnodeint/free_dlistint.c create mode 100644 0x00-challenge/4-delete_dnodeint/lists.h create mode 100644 0x00-challenge/4-delete_dnodeint/main.c create mode 100644 0x00-challenge/4-delete_dnodeint/print_dlistint.c diff --git a/0x00-challenge/4-delete_dnodeint/README.md b/0x00-challenge/4-delete_dnodeint/README.md new file mode 100644 index 0000000..6ff2eb6 --- /dev/null +++ b/0x00-challenge/4-delete_dnodeint/README.md @@ -0,0 +1 @@ +4-delete_dnodeint/ diff --git a/0x00-challenge/4-delete_dnodeint/add_dnodeint_end.c b/0x00-challenge/4-delete_dnodeint/add_dnodeint_end.c new file mode 100644 index 0000000..15511a7 --- /dev/null +++ b/0x00-challenge/4-delete_dnodeint/add_dnodeint_end.c @@ -0,0 +1,39 @@ +#include +#include +#include "lists.h" + +/** + * add_dnodeint_end - Add a node at the end of a list + * + * @head: The address of the pointer to the first element of the list + * @n: The number to store in the new element + * + * Return: A pointer to the new element + */ +dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) +{ + dlistint_t *new; + dlistint_t *l; + + new = malloc(sizeof(dlistint_t)); + if (new == NULL) + { + return (NULL); + } + new->n = n; + new->next = NULL; + if (*head == NULL) + { + *head = new; + new->prev = NULL; + return (new); + } + l = *head; + while (l->next != NULL) + { + l = l->next; + } + l->next = new; + new->prev = l; + return (new); +} diff --git a/0x00-challenge/4-delete_dnodeint/free_dlistint.c b/0x00-challenge/4-delete_dnodeint/free_dlistint.c new file mode 100644 index 0000000..1d388ab --- /dev/null +++ b/0x00-challenge/4-delete_dnodeint/free_dlistint.c @@ -0,0 +1,20 @@ +#include +#include +#include "lists.h" + +/** + * free_dlistint - Free a list + * + * @head: A pointer to the first element of the list + */ +void free_dlistint(dlistint_t *head) +{ + dlistint_t *node; + + while (head) + { + node = head; + head = head->next; + free(node); + } +} diff --git a/0x00-challenge/4-delete_dnodeint/lists.h b/0x00-challenge/4-delete_dnodeint/lists.h new file mode 100644 index 0000000..459a088 --- /dev/null +++ b/0x00-challenge/4-delete_dnodeint/lists.h @@ -0,0 +1,27 @@ +#ifndef _LISTS_H_ +#define _LISTS_H_ + +#include + +/** + * struct dlistint_s - doubly linked list + * @n: integer + * @prev: points to the prev node + * @next: points to the next node + * + * Description: doubly linked list node structure + * for Holberton project + */ +typedef struct dlistint_s +{ + int n; + struct dlistint_s *prev; + struct dlistint_s *next; +} dlistint_t; + +size_t print_dlistint(const dlistint_t *h); +dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); +int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); +void free_dlistint(dlistint_t *head); + +#endif diff --git a/0x00-challenge/4-delete_dnodeint/main.c b/0x00-challenge/4-delete_dnodeint/main.c new file mode 100644 index 0000000..5aefd89 --- /dev/null +++ b/0x00-challenge/4-delete_dnodeint/main.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "lists.h" + +/** + * main - check the code for Holberton School students. + * + * Return: Always EXIT_SUCCESS. + */ +int main(void) +{ + dlistint_t *head; + + head = NULL; + add_dnodeint_end(&head, 0); + add_dnodeint_end(&head, 1); + add_dnodeint_end(&head, 2); + add_dnodeint_end(&head, 3); + add_dnodeint_end(&head, 4); + add_dnodeint_end(&head, 98); + add_dnodeint_end(&head, 402); + add_dnodeint_end(&head, 1024); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 5); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + printf("-----------------\n"); + delete_dnodeint_at_index(&head, 0); + print_dlistint(head); + free_dlistint(head); + return (0); +} diff --git a/0x00-challenge/4-delete_dnodeint/print_dlistint.c b/0x00-challenge/4-delete_dnodeint/print_dlistint.c new file mode 100644 index 0000000..d0b97da --- /dev/null +++ b/0x00-challenge/4-delete_dnodeint/print_dlistint.c @@ -0,0 +1,23 @@ +#include +#include "lists.h" + +/** + * print_dlistint - Prints a doubly linkedlist of integers + * + * @h: A pointer to the first element of a list + * + * Return: The number of element printed + */ +size_t print_dlistint(const dlistint_t *h) +{ + size_t n; + + n = 0; + while (h) + { + printf("%d\n", h->n); + h = h->next; + n++; + } + return (n); +}