-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.c
95 lines (79 loc) · 1.29 KB
/
lists.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
#include "shell.h"
static sl_list *head;
/**
* insert_node - inserts node
* @key: variable name
* @add: address
*
* Return: sl_list ptr
*/
sl_list *insert_node(char *key, char *add)
{
sl_list *newNode;
newNode = malloc(sizeof(sl_list));
if (newNode != NULL)
{
newNode->key = _strdup(key);
newNode->add = add;
newNode->next = head;
head = newNode;
return (newNode);
}
else
return (NULL);
}
/**
* delete_node - deletes node
* @key: name of variable
*
* Return: lis
*/
int delete_node(const char *key)
{
sl_list *cur_node, *prev_node;
if (head != NULL)
{
prev_node = head;
while (prev_node != NULL)
{
cur_node = prev_node->next;
if (_strcmp(key, cur_node->key) == 0)
{
prev_node->next = cur_node->next;
free(cur_node->add);
free(cur_node->key);
free(cur_node);
cur_node = NULL;
return (0);
}
prev_node = prev_node->next;
}
}
return (-1);
}
/**
* free_list - frees a whole list
* @head: head node
*
*/
void free_list(sl_list *head)
{
sl_list *cur_node, *next_node;
cur_node = head;
while (cur_node != NULL)
{
next_node = cur_node->next;
free(cur_node->add);
free(cur_node->key);
free(cur_node);
cur_node = next_node;
}
}
/**
* free_env - helper
*/
void free_env(void)
{
if (head != NULL)
free_list(head);
}