-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_lis.c
More file actions
178 lines (147 loc) · 2.66 KB
/
linked_lis.c
File metadata and controls
178 lines (147 loc) · 2.66 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
168
169
170
171
172
173
174
175
176
#include "s_shell.h"
/**
* addNode - adds a node to the start of the list
* @head: address of pointer to head node
* @data: data field of node
* @num: node index used by history
*
* Return: size of list
*/
str_list_t *addNode(str_list_t **head, const char *data, int num)
{
str_list_t *new_node;
if (!head)
return (NULL);
new_node = malloc(sizeof(str_list_t));
if (!new_node)
return (NULL);
memSet((void *)new_node, 0, sizeof(str_list_t));
new_node->num = num;
if (data)
{
new_node->str = _strdup(data);
if (!new_node->str)
{
free(new_node);
return (NULL);
}
}
new_node->next = *head;
*head = new_node;
return (new_node);
}
/**
* addNodeEnd - adds a node to the end of the list
* @head: address of pointer to head node
* @data: data field of node
* @num: node index used by history
*
* Return: size of list
*/
str_list_t *addNodeEnd(str_list_t **head, const char *data, int num)
{
str_list_t *new_node, *node;
if (!head)
return (NULL);
node = *head;
new_node = malloc(sizeof(str_list_t));
if (!new_node)
return (NULL);
memSet((void *)new_node, 0, sizeof(str_list_t));
new_node->num = num;
if (data)
{
new_node->str = _strdup(data);
if (!new_node->str)
{
free(new_node);
return (NULL);
}
}
if (node)
{
while (node->next)
node = node->next;
node->next = new_node;
}
else
*head = new_node;
return (new_node);
}
/**
* printStr - prints only the str element of a str_list_t linked list
* @h: pointer to first node
*
* Return: size of list
*/
size_t printStr(const str_list_t *h)
{
size_t i = 0;
while (h)
{
_puts(h->str ? h->str : "(nil)");
_puts("\n");
h = h->next;
i++;
}
return (i);
}
/**
* deleteNodeAtIndex - deletes node at given index
* @head: address of pointer to first node
* @index: index
*
* Return: 1 on success, 0 on failure
*/
int deleteNodeAtIndex(str_list_t **head, unsigned int index)
{
str_list_t *node, *prev_node;
unsigned int i = 0;
if (!head || !*head)
return (0);
if (!index)
{
node = *head;
*head = (*head)->next;
free(node->str);
free(node);
return (1);
}
node = *head;
while (node)
{
if (i == index)
{
prev_node->next = node->next;
free(node->str);
free(node);
return (1);
}
i++;
prev_node = node;
node = node->next;
}
return (0);
}
/**
* freeList - frees all nodes of a list
* @head_ptr: address of pointer to head node
*
* Return: void
*/
void freeList(str_list_t **head_ptr)
{
str_list_t *node, *next_node, *head;
if (!head_ptr || !*head_ptr)
return;
head = *head_ptr;
node = head;
while (node)
{
next_node = node->next;
free(node->str);
free(node);
node = next_node;
}
*head_ptr = NULL;
}