-
Notifications
You must be signed in to change notification settings - Fork 4
/
lists_func.c
123 lines (110 loc) · 1.99 KB
/
lists_func.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "monty.h"
/**
* add_dnodeint - add node to the beginning of list
* @head: pointer to first node
* @n: data inside node
* Return: pointer to first node
*/
stack_t *add_dnodeint(stack_t **head, const int n)
{
stack_t *new;
new = malloc(sizeof(stack_t));
if (new == NULL)
return (NULL);
if (*head == NULL)
{
new->n = n;
new->next = NULL;
new->prev = NULL;
*head = new;
return (*head);
}
(*head)->prev = new;
new->n = n;
new->next = *head;
new->prev = NULL;
*head = new;
return (*head);
}
/**
* delete_dnodeint_at_index - delete node a specific spot
* @head: pointer to first node on list
* @index: position to delete
* Return: 1 if successful, -1 if failure
*/
int delete_dnodeint_at_index(stack_t **head, unsigned int index)
{
stack_t *tmp;
stack_t *tmp2;
unsigned int i;
if (*head == NULL)
return (-1);
tmp = *head;
if (index == 0)
{
*head = tmp->next;
if (tmp->next != NULL)
tmp->next->prev = NULL;
free(tmp);
return (1);
}
i = 0;
while (i < (index - 1))
{
if (tmp == NULL)
return (-1);
tmp = tmp->next;
i++;
}
tmp2 = (tmp->next)->next;
if (tmp->next->next != NULL)
tmp->next->next->prev = tmp;
free(tmp->next);
tmp->next = tmp2;
return (1);
}
/**
* add_dnodeint_end - add node to end of list
* @head: pointer to first node
* @n: data inside node
* Return: pointer to first node
*/
stack_t *add_dnodeint_end(stack_t **head, const int n)
{
stack_t *tmp = *head;
stack_t *new_node;
new_node = malloc(sizeof(stack_t));
if (new_node == NULL)
return (NULL);
new_node->n = n;
if (*head == NULL)
{
new_node->next = NULL;
new_node->prev = NULL;
*head = new_node;
return (new_node);
}
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = new_node;
new_node->prev = tmp;
new_node->next = NULL;
return (new_node);
}
/**
* free_dlistint - free a list
* @head: pointer to first node
*
*/
void free_dlistint(stack_t *head)
{
stack_t *tmp;
while (head != NULL)
{
tmp = head->next;
free(head);
head = tmp;
}
}