-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.c
142 lines (131 loc) · 2.44 KB
/
func.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
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "shell.h"
/**
* addNode - adds a new node at the beginning of a lin_t list
* @head: reference to head of linked list
* @str: string to be added on new node
* Return: address of new head;
*/
lin_t *addNode(lin_t **head, char *str)
{
lin_t *newNode;
char *newStr;
if (!str)
return (NULL);
newNode = malloc(sizeof(lin_t));
if (!newNode)
{
perror("Malloc failed\n");
exit(errno);
}
newStr = _strdup(str);
if (!newStr)
{
perror("Malloc failed\n");
exit(errno);
}
newNode->str = newStr;
newNode->nxt = *head;
*head = newNode;
return (*head);
}
/**
* addNodeEnd - adds a new node at the end of a lin_t list;
* @head: reference to head of list
* @str: string to be added to linked list
* Return: address of new node
*/
lin_t *addNodeEnd(lin_t **head, char *str)
{
lin_t *newNode;
lin_t *last = *head;
char *newStr;
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;
if (!*head)
{
*head = newNode;
return (newNode);
}
while (last->nxt)
last = last->nxt;
last->nxt = newNode;
return (last);
}
/**
* printList - prints all elements of a lin_t list
* @h: pointer to head of list
* Return: number of elements
*/
size_t printList(const lin_t *h)
{
register int count = 0;
while (h)
{
write(STDOUT_FILENO, h->str, _strlen(h->str));
displayNewLine();
h = h->nxt;
count++;
}
return (count);
}
/**
* deleteNodeAtIndex - deletes the node at index
* index of a lin_t linked list
* @head: double pointer to head of list
* @index: index of node to be deleted
* Return: 1 if success, -1 if fail
*/
int deleteNodeAtIndex(lin_t **head, unsigned int index)
{
lin_t *current;
lin_t *next;
register uint i;
if (!head || !(*head))
return (-1);
current = *head;
if (!index)
{
*head = current->nxt;
free(current);
return (1);
}
for (i = 0; current && i < index - 1; i++)
current = current->nxt;
if (!current || !(current->nxt))
return (-1);
next = current->nxt->nxt;
free(current->nxt->str);
free(current->nxt);
current->nxt = next;
return (1);
}
/**
* list_len - returns the number of elements in a linked list
* @h: head of linked list
* Return: number of elements in list_t
*/
size_t list_len(lin_t *h)
{
register unsigned int count = 0;
while (h)
{
h = h->nxt;
count++;
}
return (count);
}