-
Notifications
You must be signed in to change notification settings - Fork 17
/
doubly_functions.c
90 lines (84 loc) · 1.56 KB
/
doubly_functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "monty.h"
/**
*add_dnodeint_end - add a note at the end of the doubly link list
*@head: first position of linked list
*@n: data to store
*Return: a doubly linked list
*/
stack_t *add_dnodeint_end(stack_t **head, const int n)
{
stack_t *temp, *aux;
if (head == NULL)
return (NULL);
temp = malloc(sizeof(stack_t));
if (!temp)
{
dprintf(2, "Error: malloc failed\n");
free_vglo();
exit(EXIT_FAILURE);
}
temp->n = n;
/*Careful with the first time*/
if (*head == NULL)
{
temp->next = *head;
temp->prev = NULL;
*head = temp;
return (*head);
}
aux = *head;
while (aux->next)
aux = aux->next;
temp->next = aux->next;
temp->prev = aux;
aux->next = temp;
return (aux->next);
}
/**
*add_dnodeint - add a note at the begining of the doubly link list
*@head: first position of linked list
*@n: data to store
*Return: a doubly linked list
*/
stack_t *add_dnodeint(stack_t **head, const int n)
{
stack_t *temp;
if (head == NULL)
return (NULL);
temp = malloc(sizeof(stack_t));
if (!temp)
{
dprintf(2, "Error: malloc failed\n");
free_vglo();
exit(EXIT_FAILURE);
}
temp->n = n;
/*Careful with the first time*/
if (*head == NULL)
{
temp->next = *head;
temp->prev = NULL;
*head = temp;
return (*head);
}
(*head)->prev = temp;
temp->next = (*head);
temp->prev = NULL;
*head = temp;
return (*head);
}
/**
* free_dlistint - frees the doubly linked list
*
* @head: head of the list
* Return: no return
*/
void free_dlistint(stack_t *head)
{
stack_t *tmp;
while ((tmp = head) != NULL)
{
head = head->next;
free(tmp);
}
}