-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc2.c
129 lines (119 loc) · 2.22 KB
/
func2.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
124
125
126
127
128
129
#include "shell.h"
/**
* searchNode - searches linked list for string and
* returns index
* @head: pointer to head of list
* @str: input string
* Return: index of node with matching string
*/
int searchNode(lin_t *head, char *str)
{
register int len = 0, index = 0, i;
lin_t *current;
char *tmp, *ptr;
current = head;
while (current)
{
ptr = _strchr(current->str, '=');
len = ptr - current->str;
tmp = malloc(sizeof(char) * len + 1);
for (i = 0; i < len; i++)
tmp[i] = current->str[i];
tmp[i] = '\0';
if (_strcmp(str, tmp) == 0)
{
free(tmp);
return (index);
}
index++;
current = current->nxt;
free(tmp);
}
return (-1);
}
/**
* generateLinkedList - generates a linked list of environ
* variables
* @array: input array of strings
* Return: head of linked list
*/
lin_t *generateLinkedList(char **array)
{
register int i = 0;
lin_t *head;
head = NULL;
while (array[i])
{
addNodeEnd(&head, array[i]);
i++;
}
return (head);
}
/**
* addNodeAtIndex - add node at index with string
* @head: double pointer to head
* @index: index to add at
* @str: string to add
* Return: address of node added
*/
lin_t *addNodeAtIndex(lin_t **head, int index, char *str)
{
register int i = 0;
lin_t *newNode, *current;
char *newStr;
current = *head;
if (!str)
return (NULL);
newNode = malloc(sizeof(lin_t));
if (!newNode)
{
perror("Malloc failed\n");
exit(errno);
}
newStr = _strdup(str);
if (!newStr)
{
free(newNode);
perror("Malloc failed\n");
exit(errno);
}
newNode->str = newStr;
newNode->nxt = NULL;
while (i < index - 1)
{
if (current->nxt == NULL)
{
free(newNode);
return (NULL);
}
current = current->nxt;
i++;
}
newNode->nxt = current->nxt;
current->nxt = newNode;
return (newNode);
}
/**
* getNodeAtIndex - returns the nth node of a listint_t linked list
* @head: pointer to head of list
* @index: index of value to be returned
* Return: address of node at input index
*/
char *getNodeAtIndex(lin_t *head, unsigned int index)
{
register uint count = 0;
lin_t *current;
char *ptr;
current = head;
while (current)
{
if (count == index)
{
ptr = _strdup(current->str);
return (ptr);
}
count++;
current = current->nxt;
}
return (NULL);
}