-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.c
More file actions
134 lines (112 loc) · 2.84 KB
/
Copy pathsingly_linked_list.c
File metadata and controls
134 lines (112 loc) · 2.84 KB
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
124
125
126
127
128
129
130
131
132
133
134
#include "singly_linked_list.h"
/**
* sln_new - allocates and initialises memory for a `single_link_node`.
* @data: pointer to the object to initialise with.
* @duplicate_data: function that returns a separate copy of data,
* if NULL the pointer to the original data is stored.
*
* Return: pointer to the new node, NULL on failure.
*/
single_link_node *sln_new(void *const data, dup_func *duplicate_data)
{
single_link_node *new_node = _calloc(1, sizeof(*new_node));
if (!new_node)
return (NULL);
new_node->data = data;
if (duplicate_data)
{
new_node->data = duplicate_data(data);
if (!new_node->data && data)
return (_free(new_node));
}
return (new_node);
}
/**
* sln_insert_after - inserts a `single_link_node` after another.
* @this_node: the node to insert after.
* @other_node: the node to insert.
*
* Return: pointer to the newly inserted node.
*/
single_link_node *sln_insert_after(
single_link_node *const this_node, single_link_node *const other_node)
{
if (!this_node)
return (other_node);
if (!other_node)
return (this_node);
other_node->previous = this_node;
other_node->next = this_node->next;
if (this_node->next)
this_node->next->previous = other_node;
this_node->next = other_node;
return (other_node);
}
/**
* sln_insert_before - insert a single link node before another.
* @this_node: the node to insert before.
* @other_node: the node to insert.
*
* Return: pointer to the newly inserted node.
*/
single_link_node *sln_insert_before(
single_link_node *const this_node, single_link_node *other_node)
{
if (!this_node)
return (other_node);
if (!other_node)
return (this_node);
other_node->next = this_node;
other_node->previous = this_node->previous;
if (this_node->previous)
this_node->previous->next = other_node;
this_node->previous = other_node;
return (other_node);
}
/**
* sln_remove - deletes a node and returns its data.
* @node: the node to delete.
*
* Return: pointer to the data of the node.
*/
void *sln_remove(single_link_node *const node)
{
void *d = NULL;
if (!node)
return (NULL);
d = node->data;
node->data = NULL;
if (node->next)
node->next->previous = node->previous;
if (node->previous)
node->previous->next = node->next;
node->next = NULL;
node->previous = NULL;
_free(node);
return (d);
}
/**
* sll_clear - delete a doubly linked list from memory.
* @head: pointer to the head of the doubly linked list.
* @free_data: function that will be called to free data in the nodes.
*
* Return: NULL always.
*/
void *sll_clear(single_link_node *const head, delete_func *free_data)
{
single_link_node *walk = head;
void *data = NULL;
if (!head)
return (NULL);
while (walk->next)
{
walk = walk->next;
data = sln_remove(walk->previous);
if (free_data)
free_data(data);
}
data = sln_remove(walk);
if (free_data)
free_data(data);
return (NULL);
}