-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSS_lists1.c
More file actions
128 lines (109 loc) · 2.39 KB
/
SS_lists1.c
File metadata and controls
128 lines (109 loc) · 2.39 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
#include "shell.h"
/**
* list_len - this function determines length of linked list
* @h: parameter showing the pointer to first node
* Return: 0 if successful
*/
size_t list_len(const list_t *h)
{
size_t len = 0;
while (h)
{
h = h->next;
len++;
}
return (len);
}
/**
* list_to_strings - this function convert str field of each node in the linked
* list into an array of string
* @head: parameter showing thee pointer to first node
* Return: array of strings
*/
char **list_to_strings(list_t *head)
{
char **strs;
list_t *node = head;
size_t a = list_len(head);
char *str;
size_t b;
if (!head || !a)
return (NULL);
strs = malloc(sizeof(char *) * (a + 1));
if (!strs)
return (NULL);
for (a = 0; node; node = node->next, a++)
{
str = malloc(_strlen(node->str) + 1);
if (!str)
{
for (b = 0; b < a; b++)
free(strs[b]);
free(strs);
return (NULL);
}
str = _strcpy(str, node->str);
strs[a] = str;
}
strs[a] = NULL;
return (strs);
}
/**
* print_list - this function prints all elements of a list_t linked list
* @h: function parameter indicating the pointer to first node
* Return: 0 if successful
*/
size_t print_list(const list_t *h)
{
size_t a;
while (h)
{
_puts(convert_number(h->num, 10, 0));
_putchar(':');
_putchar(' ');
_puts(h->str ? h->str : "(nil)");
_puts("\n");
h = h->next;
a++;
}
return (a);
}
/**
* node_starts_with - this function searches for a node whose string start at a
* given prefix and matches a character
* @node: function parameter indicating the pointer to list head
* @prefix: function parameter indicating string to match
* @c: function parameter showing the next character after prefix to match
* Return: 0 if successful
*/
list_t *node_starts_with(list_t *node, char *prefix, char c)
{
char *a = NULL;
while (node)
{
a = starts_with(node->str, prefix);
if (a && ((c == -1) || (*a == c)))
return (node);
node = node->next;
}
return (NULL);
}
/**
* get_node_index - function parameter gets the index of a node in a linked lis
* @head: function parameter showing the pointer to list head
* @node: function parameter indicating the pointer to the node
* Return: 0 if successful
*/
ssize_t get_node_index(list_t *head, list_t *node)
{
size_t index = 0;
list_t *current = head;
while (current != NULL)
{
if (current == node)
return (index);
current = current->next;
index++;
}
return (-1);
}