-
Notifications
You must be signed in to change notification settings - Fork 8
/
linkedlist.c
executable file
·113 lines (98 loc) · 2.12 KB
/
linkedlist.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
#include "shell.h"
alias_t *add_alias_end(alias_t **head, char *name, char *value);
void free_alias_list(alias_t *head);
list_t *add_node_end(list_t **head, char *dir);
void free_list(list_t *head);
/**
* add_alias_end - Adds a node to the end of a alias_t linked list.
* @head: A pointer to the head of the list_t list.
* @name: The name of the new alias to be added.
* @value: The value of the new alias to be added.
*
* Return: If an error occurs - NULL.
* Otherwise - a pointer to the new node.
*/
alias_t *add_alias_end(alias_t **head, char *name, char *value)
{
alias_t *new_node = malloc(sizeof(alias_t));
alias_t *last;
if (!new_node)
return (NULL);
new_node->next = NULL;
new_node->name = malloc(sizeof(char) * (_strlen(name) + 1));
if (!new_node->name)
{
free(new_node);
return (NULL);
}
new_node->value = value;
_strcpy(new_node->name, name);
if (*head)
{
last = *head;
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
else
*head = new_node;
return (new_node);
}
/**
* add_node_end - Adds a node to the end of a list_t linked list.
* @head: A pointer to the head of the list_t list.
* @dir: The directory path for the new node to contain.
*
* Return: If an error occurs - NULL.
* Otherwise - a pointer to the new node.
*/
list_t *add_node_end(list_t **head, char *dir)
{
list_t *new_node = malloc(sizeof(list_t));
list_t *last;
if (!new_node)
return (NULL);
new_node->dir = dir;
new_node->next = NULL;
if (*head)
{
last = *head;
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
else
*head = new_node;
return (new_node);
}
/**
* free_alias_list - Frees a alias_t linked list.
* @head: THe head of the alias_t list.
*/
void free_alias_list(alias_t *head)
{
alias_t *next;
while (head)
{
next = head->next;
free(head->name);
free(head->value);
free(head);
head = next;
}
}
/**
* free_list - Frees a list_t linked list.
* @head: The head of the list_t list.
*/
void free_list(list_t *head)
{
list_t *next;
while (head)
{
next = head->next;
free(head->dir);
free(head);
head = next;
}
}