-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_functions1.c
More file actions
167 lines (144 loc) · 3.67 KB
/
list_functions1.c
File metadata and controls
167 lines (144 loc) · 3.67 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "shell.h"
/* List functions 1 */
/**
* add_node - Adds a new node at the beginning of a 'node_t' list
*
* @head: Pointer to the pointer of the head node
* @str: String to be added to the new node (char)
* @index: Integer value to be stored in the new node (int)
*
* Return: A pointer to the newly created node, or NULL if allocation fails
*/
node_t *add_node(node_t **head, const char *str, int index)
{
node_t *new_node;
/* Allocate memory for the new node */
new_node = malloc(sizeof(node_t));
if (new_node == NULL || head == NULL)
return (NULL);
/* Initialize the new node's memory */
set_memory((void *)new_node, 0, sizeof(node_t));
/* Set index and duplicate string if provided */
new_node->index = index;
if (str)
{
new_node->str = duplicate_string(str);
if (!new_node->str)
{
/* Free allocated memory on failure */
free(new_node);
return (NULL);
}
}
/* Add the new node to the beginning of the linked list */
new_node->next = *head;
*head = new_node;
/* Return a pointer to the new head of the linked list */
return (*head);
}
/**
* add_node_end - Adds a new node at the end of a 'node_t' list
*
* @head: Pointer to the pointer to the head node
* @str: String to be added to the new node (char)
* @index: Integer value to be stored in the new node (int)
*
* Return: A pointer to the head of the updated linked list,
* NULL if allocation fails
*/
node_t *add_node_end(node_t **head, const char *str, int index)
{
node_t *new_node, *temp;
/* Allocate memory for the new node */
new_node = malloc(sizeof(node_t));
if (new_node == NULL || head == NULL)
return (NULL);
/* Initialize the new node's memory */
set_memory((void *)new_node, 0, sizeof(node_t));
/* Set index and duplicate string if provided */
new_node->index = index;
if (str)
{
new_node->str = duplicate_string(str);
if (!new_node->str)
{
/* Free allocated memory on failure */
free(new_node);
return (NULL);
}
}
/* Traverse the linked list to find the end */
temp = *head;
if (temp != NULL)
{
while (temp->next != NULL)
temp = temp->next;
temp->next = new_node;
}
else
*head = new_node;
/* Return a pointer to the head of the linked list */
return (*head);
}
/**
* get_node - Gets the index of a specific node in a linked list
*
* @head: Pointer to the head of the linked list
* @node: Pointer to the node whose index needs to be found
*
* Return: An index of a node, -1 if fail
*/
ssize_t get_node(node_t *head, node_t *node)
{
size_t count;
/* Traverse the linked list to find the node and return its index */
for (count = 0; head != NULL; count++)
{
if (head == node)
return (count);
head = head->next;
}
/* Return -1 if the node is not found or head is NULL */
return (-1);
}
/**
* delete_node - Deletes a node at the specified index from a linked list
*
* @head: Pointer to the pointer to the head of the linked list
* @index: Index of the node to be deleted (int)
*
* Return: 1 if success, 0 if fail.
*/
int delete_node(node_t **head, unsigned int index)
{
unsigned int ind;
node_t *temp, *prev_node;
/* Check if the linked list is empty */
if (*head == NULL)
return (0);
/* Delete the head node if the index is 0 */
if (index == 0)
{
temp = *head;
*head = (*head)->next;
free(temp->str);
free(temp);
return (1);
}
/* Traverse the linked list to find the node at the specified index */
temp = *head;
for (ind = 0; temp != NULL; ind++)
{
if (ind == index)
{
prev_node->next = temp->next;
free(temp->str);
free(temp);
return (1);
}
prev_node = temp;
temp = temp->next;
}
/* Return 0 if the node at the specified index is not found */
return (0);
}